#互信思路
1:先检测服务器是否存活
2:如果存活,检测服务器上面是否有公匙存在,如果存在拿到密匙
3:如果不存在密匙,开始创建密匙,然后获取到新创建的密匙
4:集中所有的密匙,然后写入到服务器的~/.ssh/authorized_keys 文件里面
config.py
# -*- coding: utf-8 -*-
# @Time : 2020/4/22 下午3:43
# @Author : liwentong
import multiprocessing
PingHost=""
''' 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"
'''进程数'''
Pnum=multiprocessing.cpu_count()
mythread.py
import sys
import threading
from threading import Thread
from linuxAuthSshKey import ssh_cmd
class MyThread(Thread):
def __init__(self, tname, exec_name,Host_info_list):
Thread.__init__(self, name = tname)
self.IsRunning = False
self.hostList=Host_info_list
self.ssh_cmd=exec_name
self.result=[]
# 执行函数,进行计算
def run(self):
self.IsRunning = True;
host_list=self.hostList[0]
newList=[] if len(self.hostList[1:])==0 else self.hostList[1:]
for host_info in host_list:
newList.insert(0,host_info)
res=self.ssh_cmd(newList)
self.result.append(res)
del(newList[0])
self.IsRunning = False;
# 线程退出函数
def ExitThread():
sys.exit(0)
def get_result(self):
if len(self.result)!=0:
return self.result
else:
return []
linuxAuthSSH.py
# -*- coding: utf-8 -*-
# @Time : 2020/4/22 下午3:42
# @Author : liwentong
import paramiko
import sys
import os
import time
import subprocess
#import nmap
import argparse
import multiprocessing
from config import *
import socket
import mythread
#检查服务器是否存活
#nmap方式检测,需要装依赖,不适用新装服务器
#def checkHostExist(hostip,port):
# nm=nmap.PortScanner()
# cresult=nm.scan(hostip,'%s'%port,arguments="-P0 -sT -A")
# # cresult=nm.scan(hostip,'%s'%port)
## print(cresult)
# if cresult['scan'][hostip]['tcp'][int(port)]['state']=='open':
# return True
# else:
# return False
#socket检查服务器存活
def checkHostExist(flist):
hostinfo=flist[0]
hostip=hostinfo[0]
port=hostinfo[-1