I am writing a bash script with expect in it.
#!/bin/bash
IP1="a.b.c.d"
IP2="e.f.g.h"
HOST="xyz.com"
KEY="/path/to/key/file"
PORT="sshport"
/usr/bin/expect << EOD
spawn ssh -p $PORT -i $KEY $HOST
expect "*#"
send "sh somescript\r"
expect "Prompt from script:"
send "$IP1\r"
expect "Second Prompt from script"
send "$IP2\r"
interact
EOD
I am expecting to get the control back so that user can interact with the script after sending IP2 , but the script terminates, logs out of session from remote host. Any pointers please ?
Thank you Amit
You are not setting "expect" commands, but just sending to "expect"'s stdin.
You can use "-c" to specify expect command like below, or "-f" to specify command file.
/usr/bin/expect -c "
spawn ssh -p $PORT -i $KEY $HOST
expect \"*#\"
send \"sh somescript\r\"
expect \"Prompt from script:\"
send \"$IP1\r\"
expect \"Second Prompt from script\"
send \"$IP2\r\"
interact
"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With