Python 如何通过网络和线程收集、存储和组织输出?

Python 如何通过网络和线程收集、存储和组织输出?,python,python-3.x,multithreading,python-multithreading,netmiko,Python,Python 3.x,Multithreading,Python Multithreading,Netmiko,需要您的帮助和建议,我正在从网络设备收集数据,并将其存储到字典“output_dict={}”,同时我将使用此字典来组织数据,但是在使用多线程时,我无法收集所有数据 现在有两个问题 我可以从设备1和设备2获取数据。尽管在某些情况下,我只能获得1台设备:( 'output1'}{'MGMT IP':'192.168.2.113','output2'} 如何在使用多线程和字典时实现这一点。谢谢。这是我的代码 # ~~~~~~~~ # Import modules # ~~~~~~~~ import

需要您的帮助和建议,我正在从网络设备收集数据,并将其存储到字典“output_dict={}”,同时我将使用此字典来组织数据,但是在使用多线程时,我无法收集所有数据

现在有两个问题

  • 我可以从设备1和设备2获取数据。尽管在某些情况下,我只能获得1台设备:(
  • 'output1'}{'MGMT IP':'192.168.2.113','output2'}

    如何在使用多线程和字典时实现这一点。谢谢。这是我的代码

    # ~~~~~~~~
    # Import modules
    # ~~~~~~~~
    import subprocess, logging, re, getpass,time
    from netmiko import ConnectHandler
    from netmiko.ssh_exception import NetMikoTimeoutException, NetMikoAuthenticationException
    from paramiko.ssh_exception import SSHException
    import threading
    from queue import Queue
    ##from threading import Thread
    
    logging.basicConfig(level=logging.INFO,
                        format="{asctime} {levelname:<8} {message}",
                        style='{',
                        filename='%slog' % __file__[:-2],
                        filemode='a'
                        )
    ##logging.info("this is a message")
    
    # ~~~~~~~~
    # Define functions
    # ~~~~~~~~
    dev_list = []
    success_dev = []
    failed_dev = []
    output_dict = {}
    
    def ping_ip(ip):
        global gips
        rstlongstr = ''
        (output,error) = subprocess.Popen((['ping', ip, '-c', '2']), stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True).communicate()
        if 'bytes from' in output:
            dev_list.append(ip)
            logging.info(output)
    ##        print (output)
            reg_rttavg = re.findall(r'(?=rtt.*=\s[\d.]+\/([\d.]+))',output)
            strpreg_rttavg = [test2.strip() for test2 in reg_rttavg]
            str_rttavg = '\n'.join(strpreg_rttavg)
            reg_ploss = re.findall(r'[\d%.]+(?= packet loss)',output)
            strpreg_ploss = [test2.strip() for test2 in reg_ploss]
            str_ploss = '\n'.join(strpreg_ploss)
            return "[ " + ip + " ] Reachable - rtt(avg): %s ploss: %s" % (str_rttavg,str_ploss)
        elif 'Host Unreachable' in output:
            return "[ " + ip + " ] Unreachable"
        else:
            return "[ " + ip + " ] Unreachable"
        thread_q.put
    
    def seq_sendcmd(device_conn,ip_a):
        global success_dev, failed_dev,output_dict
        FinalResult = ''
        sh_commands  = ['show run | i hostname', 'show clock']
        for cmd in sh_commands:
            result_out = device_conn.send_command(cmd)
            output_dict = {}
            output_dict['MGMT IP'] = ip_a
            output_dict['OUTPUT'] = result_out
            FinalResult = FinalResult + ('*****%s\n\n%s\n\n' %(cmd,result_out))
        device_conn.disconnect()
        print ('[ ' + ip_a + ' ] Logged out')
        return FinalResult
    
    def connect_now(ip_a,username,password,enable_s):
        global success_dev, failed_dev
        print ('[ ' + ip_a + ' ] Loging into IP')
        
        try:
            
            print ("X is: ",x)
            device_conn = ConnectHandler(device_type='cisco_ios_ssh', ip=ip_a,
                                                 username=username, password=password,
                                                 secret=enable_s, verbose=False)
            output = seq_sendcmd(device_conn,ip_a)
            output_q.put(output)
            
        except Exception as e: print(e) 
    
    #~~~~~~~~~~~~~~
    #MAIN
    #~~~~~~~~~~~~~~
    ##ipadd = ['192.168.2.111',"192.168.2.113", '9.9.9.222','192.168.2.112',"192.168.2.66",]
    ipadd = ['192.168.2.111',"192.168.2.113"]
    thread_q = Queue()
    for x in ipadd:
        m_thread = threading.Thread(target=ping_ip, args=(x,))
        m_thread.start()
        time.sleep(3)
    m_thread.join()    
    print (dev_list)   
    
    Results = []
    uname = "test"
    pswd = "test"
    ena = "test"
    
    if uname == "" or pswd == "" or ena == "":
        print('username, password or enable_secret cannot be empty. Please enter valid credentials!!!')
        logging.info("username, password or enable_secret cannot be empty. Please enter valid credentials!!!")
        sys.exit()
    
    logging.info("List of reachable device: " + str(dev_list))
    start = time.time()
    output_q = Queue()
    threads = []
    x = 0
    for ip in dev_list:
        x+=1
        print ("Processing: ",ip)
        t = threading.Thread(target=connect_now, args=(ip,uname,pswd,ena))
        threads.append(t)
        t.start()
        time.sleep(3)
    for t in threads:
      t.join()
    ##print (output_q.get())
    print (output_dict)
    end = time.time()
    total = int(end - start)
    print("\n Elapsd Time: " + str(total) + " Sec\n")
    
    #~~~~~~~~
    #导入模块
    # ~~~~~~~~
    导入子流程、日志记录、re、getpass、时间
    从netmiko导入ConnectHandler
    从netmiko.ssh_异常导入NetMikoTimeoutException、NetMikoAuthenticationException
    从paramiko.ssh_异常导入SSHException
    导入线程
    从队列导入队列
    ##从线程导入线程
    logging.basicConfig(level=logging.INFO,
    
    format=“{asctime}{levelname:Python一点也不擅长,也许可以试试node.js?express?HI Flash,不幸的是,这是我们环境中唯一允许的程序语言。
    ['192.168.2.111', '192.168.2.113']
    Processing:  192.168.2.111
    [ 192.168.2.111 ] Loging into IP
    Processing:  192.168.2.113
    [ 192.168.2.113 ] Loging into IP
    [ 192.168.2.111 ] Logged out
    [ 192.168.2.113 ] Logged out
    {'MGMT IP': '192.168.2.113', 'OUTPUT': '*00:55:21.055 UTC Thu May 6 2021'} <-- Overides devices 1
    
    cli output: 
    x = {'MGMT IP': '192.168.2.111', 'OUTPUT': 'output1'}{'MGMT IP': '192.168.2.111', 'OUTPUT': 'output2'}{'MGMT IP': '192.168.2.113',
    
    # ~~~~~~~~
    # Import modules
    # ~~~~~~~~
    import subprocess, logging, re, getpass,time
    from netmiko import ConnectHandler
    from netmiko.ssh_exception import NetMikoTimeoutException, NetMikoAuthenticationException
    from paramiko.ssh_exception import SSHException
    import threading
    from queue import Queue
    ##from threading import Thread
    
    logging.basicConfig(level=logging.INFO,
                        format="{asctime} {levelname:<8} {message}",
                        style='{',
                        filename='%slog' % __file__[:-2],
                        filemode='a'
                        )
    ##logging.info("this is a message")
    
    # ~~~~~~~~
    # Define functions
    # ~~~~~~~~
    dev_list = []
    success_dev = []
    failed_dev = []
    output_dict = {}
    
    def ping_ip(ip):
        global gips
        rstlongstr = ''
        (output,error) = subprocess.Popen((['ping', ip, '-c', '2']), stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True).communicate()
        if 'bytes from' in output:
            dev_list.append(ip)
            logging.info(output)
    ##        print (output)
            reg_rttavg = re.findall(r'(?=rtt.*=\s[\d.]+\/([\d.]+))',output)
            strpreg_rttavg = [test2.strip() for test2 in reg_rttavg]
            str_rttavg = '\n'.join(strpreg_rttavg)
            reg_ploss = re.findall(r'[\d%.]+(?= packet loss)',output)
            strpreg_ploss = [test2.strip() for test2 in reg_ploss]
            str_ploss = '\n'.join(strpreg_ploss)
            return "[ " + ip + " ] Reachable - rtt(avg): %s ploss: %s" % (str_rttavg,str_ploss)
        elif 'Host Unreachable' in output:
            return "[ " + ip + " ] Unreachable"
        else:
            return "[ " + ip + " ] Unreachable"
        thread_q.put
    
    def seq_sendcmd(device_conn,ip_a):
        global success_dev, failed_dev,output_dict
        FinalResult = ''
        sh_commands  = ['show run | i hostname', 'show clock']
        for cmd in sh_commands:
            result_out = device_conn.send_command(cmd)
            output_dict = {}
            output_dict['MGMT IP'] = ip_a
            output_dict['OUTPUT'] = result_out
            FinalResult = FinalResult + ('*****%s\n\n%s\n\n' %(cmd,result_out))
        device_conn.disconnect()
        print ('[ ' + ip_a + ' ] Logged out')
        return FinalResult
    
    def connect_now(ip_a,username,password,enable_s):
        global success_dev, failed_dev
        print ('[ ' + ip_a + ' ] Loging into IP')
        
        try:
            
            print ("X is: ",x)
            device_conn = ConnectHandler(device_type='cisco_ios_ssh', ip=ip_a,
                                                 username=username, password=password,
                                                 secret=enable_s, verbose=False)
            output = seq_sendcmd(device_conn,ip_a)
            output_q.put(output)
            
        except Exception as e: print(e) 
    
    #~~~~~~~~~~~~~~
    #MAIN
    #~~~~~~~~~~~~~~
    ##ipadd = ['192.168.2.111',"192.168.2.113", '9.9.9.222','192.168.2.112',"192.168.2.66",]
    ipadd = ['192.168.2.111',"192.168.2.113"]
    thread_q = Queue()
    for x in ipadd:
        m_thread = threading.Thread(target=ping_ip, args=(x,))
        m_thread.start()
        time.sleep(3)
    m_thread.join()    
    print (dev_list)   
    
    Results = []
    uname = "test"
    pswd = "test"
    ena = "test"
    
    if uname == "" or pswd == "" or ena == "":
        print('username, password or enable_secret cannot be empty. Please enter valid credentials!!!')
        logging.info("username, password or enable_secret cannot be empty. Please enter valid credentials!!!")
        sys.exit()
    
    logging.info("List of reachable device: " + str(dev_list))
    start = time.time()
    output_q = Queue()
    threads = []
    x = 0
    for ip in dev_list:
        x+=1
        print ("Processing: ",ip)
        t = threading.Thread(target=connect_now, args=(ip,uname,pswd,ena))
        threads.append(t)
        t.start()
        time.sleep(3)
    for t in threads:
      t.join()
    ##print (output_q.get())
    print (output_dict)
    end = time.time()
    total = int(end - start)
    print("\n Elapsd Time: " + str(total) + " Sec\n")