Expect : SSH login

Syntax

# assign variables
set variable_name variable_value
# do bash
spawn .....
# expect
expect ...
# send
send "..."
# interact
interact

Examples

#!/usr/bin/expect -f
set ipaddr "localhost"
set passwd "iforgot"
spawn ssh root@$ipaddr
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]# "
send "touch a.txt\r" 
send "exit\r"
expect eof
exit


References

  1. how to use regex in switch case in expect script?
  2. Embedding an expect inside a bash script
  3. How do I embed an expect script that takes in arguments into a bash shell script?
  4. 6 Expect Script Command Line Argument Examples

推荐阅读更多精彩内容