0

Heres the thread I'm bouncing off of: Inputting input in a command?

So I'm trying to run this command: cd /etc/openvpn/easy-rsa; . ./vars; ./build-key username

However, after running that command, I need to input nothing [just press enter] 10 times, and press y twice. The user on the last thread suggested I use except, but I couldn't get it working. Here are the questions it asks:

root@suffice-vpn:/etc/openvpn/easy-rsa# ./build-key usernamtest
Generating a 2048 bit RSA private key
..................+++
........+++
writing new private key to 'usernamtest.key'
-----
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.
-----
Country Name (2 letter code) [US]:
State or Province Name (full name) [NY]:
Locality Name (eg, city) [Merrick]:
Organization Name (eg, company) [IceWare]:
Organizational Unit Name (eg, section) [IceWare]:
Common Name (eg, your name or your server's hostname) [usernamtest]:
Name [server]:
Email Address [iceware@programmer.net]:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'US'
stateOrProvinceName   :PRINTABLE:'NY'
localityName          :PRINTABLE:'Merrick'
organizationName      :PRINTABLE:'IceWare'
organizationalUnitName:PRINTABLE:'IceWare'
commonName            :PRINTABLE:'usernamtest'
name                  :PRINTABLE:'server'
emailAddress          :IA5STRING:'iceware@programmer.net'
Certificate is to be certified until Jan 14 18:27:48 2026 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y

As you can see, I press enter 8 times initially [to keep the default input] and then press enter another 2 times to skip the optional commands. Then, I need to press y twice. How can I automate this? I need to have this all happen in one command. Any suggestions? Thanks in advance.

1
  • I'm confused. This seems to be an exact duplicate of your earlier question and yet you say you solved it.. Why are you asking again?
    – Seth
    Jan 21 '16 at 0:18
1

Here's a quick'n'dirty expect script that you can try (I can't test it myself, since I'm not using openvpn anywhere):

#!/usr/bin/expect

set timeout 20
set username [lindex $argv 0]

spawn ./build-key $username

while 1 { 
  expect { 
    "y/n]" { send "y\r" }
    "]:" { send "\r" }
    eof { break } 
  } 
}

It basically sends y whenever it sees a y/n] question and just a newline whenever it sees any other closing square bracket ], until it runs out of input.

You can save it as whatever, make it executable, and then execute

./whatever usernametest
1

I found an easier alternative:

cd /etc/openvpn/easy-rsa; . ./vars; ./build-key --batch username

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.