expect命令自动登录ssh

时间:2022-05-09 19:19:30

expect是简单的工具原因,依赖于tcl。

直接apt安装就行。

四个关键字:

spawn,派生出新进程。

expect,期待得到的字符串,可以模式匹配。

send,向进程发送字符串。

interact,进入交互模式。

下面是连接ssh例子:

#! /usr/bin/expect
spawn ssh root@192.168.10.31
expect {
"*yes/no" {
send "yes\r";
expect "*assword:"
send "123456\r"
}
"*assword:" {send "123456\r"}
}
expect "*#"
interact