shell的expect命令

场景一:

#!/bin/expect
spawn su root
expect "password: "
send "123456\r"
expect eof
exit
 
场景二:用于bash中简单的交互
expect "*password*" {send "$password\r"}

场景三:嵌入bash中的多个expect交互
while :
do
    /usr/bin/expect << EOF
    set timeout 30  # 超时参数,默认单位为秒;超过30s则跳过expect;timeout -1 永不超时
    spawn ssh root@192.168.2.195
    expect {                       # 一个命令需要进行多次交互
        "(yes/no)?" { send "yes\r"; exp_continue }    # exp_continue:附加在expect语句后,同个expect语句中已匹配一项后,可以继续匹配其他项,例:"password:"
        "password:" { send "123456\r" }       # '\r'一定要带
    }
    expect "*#"    # 登录节点后的在命令行上交互
    send "touch test.sh\r"
    expect "*#"
    send "exit\r"
    interact      # 执行完成后保持交互状态,可以手工操作。如果没有该命令,命令完成后即退出。
EOF
    sleep 5m
done
posted @ 2019-07-23 22:50  羽惑飞  阅读(398)  评论(0)    收藏  举报