Automatically enter input in command line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automatically enter input in command line
# 1  
Old 10-26-2016
Automatically enter input in command line

Hi,

This is a script which to create an opvn user, I want which answer automatically to a certain part so, I try this, it works without the red part but I must type manually.. :
Code:
#!/bin/bash

## Environnement ##
LC_ALL=C

## Paths ##
rsa_dir="etc/openvpn/easy-rsa"
rsa_key_dir="etc/openvpn/easy-rsa/keys"
user_dir="etc/openvpn/users"
ccd_dir="etc/openvpn/ccd"

## Regex filter ##
regex_filter='[^a-zA-Z_0-9\s]'

## Position ##
cd /"$rsa_dir"/

## Loop ##
while
        read -p "Please can you enter the vpn's username : " username
        [[ -z "$username" ]] || [[ ${#username} -lt 2 ]] || [[ ${#username} -gt 15 ]] || [[ "$username" =~ $regex_filter ]]
do
        echo "Your entry must not contain special characters and its length must do between 2-15 characters."
done

## Commands ##
. ./vars
./build-key "$username" << EOF
(14 empty lines feed)
y
y
EOF

cp -p /"$rsa_key_dir"/"$username".* /"$user_dir"/
cd /"$user_dir"/; cp template.ovpn "$username".ovpn
sed -i 's/name/'$username'/g' "$username".ovpn
tar -czvf "$username".tar.gz "$username".* ta.key ca.crt
touch ../ccd/"$username"
cp -a "$username".tar.gz /tmp/
scp "$username".* usersend@1.1.1.1:/tmp/.

Ouput :
Code:
$./user_ovpn.sh
Please can you enter the vpn's username : iki
NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
Generating a 2048 bit RSA private key
........................................................................+++
....+++
writing new private key to 'iki.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) [FR]:State or Province Name (full name) [FR]:Locality Name (eg, city) [hello]:
Organization Name (eg, company) [Hello]:Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [iki]:Name [HELLO_VPN]:Email Address [hello@machin.com]:
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:'FR'
stateOrProvinceName   :PRINTABLE:'FR'
localityName          :PRINTABLE:'Hello'
organizationName      :PRINTABLE:'Hello'
commonName            :PRINTABLE:'Hello'
name                  :T61STRING:'Hello'
emailAddress          :IA5STRING:'Hello@machin.com'
Certificate is to be certified until Oct 13 06:01:44 2026 GMT (3650 days)
Sign the certificate? [y/n]:CERTIFICATE WILL NOT BE CERTIFIED: I/O error
iki.crt
iki.csr
iki.key
iki.ovpn
ta.key
ca.crt

Example of output with manually entries :
Code:
$./vpn_user.sh
Please can you enter the vpn's username : testuser2
NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
Generating a 2048 bit RSA private key
............................................+++
............+++
writing new private key to 'testuser2.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) [FR]:
State or Province Name (full name) [FR]:
Locality Name (eg, city) [Hello]:
Organization Name (eg, company) [hello]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) [testuser2]:
Name [HELLO_VPN]:
Email Address [hello@machin.com]:

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:'FR'
stateOrProvinceName   :PRINTABLE:'FR'
localityName          :PRINTABLE:'Hello'
organizationName      :PRINTABLE:'Hello'
commonName            :PRINTABLE:'testuser2'
name                  :T61STRING:'HELLO_VPN'
emailAddress          :IA5STRING:'hello@machin.com'
Certificate is to be certified until Oct 13 05:45:45 2026 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
testuser2.crt
testuser2.csr
testuser2.key
testuser2.ovpn
ta.key
ca.crt

In red that represent the manually entries that I want which works automatically
Thanks in advance.. SmilieSmilie

Last edited by Arnaudh78; 10-26-2016 at 04:09 PM..
# 2  
Old 10-26-2016
I counted 7 lines to be input with default values BEFORE the first 'y':
Code:
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'FR'
stateOrProvinceName   :PRINTABLE:'FR'
localityName          :PRINTABLE:'Hello'
organizationName      :PRINTABLE:'Hello'
commonName            :PRINTABLE:'testuser2'
name                  :T61STRING:'HELLO_VPN'
emailAddress          :IA5STRING:'hello@machin.com'
Certificate is to be certified until Oct 13 05:45:45 2026 GMT (3650 days)
Sign the certificate? [y/n]:y

And you have (14 empty lines feed) in your here-doc?
# 3  
Old 10-26-2016
That's the same I try :
Code:
./build-key "$username" << EOF
7 lines feed
y
2 lines feed
y

# 4  
Old 10-26-2016
Quote:
Originally Posted by Arnaudh78
That's the same I try :
Code:
./build-key "$username" << EOF
7 lines feed
y
2 lines feed
y

I'd suggest doing it manually counting how many time you need to hit Enter and where. Then change you here-doc accordingly...
# 5  
Old 10-26-2016
I have tested many time with different solution but same.. there is no another way ?
# 6  
Old 10-26-2016
Quote:
Originally Posted by Arnaudh78
I have tested many time with different solution but same.. there is no another way ?
It depends on whether ./build-key requires a controlling terminal, If so, you cannot use the here-doc.

Others may suggest other alternatives/ideas.
# 7  
Old 10-26-2016
Yes ./build-key requires a controlling terminal Smilie

---------- Post updated at 04:54 PM ---------- Previous update was at 04:19 PM ----------

have you got an idea?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect command to send the user input enter or ctrl+c

Hey All, I am writing one script using expect, that script which is used in spawn will accepts only 1. Enter 2. Ctrl+c Press Control-C to exit, Enter to proceed. Could some one share some thoughts to send the above user inputs in linux expect block ? Thanks, Sam (0 Replies)
Discussion started by: SCHITIMA
0 Replies

2. Shell Programming and Scripting

How to automatically enter password in a script?

Hi I'm working with AIX 6.1 I would like to ssh to a server without entering password ( to monitor something) but there's no way to do that by authentication keys, so I need to write a script which can ssh to that server without entering password ( no need to hide passsword in the script, just an... (9 Replies)
Discussion started by: bobochacha29
9 Replies

3. Shell Programming and Scripting

Loop logic, enter into respective IF as per enter input file name

have three big data file, however I just need to see the mentioned below one line form the all the file which has SERVER_CONNECTION Value File 1 export SERVER_CONNECTION=//dvlna002:10001/SmartServer File2 export SERVER_CONNECTION=///SmartServer File3 export... (1 Reply)
Discussion started by: Nsharma3006
1 Replies

4. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

5. Shell Programming and Scripting

Enter an input and reference another line in ksh script

Hi I have a file like so: Code: Frank Peter Tony Robert Mike 1 2 3 4 5 5 4 2 3 1 4 3 1 5 2 My out should look like this: Peter Tony Mike and so on.... I have the first part done to ask the user to... (8 Replies)
Discussion started by: bombcan1
8 Replies

6. Shell Programming and Scripting

using read to enter the input at runtime

Hi I am stucked in the below script .I want to input with yes/no from the user and then execute the code inside if but it is not working .I just need the logic as where I am wrong so that i can use the same in my work . then echo "Hi All" fi ]. Please suugest . (4 Replies)
Discussion started by: mani_isha
4 Replies

7. Shell Programming and Scripting

Script to automatically enter a password

I need to retrieve thousands of lines of information from hundreds of nodes. Each node requires a passowrd in order to retrieve the information. Fortunately, the password is the same for each one of them. So I am trying to come up with a script that would allow me to include the password so I can... (0 Replies)
Discussion started by: Ernst
0 Replies

8. UNIX for Dummies Questions & Answers

how to enter hardcoded password automatically

In the script i am passing a command from script which is called from cron. When this command is called from cron the unix prompt asks for password. Can we automatically enter the password when promted(if the password is hardcoded in script)?? Please let me know how to enter the password... (4 Replies)
Discussion started by: abhi_n123
4 Replies

9. Shell Programming and Scripting

How to enter a password in the script automatically when prompted?

Hi Friends, We need to create a script which will invoke a command with diffrent parameters. The command invoked needs the password. So how automatically we can enter password in the script to the command? example.: #!/bin/ksh for par in `cat parfile` do # Here is the main command... (1 Reply)
Discussion started by: sourabhsharma
1 Replies

10. Shell Programming and Scripting

How to enter if-then condition on command prompt/line ?

Hi I have some trouble entering if-then condition in a single line on a command prompt in csh. Could someone show how does one do that ? eg: source .cshrc; cd $dir; pwd; test -d $backup_dir; if then mkdir -p ${backup_dir}; echo inside loop; fi; echo outside loop; mv -f... (3 Replies)
Discussion started by: mpc8250
3 Replies
Login or Register to Ask a Question