以下expect脚本是用root用户登录某server并执行touch test.txt
#!/usr/bin/expect -f
#Filename:login.exp
#write by zhangll
set ip [lindex $argv 0 ]
set password [lindex $argv 1 ]
set timeout 50
spawn ssh root@$ip
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" { send "$password\r" }
}
expect "]*"
send "touch test.txt\r"
send "exit\r"
expect eof
执行:
chmod +x login.exp
[......]