0

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"
executable
  • 3,365
  • 6
  • 24
  • 52
  • 2
    Your shebang is `#!/usr/bin/expect -f`. So the command in the script will not be interpreted by `bash`, but by `expect` when you run it with `./`. – Aserre Jan 09 '19 at 15:54
  • Possible duplicate of [How does the #! shebang work?](https://stackoverflow.com/questions/3009192/how-does-the-shebang-work) – Aserre Jan 09 '19 at 15:55

1 Answers1

3

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
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432