expect 实现iterm2自动加载pem登录跳板机

#!/usr/bin/expect

set timeout
spawn ssh -i xx.pem user@59.151.78.78 -p
expect {
"connecting (yes/no)?" { send "yes\r"; }
"passphrase for key" { send "password\r" }
}
interact

执行方法 sudo ~/skip/ssh-login.sh

#!/usr/bin/expect -f
set ip [lindex $argv ] //接收第一个参数,并设置IP
set password [lindex $argv ] //接收第二个参数,并设置密码
set timeout //设置超时时间
spawn ssh root@$ip //发送ssh请滶
expect { //返回信息匹配
"*yes/no" { send "yes\r"; exp_continue} //第一次ssh连接会提示yes/no,继续
"*password:" { send "$password\r" } //出现密码提示,发送密码
}
interact //交互模式,用户会停留在远程服务器上面.

参考文章:https://www.cnblogs.com/lixigang/articles/4849527.html

上一篇:ubuntu14.04中mysql的安裝及utf8编码集配置


下一篇:C#高级编程第9版 阅读笔记(一)