#! /usr/bin/python # _*_ coding: UTF-8 _*_ # coding=utf-8 import os import sys import configparser import subprocess import paramiko import time # from multiprocessing import Process,Pool # import base64 ''' 主机存活检测命令''' pingCheck = ['ping', '-c', '3', '-i', '0.2', '-W', '1'] ''' ssh-keygen 命令 ''' keygenCmd = '''ssh-keygen -q -t rsa -P '' -f ~/.ssh/id_rsa && echo yes || echo no''' ''' 读取pub文件 ''' readPubCmd = '''test -f ~/.ssh/id_rsa.pub && cat ~/.ssh/id_rsa.pub || echo ''' ''' 查看pub文件是否存在 ''' testExistsCmd = '''test -f ~/.ssh/id_rsa.pub && echo yes || echo no''' ''' 追加公钥至authorized_keys中''' addKeyCmd = '''echo %s >> ~/.ssh/authorized_keys && echo yes || echo no''' ''' 建立互信后,首次登录时不输入yes''' add_ssh_config=''' echo 'StrictHostKeyChecking no' >> /etc/ssh/ssh_config ''' del_ssh_config=''' sed -i 's/^StrictHostKeyChecking no/#StrictHostKeyChecking no/' /etc/ssh/ssh_config ''' ''' 测试ssh互信''' ssh_date ='''ssh %s date ''' ''' 消息返回分隔符,应尽量复杂''' _mess_part = '''|+|''' ''' 进程池大小 ''' PROCESSES = 30 ''' 初始化配置文件 ''' def change_init(): server_id = '20,21,23' node = 'node2' config_file_path='my.ini' # change host # os.popen("hostnamectl set-hostname %s" % node) # os.popen("hostname") ## change my.cnf # os.chdir('/root/pycharm') cf = configparser.ConfigParser() cf.read(config_file_path) print(cf.get('mysqld','server_id')) cf.set('mysqld','server_id',server_id) cf.set('mysqld','report_host','192.168.66.102') with open(config_file_path, "w") as cfg: cf.write(cfg) print('server_id:' + str(eval(cf.get('mysqld','server_id')))) print('report_host:' + cf.get('mysqld','report_host')) sys.exit() aut_dic=[] ip_list=['192.192.66.101','192.168.66.102','node1','node2'] s_dic=[ {'ip':'127.0.0.1','port':'1010','username':'root','password':'root0101'}, {'ip':'127.0.0.1','port':'2020','username':'root','password':'root0101'} ] # ssh-keygen 命令 def exec_ssh_keygen(i,cmd=keygenCmd): server = paramiko.SSHClient() server.load_system_host_keys() server.set_missing_host_key_policy(paramiko.AutoAddPolicy()) server.connect(i['ip'],str(i['port']),i['username'],i['password'], timeout=5) stdin, stdout, stderr = server.exec_command(cmd) # print("stdin=%s\nstdout=%s\nstderr=%s" % (stdin,stdout,stderr)) # 读取pub文件 def readPubInfo(i,cmd=readPubCmd): server = paramiko.SSHClient() server.load_system_host_keys() server.set_missing_host_key_policy(paramiko.AutoAddPolicy()) server.connect(i['ip'],i['port'],i['username'],i['password'], timeout=5) stdin, stdout, stderr = server.exec_command(cmd) result = stdout.read() result = str(result,'utf-8') aut_dic.append(result.replace('\n','')) # i['aud'] = result.replace('\n','') # print(aut_dic) # 追加公钥至authorized_keys中 def writeKeys(i,cmd=addKeyCmd): server = paramiko.SSHClient() server.load_system_host_keys() server.set_missing_host_key_policy(paramiko.AutoAddPolicy()) server.connect(i['ip'],i['port'],i['username'],i['password'], timeout=5) # result = readPubInfo(i['ip'],i['port'],i['username'],i['password'],readPubCmd) for x in aut_dic: stdin, stdout, stderr = server.exec_command(cmd % x ) def test_ssh(i): server = paramiko.SSHClient() server.load_system_host_keys() server.set_missing_host_key_policy(paramiko.AutoAddPolicy()) server.connect(i['ip'], i['port'], i['username'], i['password'], timeout=5) stdin, stdout, stderr = server.exec_command(add_ssh_config) for ip in ip_list: stdin, stdout, stderr = server.exec_command(ssh_date % ip) stdin, stdout, stderr = server.exec_command(del_ssh_config) if __name__ == '__main__': for i in s_dic: exec_ssh_keygen(i, keygenCmd) readPubInfo(i,readPubCmd) for i in s_dic: writeKeys(i, cmd=addKeyCmd) test_ssh(i)
python3实现ssh互信并不输入YES
最新推荐文章于 2023-11-23 20:51:57 发布