1. 声明 expect #!/usr/bin/expect -f
2. 设置超时时间, 获取参数
- set ip [lindex $argv 0 ] // 接收第一个参数, 并设置 IP
- set password [lindex $argv 1 ] // 接收第二个参数, 并设置密码
- set timeout 10 // 设置超时时间, 单位为秒
3. 执行 SSH 命令
spawn SSH $user@$ip
4. 终端输出包含字符串 "password:", 输入密码, 记得加上 \ r
expect "password:"{send "$pwd\r"}
5. 终端输出分支
- expect{
- "yes/no"{send "yes\r"; exp_continue}
- "password:"{send "$pwd\r"}
- }
6. 设置终端输出终止符, 并退出脚本
expect eof exit
来源: http://www.bubuko.com/infodetail-2867305.html