expect脚本实现通过跳板机登录多个机器做操作
如下:192.168.10.1是跳板机IP,192.168.10.2和192.168.10.3是我们需要操作的服务器。
(需要在跳板机实现自动登录的脚本autologin)
#!/usr/bin/expect set ipaddr "192.168.10.1" set password "test" set timeout -1 spawn ssh root@$ipaddr -p 22 "autologin 192.168.10.2" expect "*?\(yes/no\)*" { send -- "yes\r" expect "*?assword:*" send -- "$password\r" } "*?assword:*" { send -- "$password\r" } expect "Last login:" { send -- "./your_command \r" } expect "command finished" { send -- "exit\r" send -- "exit\r" } spawn ssh root@$ipaddr -p 22 "autologin 192.168.10.3" expect "*?\(yes/no\)*" { send -- "yes\r" expect "*?assword:*" send -- "$password\r" } "*?assword:*" { send -- "$password\r" } expect "Last login:" { send -- "./your_command \r" } expect "command finished" { send -- "exit\r" send -- "exit\r" } interact
autologin脚本也可以通过expect实现