再来看一个登陆后,执行命令然后退出的脚本:


#!/usr/bin/expect

set user "root"

set passwd "123456"

spawn ssh $user@192.168.11.18

expect {

"yes/no" { send "yes\r"; exp_continue}

"password:" { send "$passwd\r" }

}                                      #以上和上一个脚本相同

expect "]*"                            

send "touch /tmp/12.txt\r"

expect "]*"

send "echo 1212 > /tmp/12.txt\r"

expect "]*"

send "exit\r"


注意:登陆远程机器后,命令行为[root@wyp ~]#  所以expect "]*"的意思是匹配"]"和*(任意一个字符),因为有的机器是#有的也可能是$或者其他。