前言

此脚本虽然是python脚本,但是里面调用了太多os.system命令(囧,哥的python太水了),只要懂shell脚本,就可用shell来完成自动配置ssh互信脚本。为何当初没有使用except,因为本公司的centos中没有携带except安装包,centos还是精简版,很多依赖包都没有。我不想太折腾。

我会一点python,使用python是理所当然的。原计划用python paramiko模块来实现密码放文件里登陆,但是paramiko安装编译需要gcc环境。精简的centos也没有,弄了半天,几十个依赖包弄得我头晕眼花,大小达到50M,不通用,懒得做了。

现在的这个脚本需要手动输入远程主机密码,不适用于成百上千台服务器部署ssh互信。在网上找了很久,确认ssh 不支持脚本输入密码。

由于我的业务上经常也就同时做10台以内的ssh互信,这个脚本就满足需求了。

ssh配置互信重点

1、不交互自动生成密钥文件

用ssh-keygen 的-f和-P参数,生成密钥不需要交互。如下:

rm -rf ~/.ssh/

/usr/bin/ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''

cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys

2、自动添加/etc/hosts中主机名与IP对应关系,并推送到远端主机

这个简单,手动配置或脚本配置都行。互信成功后,用脚本批量推送就行了。

3、推送密钥文件至远端主机

最简单的方法:scp  -r /root/.ssh root@hostb:/root

但是咱公司精简的centos没有关闭selinux。拷贝过去还是不能无密码登陆(不信可自己测试)。如果采用输入两次密码,第一次登陆上去关闭selinux,第二次拷贝/root/.ssh过去也可以(不想梅开二度)。我采用的是另一种方法,只需输入一次密码即可。怎么实现的?见代码。

第一次登陆交互需要输入yes获取对端公钥

.ssh文件拷贝过去后,第一次登陆会让你输入yes接收公钥,而且,不止自己登陆对方要输入yes,每一台登陆远端主机都要输入yes,如果一共10台服务器,每台输入9次,共需要输入81次。这个怎么破?见代码。

执行效果

[root@VueOS scripts]#python ssh_trust.py

Input node's info. Usage:hosta/192.168.0.101. Press Enter is complete.

Please input Node info: nodea/192.168.2.151

#获取节点信息,主机名与IP,生产中尽量使用主机名,方便以后环境迁移

check ip address success!

Input node's info. Usage:hosta/192.168.0.101. Press Enter is complete.

Please input Node info:  nodeb/192.168.2.158

check ip address success!

Input node's info. Usage:hosta/192.168.0.101. Press Enter is complete.

Please input Node info:

Are you sure you want  to set ssh mutual trust? (yes/no) yes

#是否要设置互信?因为本次互信是我部署其它程序的前提。互信一次就能配置成功,程序可能多次失败,不需要每次运行都做互信。

Generating public/private rsa key pair.

Created directory '/root/.ssh'.

Your identification has been saved in/root/.ssh/id_rsa.

Your public key has been saved in/root/.ssh/id_rsa.pub.

The key fingerprint is:

79:97:67:36:e5:db:0d:4c:8e:20:63:a2:59:f6:bf:17root@VueOS

The key's randomart image is:

+--[ RSA 2048]----+

|                 |

|                 |

|     + + .   . .|

|    = + + . * o |

|   o   S . + O .|

|        o .E+ o+|

|         .  . .o|

|          ..    |

|         ..     |

+-----------------+

#自动生成完密钥后,把密钥拷贝到其它节点

Scp ssh key file too another node...

The authenticity of host 'nodeb(192.168.2.158)' can't be established.

RSA key fingerprint isc6:2d:8e:e0:a1:e1:2e:c3:77:c7:1d:ef:92:7c:41:ba.

Are you sure you want to continueconnecting (yes/no)? yes

Warning: Permanently added'nodeb,192.168.2.158' (RSA) to the list of known hosts.

root@nodeb's password:

#输入远端节点密码

copy ssh file successful

Done

Warning: Permanently added'nodea,192.168.2.151' (RSA) to the list of known hosts.

local nodea RsaKey remote nodea issuccessful

local nodea RsaKey remote nodeb issuccessful

known_hosts                                                                                      100%  802     0.8KB/s  00:00

hosts                                                                                             100%   84    0.1KB/s   00:00

[root@VueOS scripts]#ssh nodeb

Last login: Fri Apr  4 15:34:06 2014 from 192.168.2.72

[root@VueOS ~]#exit

logout

Connection to nodeb closed.

[root@VueOS scripts]# ssh nodea

Last login: Fri Apr  4 11:55:58 2014 from hosta

[root@VueOS ~]# exit

logout

Connection to nodea closed.

#如上所示,互信自动配置成功。

脚本代码

