My question is quite similar to another one here but not quite the same. I have a sequence of commands to create an ssl key/crt ect. And I want to be able to create an automated, default one. These are the commands (they came from this page):openssl genrsa -des3 -out server.key 2048openssl req -new -key server.key -out server.csrcp server.key server.key.orgopenssl rsa -in server.key.org -out server.keyopenssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crtIf each one only took one argument then it would be fine and I would do something like openssl genrsa -des3 -out server.key 2048 <<< arg1But one of them needs as many as 10 inputs which it asks for sequentially.I tried something like this but it didn't workopenssl genrsa -des3 -out server.key 2048 << fooarg1arg2fooEDIT: This approach is actually working I think but not for the arguments that are supposed to be passwords. Does anyone have a workaround for that?Could it make a difference that some of the arguments are passwords?What is the simplest way to go about this?
Automate textual input to a command from a bash script
bash;shell script
This works as expected...I've been piping heredocs to openssl to create certs for years (e.g. i wrote the script below sometime in 2002, and that's the new version of the script...no idea when i first wrote it).You need to provide ALL of the inputs that openssl expects, in the exact order that it expects them, even if some of those inputs are just a blank line (to accept the default).For example, here's (a slightly edited version of) my script to generate self-signed certs for postfix:#! /bin/shumask 077# $site is used for the subdir to hold the certs AND for# the certificate's Common Namesite=$1mkdir -p $siteumask 277REQ=$site/key.pemCERT=$site/cert.pemSERV=$site/server.pemFING=$site/cert.fingerprint# certificate details for herenow script (configurable)COUNTRY=AU                # 2 letter country-codeSTATE=Victoria            # state or province nameLOCALITY=Melbourne        # Locality Name (e.g. city)ORGNAME=organisation name # Organization Name (eg, company)ORGUNIT=                  # Organizational Unit Name (eg. section)EMAIL=root@example.com    # certificate's email address# optional extra detailsCHALLENGE=                # challenge passwordCOMPANY=                  # company nameDAYS=-days 365# create the certificate requestcat <<__EOF__ | openssl req -new $DAYS -nodes -keyout $REQ -out $REQ$COUNTRY$STATE$LOCALITY$ORGNAME$ORGUNIT$site$EMAIL$CHALLENGE$COMPANY__EOF__# sign it - will ask for demoCA's passwordopenssl ca $DAYS -policy policy_anything -out $CERT -infiles $REQ# cert has to be readable by postfixchmod 644 $CERT# create server.pem for smtpd by concatenating the certificate (cert.pem) +# demoCA's public certificate + the host's private key (key.pem)cat $CERT ./demoCA/cacert.pem $REQ >$SERV# create fingerprint fileopenssl x509 -fingerprint -in $CERT -noout > $FINGNOTE: there is no error-checking here, just assumptions about the exact order of input required by openssl for this particular task.  If you want error checking, use expect or perl's Expect.pm or python's pexpect.