0

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

现在有2个问题。

  1. 在某些情况下,我可以从设备 1 和 2 获取数据。尽管在某些情况下我只获得了 1 个设备。:(
Result 1:
['192.168.2.111', '192.168.2.113']      <-- List of devices
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
{2: {'MGMT IP': '192.168.2.113', 'OUTPUT': '*00:20:45.015 UTC Thu May 6 2021'}} <-- only device2

Result 2:
['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
{1: {'MGMT IP': '192.168.2.111', 'OUTPUT': '\nhostname R1'}, 2: {'MGMT IP': '192.168.2.113', 'OUTPUT': '*00:23:19.967 UTC Thu May 6

2021'}} <-- 设备 1 和 2 都存在。

  1. 我正在尝试将键值对附加到字典中,但它会覆盖数据。
['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

我的目标是拥有这样的东西。所以我可以很容易地组织数据。[在此处输入图像描述][1] [1]:https://i.stack.imgur.com/EL5om.png

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',

'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")
4

0 回答 0