免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9229 | 回复: 3
打印 上一主题 下一主题

[分享]自动建立ssh信任脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-30 20:50 |只看该作者 |倒序浏览
本帖最后由 where27 于 2011-11-30 20:55 编辑

在工作中经常遇到给两台主机建立ssh信任,手动建立太费事了,索性胡乱写了个脚本ssh_trust.sh来自动建立信任:
  1. #!/bin/bash
  2. src_host=$1
  3. src_username=$2
  4. src_passwd=$3

  5. dst_host=$4
  6. dst_username=$5
  7. dst_passwd=$6

  8. #在远程主机1上生成公私钥对
  9. Keygen()
  10. {
  11. expect << EOF

  12. spawn ssh $src_username@$src_host ssh-keygen -t rsa
  13. while 1 {

  14.         expect {
  15.                         "password:" {
  16.                                         send "$src_passwd\n"
  17.                         }
  18.                         "yes/no*" {
  19.                                         send "yes\n"
  20.                         }
  21.                         "Enter file in which to save the key*" {
  22.                                         send "\n"
  23.                         }
  24.                         "Enter passphrase*" {
  25.                                         send "\n"
  26.                         }
  27.                         "Enter same passphrase again:" {
  28.                                         send "\n"
  29.                                         }

  30.                         "Overwrite (y/n)" {
  31.                                         send "n\n"
  32.                         }
  33.                         eof {
  34.                                    exit
  35.                         }

  36.         }
  37. }
  38. EOF
  39. }

  40. #从远程主机1获取公钥保存到本地
  41. Get_pub()
  42. {
  43. expect << EOF

  44. spawn scp $src_username@$src_host:~/.ssh/id_rsa.pub /tmp
  45. expect {
  46.              "password:" {
  47.                             send "$src_passwd\n";exp_continue
  48.                 }
  49.                 "yes/no*" {
  50.                             send "yes\n";exp_continue
  51.                 }   
  52.                 eof {
  53.                                 exit
  54.                 }
  55. }
  56. EOF
  57. }
  58. #将公钥的内容附加到远程主机2的authorized_keys
  59. Put_pub()
  60. {
  61. src_pub="$(cat /tmp/id_rsa.pub)"
  62. expect << EOF
  63. spawn ssh $dst_username@$dst_host "chmod 700 ~/.ssh;echo $src_pub >> ~/.ssh/authorized_keys;chmod 600 ~/.ssh/authorized_ke
  64. ys"
  65. expect {
  66.             "password:" {
  67.                         send "$dst_passwd\n";exp_continue
  68.              }
  69.             "yes/no*" {
  70.                         send "yes\n";exp_continue
  71.              }   
  72.             eof {
  73.                         exit
  74.              }
  75. }
  76. EOF
  77. }
  78. Keygen
  79. Get_pub
  80. Put_pub
复制代码
脚本主要由3个expect组成,比较简单,用法是
  1. ./ssh_trust.sh host1 user1 passwd1 host2 user2 passwd2
复制代码
即建立从user1@host1到user2@host2的ssh信任。
说明:
1、当然得安装expect
2、脚本放在第三方机器(能远程登录host1和host2)上运行即可,当然放在host1和host2上运行也行。
3、如果想批量建立信任,可以编辑一个文件夹file如:
  1. host1 user1 passwd1 host2 user2 passwd2
  2. host3 user3 passwd3 host4 user4 passwd4
  3. host5 user5 passwd5 host6 user6 passwd6
复制代码
使用下面命令执行脚本即可:
  1. xargs -n6 ./ssh_trust.sh < file
复制代码
4、仓促写的,脚本只是简单实现功能,使用前确保参数的可用性(用户密码主机名),不然很容易报错
5、只在linux redhat上测试过,运行成功,欢迎大家提意见~~

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
2 [报告]
发表于 2011-11-30 21:09 |只看该作者
expect + ssh 自动建立双机信任关系
http://blog.chinaunix.net/space. ... blog&id=2759648

论坛徽章:
13
双鱼座
日期:2013-10-23 09:30:05数据库技术版块每日发帖之星
日期:2016-04-20 06:20:00程序设计版块每日发帖之星
日期:2016-03-09 06:20:002015亚冠之塔什干火车头
日期:2015-11-02 10:07:452015亚冠之德黑兰石油
日期:2015-08-30 10:07:07数据库技术版块每日发帖之星
日期:2015-08-28 06:20:00数据库技术版块每日发帖之星
日期:2015-08-05 06:20:002015年迎新春徽章
日期:2015-03-04 09:57:09辰龙
日期:2014-12-03 14:45:52酉鸡
日期:2014-07-23 09:46:23亥猪
日期:2014-03-13 08:46:22金牛座
日期:2014-02-11 09:36:21
3 [报告]
发表于 2011-12-01 09:14 |只看该作者
还不错{:2_168:}

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
4 [报告]
发表于 2011-12-01 11:41 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP