#!/usr/bin/expect set timeout 60 spawn ssh -i xx.pem user@59.151.78.78 -p 1715 expect { "connecting (yes/no)?" { send "yes "; } "passphrase for key" { send "password " } } interact
执行方法 sudo ~/skip/ssh-login.sh
#!/usr/bin/expect -f set ip [lindex $argv 0 ] //接收第一个参数,并设置IP set password [lindex $argv 1 ] //接收第二个参数,并设置密码 set timeout 10 //设置超时时间 spawn ssh root@$ip //发送ssh请滶 expect { //返回信息匹配 "*yes/no" { send "yes "; exp_continue} //第一次ssh连接会提示yes/no,继续 "*password:" { send "$password " } //出现密码提示,发送密码 } interact //交互模式,用户会停留在远程服务器上面.
参考文章:https://www.cnblogs.com/lixigang/articles/4849527.html