通过paramiko实现验证linux系统ip,端口,密码是否正确

import time
import paramiko
import os
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在known_hosts文件上的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def conn(hostname,port,username,passwd):
    '''检测连接是否成功'''
    try:
        ssh.connect(hostname,port,username,passwd)
        ssh.close()
        return('%s     %s连接成功\n'%(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())),hostname))
    except TimeoutError as e:
        ssh.close()
        return ('%s     %s连接超时\n'%(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())),hostname))
# # 字节bytes转化kb\m\g
# def formatSize(bytes):
#     '''文件大小转换'''
#     try:
#         bytes = float(bytes)
#         kb = bytes / 1024
#     except Exception as e:
#         print("传入的字节格式不对")
#         return "Error"
#     if kb >= 1024:
#         M = kb / 1024
#         if M >= 1024:
#             G = M / 1024
#             return "%fG" % (G)
#         else:
#             return "%fM" % (M)
#     else:
#         return "%fkb" % (kb)
# def getDocSize(path):
#     '''获取文件大小'''
#     try:
#         size = os.path.getsize(path)
#         return formatSize(size)
#     except Exception as err:
#         print(err)
# def check():
#     '''检测resoult是否存在,存在重命名为昨天的,没必要,文件太小了'''

# check()
with open('account','r') as f:
    a = f.readlines()
    for i in a:
        '''遍历文件,其中account为存储ip,port,passwd的文件,result文件为输出的结果'''
        account = i.split(',')
        hostname =account[0].strip()
        port = account[1].strip()
        username= account[2].strip()
        passwd = account[3].strip()
        conn = conn
        out = conn(hostname,port,username,passwd)
        with open('result','a',encoding='utf-8') as q:
            q.write(out)


多线程

#HUCHEN
import time
import paramiko
import os
import threading
import queue
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在known_hosts文件上的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

def conn(hostname,port,username,passwd):
    '''检测连接是否成功'''
    try:
        ssh.connect(hostname,port,username,passwd)
        ssh.close()
        return('%s     %s连接成功\n'%(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())),hostname))
    except:
        ssh.close()
        # return ('...')
        return ('%s     %s有毛病,大佬快去看看\n'%(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())),hostname))
acc = queue.Queue()
lock =threading.RLock()
thread_list = []
def queue_put(a):
    '''把文件中的内容读取到缓存中,即队列中'''
    with open(a,'r') as f:
        a = f.readlines()
        for i in a:
            acc.put(i)

def write_res(account):
    '''把结果写入文件中'''
    semaphore.acquire()
    account = account.split(',')
    hostname =account[0].strip()
    port = account[1].strip()
    username= account[2].strip()
    passwd = account[3].strip()
    out = conn(hostname, port, username, passwd)
    lock.acquire()
    with open('result','a',encoding='utf-8') as q:
        q.write(out)
    lock.release()
    semaphore.release()


queue_put('account')
count = acc.qsize()
semaphore  = threading.BoundedSemaphore(10)
# while acc.qsize()!=0:

for i in range(count):
    t = threading.Thread(target=write_res,args=(acc.get(),))
    t.start()

while threading.active_count() != 1:
    pass #print threading.active_count()
else:
    print('----all threads done---')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值