六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 26|回复: 0

分享一个ssh打通的脚本

[复制链接]

升级  82.67%

52

主题

52

主题

52

主题

秀才

Rank: 2

积分
174
 楼主| 发表于 2013-2-4 13:29:15 | 显示全部楼层 |阅读模式
分享一个ssh打通的脚本,经过测试可用。目前只能单向打通,且要求本地用户名为admin(写入代码,可简单修改)。本身只是个人使用,故通用性、异常情况考虑不多,大家可以做个参考。
补充一点,Important Tip:
authorized_keys文件的权限很重要,如果设置为777,那么登录的时候,还是需要提供密码的 :
chmod  400 authorized_keys
 
 
#!/usr/local/bin/expect -fset 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.pubexpect {"*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}}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表