[root@VueOS scripts]# cat ssh_trust.py
#!/usr/bin/python
import datetime
import os,sys,time
import commands
#这个函数是校验IP合法性的。
def check_ip(ipaddr):import sysaddr=ipaddr.strip().split('.')#print addrif len(addr) != 4:print "check ip address failed!"sys.exit()for i in range(4):try:addr[i]=int(addr[i])except:sys.exit()if addr[i]<=255 and addr[i]>=0:passelse:print "check ip address failed!"sys.exit()i+=1else:print "check ip address success!"
node_dict={}
host_all=[]
host_ip_all={}
#这个函数获取输入的节点信息,并校验输入的IP是否合法。
def get_nodes():while True:node=raw_input("""Input node's info. Usage: hosta/192.168.0.101. Press Enter is complete.
Please input Node info:  """)if len(node)==0:return 2node_result=node.strip().split('/')host_ip_all[node_result[0]]=[node_result[1],'']#print node_resultif len(node_result[0])==0:print "Hostname is failed!"sys.exit()check_ip(node_result[1])#node_dict[node_result[0]]=[node_result[1]]host_all.append(node_result[0])#print node_dictlocal_ip_status,local_ip_result=commands.getstatusoutput("""ifconfig |grep 'inet addr'|awk -F '[: ]+' '{print $4}' """)local_ip=local_ip_result.split('\n')#print host_allif len(host_all)==1:if node_result[1] in local_ip:passelse:print "The first IP must be native IP."sys.exit()
#这个函数生成ssh密钥文件,简单的3条shell命令
def create_sshkey_file():os.system("rm -rf ~/.ssh/")os.system("/usr/bin/ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''    ")os.system("cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys")
#这个函数配置/etc/hosts文件
def add_etc_hosts():for i in host_ip_all.keys():#print host_ip_all.keys()#判断/etc/hosts是否存在if os.path.exists('/etc/hosts'):passelse:os.system("touch /etc/hosts")#删除掉原有主机名项,并添加新的对应关系os.system("sed -i '/%s/d' /etc/hosts"%i)#print i,host_ip_all[i][0]os.system(''' echo '%s %s\r' >>/etc/hosts '''%(host_ip_all[i][0],i))#with open('/etc/hosts','ab') as f :#       f.write('%s  %s\r'%(host_ip_all[i][0],i))
#拷贝ssh密钥文件
def scp_sshkey():#把/root/.ssh文件放在/tmp下,并开使其生效。os.system("sed -i /tmp/d /etc/exports")os.system("echo '/tmp *(rw)' >>/etc/exports")os.system("exportfs -r")os.system("cp -a /root/.ssh /tmp")os.system("chmod 777 /tmp")os.system("/etc/init.d/iptables stop")os.system("chmod 777 /tmp/.ssh/id_rsa")os.system("chmod 777 /tmp/.ssh")print 'Scp ssh key file too another node...'print host_all[1:]localhost=host_all[0]local_ip=host_ip_all[localhost][0]#输入一次密码,登陆上去后执行了N条命令,挂载nfs,拷贝.ssh,修改权限,卸载等等。for i in host_all[1:]:os.system('''/usr/bin/ssh %s "/bin/umount -lf /mnt >/dev/null 2 &>1;/bin/mount %s:/tmp /mnt ;rm -rf /root/.ssh;mkdir /root/.ssh;sleep 3; /bin/cp -a /mnt/.ssh/* /root/.ssh/ ;echo 'copy ssh file successful';/bin/chmod 600 /root/.ssh/id_rsa;/bin/chmod 700 /root/.ssh;/bin/umount -lf /mnt "'''%(i,local_ip))print "Done"
#测试ssh互信是否成功
def test_sshkey(host):for host_n in host_all:try:#to gain public key#使用StrictHostKeyChecking跳过公钥接收提示。并远程执行命令,连上了自然会输出成功信息。os.system(''' ssh  -o  "StrictHostKeyChecking no" %s "echo -e local %s RsaKey remote %s  is successful" '''%(host_n,host_all[0],host_n))except:print "\033[31mlocal %s RsaKey remote %s is fail.\033[0m"%(host_all[0],host_n)
else:#全部测试完成后,把known_hosts文件拷贝到所有主机。一台服务器获取了所有公钥,那拷贝到远端自然是所有节点都有对方公钥了。for host_m in host_all[1:]:#copy ~/.ssh/known_hosts file to another host...os.system("""/usr/bin/scp ~/.ssh/known_hosts %s:~/.ssh/known_hosts"""%host_m)# copy hosts file to another host...os.system("""/usr/bin/scp /etc/hosts %s:/etc/hosts """%host_m)
#函数执行部分
get_nodes()
a=raw_input("Are you sure you want  to set ssh mutual trust? (yes/no) ").strip()
#提示一下,做不做互信要让用户选择。
if a=="yes":create_sshkey_file()add_etc_hosts()scp_sshkey()test_sshkey('192.168.2.73')
elif a=="no":
#输入no就代表互信已做好了。接下来执行别的代码。pass
else:print 'Byebye,Please input yes or no.'sys.exit()

