I have installed expect command on CentOS 7 with yum install expect -y
. I want to automate some input in my script but it seems like it doesn't interpret in bash anymore.
Here is my script :
#!/usr/bin/expect -f
homeDir="/home"
if [ $# -eq 1 ]; then
CLIENT="$1"
./easyrsa build-client-full "$CLIENT" nopass
elif [ $# -eq 2 ]; then
CLIENT="$1"
password="$2"
set timeout -1
spawn ./easyrsa build-client-full "$CLIENT"
expect "Enter PEM pass phrase:"
send -- "$password"
expect eof
else
echo "script <username> <password (optional)>"
fi
I made the script executable with chmod +x script
and run it like ./script
. The error I get :
script: line11: spawn : command not found couldn't read file "Enter PEM pass phrase:": no such file or directory script: ligne13: send : command not found couldn't read file "eof": no such file or directory
If I make whereis expect
I get :
expect: /usr/bin/expect /usr/share/man/man1/expect.1.gz
I would like to ask if there is some alternative without using another lib ?
I also tried using this line of code but this doesn't give any return :
echo "$password" | ./easyrsa build-client-full "$CLIENT"
You basically want an expect
script inside a bash
script something like this:
#!/bin/bash
homeDir="/home"
if [ $# -eq 1 ]; then
CLIENT="$1"
./easyrsa build-client-full "$CLIENT" nopass
elif [ $# -eq 2 ]; then
CLIENT="$1"
password="$2"
/usr/bin/expect <<EOF
...
DO EXPECT STUFF
...
EOF
else
echo "script <username> <password (optional)>"
fi
The following is an example using sexpect (Expect for Shells) . Just FYI.
#!/bin/bash
homeDir="/home"
if [ $# -eq 1 ]; then
CLIENT="$1"
./easyrsa build-client-full "$CLIENT" nopass
elif [ $# -eq 2 ]; then
CLIENT="$1"
password="$2"
export SEXPECT_SOCKFILE=/tmp/sexpect.tmp.$$.sock
sexpect spawn ./easyrsa build-client-full "$CLIENT"
sexpect expect "Enter PEM pass phrase:"
sexpect send -enter "$password"
sexpect expect -eof
sexpect wait
else
echo "script <username> <password (optional)>"
fi
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.