linux互信(python 版本)

这篇博客介绍了如何使用Python来实现Linux系统的互信自动化。主要内容包括`config.py`配置文件、`mythread.py`多线程模块以及`linuxAuthSSH.py`核心互信脚本的详细讲解和应用。
摘要由CSDN通过智能技术生成

#互信思路

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值