脚本自动配置ssh互信相关推荐

  1. 批处理bat脚本自动配置java的jdk环境变量

    前言 每当更换电脑或者是重装系统之后,都需要重新配置java系统路径.但是又不想每次都去查配置方法,所以写了个脚本自动配置. 脚本内容 @echo off @echo 第一步 输入要设置的JAVA_H ...

  2. 三个机器配置SSH互信

    --配置三个机器的SSH互信 1.首先确保三个机器能相互ping通 2.三台机器分别执行Keygen命令 //rsa可用dsa替换 //最好不要对root用户配置互信 ssh-keygen -t rs ...

  3. java代码使用.pac脚本自动配置代理服务器策略

    PAC是什么? 一个PAC文件包含一个JavaScript形式的函数"FindProxyForURL(url, host)".这个函数返回一个包含一个或多个访问规则的字符串.用户代 ...

  4. Linux之间配置SSH互信(SSH免密码登录)

    为简化SSH过程,采用证书方式,免去SSH登入时需要输入账号密码的过程,具体操作如下: 一.在SSH服务器所在机器上 1.以root用户登录,更改ssh配置文件 /etc/ssh/sshd_confi ...

  5. lnmp环境脚本自动配置

    #!/usr/bin/env bash echo "=============START=====================" cd /home/ echo '[yum] 安 ...

  6. Linux 操作系统 Centos7.6安装教程详细Linux系统安装磁盘分区IP配置SSH互信NTP时间服务器关闭图形界面关闭防火墙配置hosts文件

    Centos7.6安装 系统安装版本说明 版本:CentOS7.3.1611 (64bit) CentOS-7-x86_64-DVD-1611.iso CentOS-7-x86_64-Everythi ...

  7. 配置Linux之间SSH互信连接

    配置Linux之间SSH互信连接 在多节点并行计算设置中,建立Linux主机间的SSH互信是非常重要的环节.常见的互信机制包括RSH和SSH两种,其中SSH互信较常用到,下面介绍SSH的原理及其配置方 ...

  8. Centos7配置ssh、rsh免密互信集群服务

    ssh免密互信操作 一.在SSH服务器所在机器上 1.以root用户登录,更改ssh配置文件 /etc/ssh/sshd_config,去除以下配置的注释 RSAAuthentication yes ...

  9. linux中三台主机之间互信,22、linux的ssh互信配置

    转载:https://blog.csdn.net/hrn1216/article/details/51568830 https://blog.csdn.net/u013144287/article/d ...

最新文章

  1. 现代软件工程 结对编程 (I) 三维棋类游戏
  2. NameError: name ‘sess‘ is not defined
  3. 【软考】2017年11月软件设计师上午真题5-8题答案解析
  4. string stringbuffer stringbuilder 区别
  5. C语言以递归实现归并排序Merge Sort算法(附完整源码)
  6. Java声明定义抽象类_接口_继承_实现
  7. leetcode124. 二叉树中的最大路径和
  8. Google 正式开源 Jib ,帮助 Java 应用快速容器化
  9. springboot整合canal
  10. 【一套代码小程序NativeWeb阶段总结篇】可以这样阅读Vue源码
  11. SpringBoot(Properties)
  12. Tomcat—logs文件夹中不再产生日志文件
  13. 逻辑表达式在c语言中作用,C语言中逻辑表达式与关系表达式的值
  14. 计算机网络第三章:数据链路层
  15. 17-sendto函数和异步错误
  16. 你应该懂得的关于电脑配置冷知识
  17. 添加打印机计算机无法访问,Win7系统添加打印机提示Windows无法打开“添加打印机”的解决方法...
  18. MySQL Error 1114
  19. Linux进程管理与控制课后作业
  20. English trip -- Phonics 1 ar

热门文章

  1. python安装在什么系统下最好-自学python用什么系统好
  2. NVIDIA Jetson Xavier NX中安装的python库包的版本
  3. redux-thunk的简单使用
  4. 图解kafka - 设计原理解析
  5. (转)python协程2:yield from 从入门到精通
  6. php 超全局变量(整理)
  7. bzoj 1827: [Usaco2010 Mar]gather 奶牛大集会【树形dp】
  8. 廉颇老矣,尚能饭否?响鼓重擂,上阵杀敌!
  9. MySQL 第七天(核心优化一)
  10. Python系统命令操作