2020年9月5日 作者 zeroheart

expect实现登录ssh

sudo apt-get install tcl tk expect

一般情况,我们还是使用sshKey来免密登录,今天试试expect,自动交互登录。

脚本如下:

#!/usr/bin/expect
set timeout 30
spawn ssh -p [lindex $argv 0] [lindex $argv 1]@[lindex $argv 2]
expect {
        "(yes/no)?"
        {send "yes\n";exp_continue}
        "password:"
        {send "[lindex $argv 3]\n"}
}
interact
运行密令:
/usr/local/bin/gss.sh port username ip 'pass'