分享一个ssh打通的脚本,经过测试可用。目前只能单向打通,且要求本地用户名为admin(写入代码,可简单修改)。本身只是个人使用,故通用性、异常情况考虑不多,大家可以做个参考。
补充一点,Important Tip:
authorized_keys文件的权限很重要,如果设置为777,那么登录的时候,还是需要提供密码的 :
chmod 400 authorized_keys
#!/usr/local/bin/expect -f set user [lindex $argv 0] set ipaddr [lindex $argv 1] set passwd [lindex $argv 2] spawn ssh-keygen -t rsa expect { "*save the key" {send "\r";exp_continue} "Overwrite (y/n)?" {send "\r";exp_continue} } spawn ssh $user@$ipaddr "mkdir /home/$user/.ssh" expect { "*password:" {send "$passwd\r";exp_continue} } spawn scp /home/admin/.ssh/id_rsa.pub $user@$ipaddr:/home/$user/.ssh/id_rsa.pub expect { "*password:" {send "$passwd\r";exp_continue} } spawn ssh $user@$ipaddr "touch /home/$user/.ssh/authorized_keys" expect { "*password:" {send "$passwd\r";exp_continue} } spawn ssh $user@$ipaddr "cat /home/$user/.ssh/id_rsa.pub >> /home/$user/.ssh/authorized_keys" expect { "*password:" {send "$passwd\r";exp_continue} }