Home > Back-end >  Create certificates with openssl from csv script.sh Ubuntu Server
Create certificates with openssl from csv script.sh Ubuntu Server

Time:07-15

I'm creating certificates in a script.sh to generate certificates with all the data of a user, but I don't know how to bring that data from a CSV, I managed to read the data but I can't figure out how to put it in the command.

my CSV contains (one thousand records):

Country, place, city, company, nameuser, email
EU,HOME,HOME1,DESKTOP,USERNAME,[email protected]
xx,xxx,xxxx,xxxx,xxxx,xxxx
xx,xxx,xxxx,xxxx,xxxx,xxxx
etc....
#!/bin/bash

openssl \
        req -x509 \
        -newkey rsa:4096 \
        -keyout user.key \
        -out user.crt \
        -days 365 \
        -nodes \
        -subj "/C=EU/ST=HOME/L=HOME1/O=Desktop/CN=USERNAME/[email protected]"

thank you!!!

CodePudding user response:

If that CSV data is truly that straightforward, it could be done with a few lines of bash like this (this assumes the CSV data is in data.csv):

#!/bin/bash

# Skip the first line, then read the comma-separated lines into individual variables
tail -n  2 data.csv | while IFS=, read f1 f2 f3 f4 f5 f6; do
    echo openssl \
        req -x509 \
        -newkey rsa:4096 \
        -days 365 \
        -keyout "$f5.key" \
        -out "$f5.crt" \
        -nodes \
        -subj "/C=$f1/ST=$f2/L=$f3/O=$f4/CN=$f5/emailAddress=$f6"
done

For demonstration purposes I prefixed it with an echo there, just remove that to run the actual commands.

With input like the following...

Country, place, city, company, nameuser, email
EU,HOME,HOME1,DESKTOP,USERNAME,[email protected]
x1,xx2,xxx3,xxx4,xxx5,xxx6
y1,yy2,yyy3,yyy4,yyy5,yyy6

... the script will generate command-lines like this (I assumed you would also want unique *.crt and *.key filenames, keyed on the username, by the way):

$ ./cert_gen.sh
openssl req -x509 -newkey rsa:4096 -keyout USERNAME.key -out USERNAME.crt -days 365 -nodes -subj /C=EU/ST=HOME/L=HOME1/O=DESKTOP/CN=USERNAME/[email protected]
openssl req -x509 -newkey rsa:4096 -keyout xxx5.key -out xxx5.crt -days 365 -nodes -subj /C=x1/ST=xx2/L=xxx3/O=xxx4/CN=xxx5/emailAddress=xxx6
openssl req -x509 -newkey rsa:4096 -keyout yyy5.key -out yyy5.crt -days 365 -nodes -subj /C=y1/ST=yy2/L=yyy3/O=yyy4/CN=yyy5/emailAddress=yyy6
  • Related