多线程查系统(类的继承)

import threading                                                                         
import paramiko                                                                          
from paramiko.ssh_exception import NoValidConnectionsError,AuthenticationException       
class unameThread(threading.Thread):                                                     
    def __init__(self,cmd,hostname,port=22,user='root'):                                 
        super(unameThread, self).__init__()                                              
        self.cmd = cmd                                                                   
        self.hostname = hostname                                                         
        self.port = port                                                                 
        self.user = user                                                                 
    def run(self):                                                                       
        client = paramiko.SSHClient()                                                    
        private_key = paramiko.RSAKey.from_private_key_file('hhh.py')                    
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())                     
        try:                                                                             
            client.connect(hostname=self.hostname,                                       
                           port=self.port,                                               
                           username=self.user,                                           
                           pkey=private_key)                                             
            stdin, stdout, stderr = client.exec_command(self.cmd)                        
        except NoValidConnectionsError as e:                                             
            print("%s连接失败" % (self.hostname))                                            
        except AuthenticationException as e:                                             
            print("%s密码错误" % (self.hostname))                                            
        except TimeoutError as e:                                                        
            print("%s连接超时" % (self.hostname))                                            
        else:                                                                            
            result = stdout.read().decode('utf-8')                                       
            print("%s运行结果:" % (self.hostname), result)                                   
        finally:                                                                         
            client.close()                                                               
def use_thread():                                                                        
    threads = []                                                                         
    for count in range(254):                                                             
        host = '172.25.254.%s' % (count + 1)                                             
        t = unameThread("uname",host)                                                    
        threads.append(t)                                                                
        t.start()                                                                        
    _ = [thread.join() for thread in threads]                                            
    print("任务执行结束.....")                                                                 

if __name__=="__main__":                                                                 
    use_thread()                                                                         

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值