使用expect脚本自动进行ssh登录
- 参考:
-
安装 expect 和 openssh
-
编写 ssh-pass 脚本,放入 $PATH 的搜索范围
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#!/usr/bin/expect #Usage ssh-pass <host> <ssh user> <ssh password> set timeout 60 set username [lindex $argv 1] set password [lindex $argv 2] set hostname [lindex $argv 0] log_user 0 if {[llength $argv] == 0} { send_user "Usage: ssh-pass hostname username \'password\'\n" exit 1 } send_user "\n#####\n# $hostname\n#####\n" spawn ssh -q -oStrictHostKeyChecking=no -oCheckHostIP=no $username@$hostname expect { timeout { send_user "\nFailed to get password prompt\n"; exit 1 } eof { send_user "\nSSH failure for $hostname\n"; exit 1 } "*?assword" } send "$password\r" interact
-
在 bash 中创建快捷函数
1 2 3
function sshtest() { ssh-pass 192.168.100.$1 root your-pass }