Bash pass typed yes to EasyRSA gen-req

Viewed 1161

Problem: I'm trying to script an EasyRSA key gen request, but, if the key file already exists, any attempt to overwrite using yes or echo yes commands result in Aborting without confirmation. I believe EasyRSA is requiring me to actually type 'yes' and anything else results in an abort.

I'm wondering if there's another way to pass 'yes' to a prompt that requires a typed response?

What I've tried: yes | <command>, yes yes | <command> and echo yes | <command>

2 Answers

You can do two things. First one might not work but second would.

First: You can execute easyrsa by

pi@raspberry$ sudo easyrsa <some command> -y

The -y will force it to do yes. You can alternatively use -yes or -f arguments too. If this doesn't work, try the second method.

pi@raspberry$ sudo ./script.sh

The script will contain this:

cat << EOF | sudo easyrsa <some command>
yes
EOF

This will definitely work. Glad to help :)

This ain't all of it (?), but a part that works creating new key or whether to overwrite existing key or not ,

#client common name - a string
CN=$1

# 'yes' or 'no' to overwrite or not resp.
OVERWRITE=$2 

#paths
EASYRSA_DIR="/path/to/easy-rsa/easyrsa3"
EASYRSA="./easyrsa"

if [ -f "$EASYRSA_DIR/pki/private/$CN.key" ]; then         
    cat <<-EOF  | sudo $EASYRSA gen-req $CN nopass         
    $OVERWRITE
    $CN
    EOF
else
    cat <<-EOF  | sudo $EASYRSA gen-req $CN nopass         
    $CN   
    EOF
fi

then run the script with further commands to satisfy easy-rsa! Expected output:

Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa/easyrsa3/vars
Using SSL: openssl OpenSSL 1.1.1h  22 Sep 2020


WARNING!!!

An existing private key was found at /etc/openvpn/easy-rsa/easyrsa3/pki/private/testuser.key
Continuing with key generation will replace this key.

Type the word 'yes' to continue, or any other input to abort.
  Confirm key overwrite: Generating a RSA private key
............................+++++
.....................+++++
writing new private key to '/etc/openvpn/easy-rsa/easyrsa3/pki/easy-rsa-12805.EocLO7/tmp.CmRehr'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [testuser]:
Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/easyrsa3/pki/reqs/testuser.req
key: /etc/openvpn/easy-rsa/easyrsa3/pki/private/testuser.key
Related