`
Goldice
  • 浏览: 104708 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

分享一个ssh打通的脚本

阅读更多

分享一个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}
}
1
2
分享到:
评论
2 楼 Goldice 2011-10-15  
fuliang 写道
直接ssh-copy-id不就ok了?

是的。你的整个命令更简单。我以前没用过,呵呵。
1 楼 fuliang 2011-10-14  
直接ssh-copy-id不就ok了?

相关推荐

Global site tag (gtag.js) - Google Analytics