简体   繁体   English

无法获得多处理netmiko多个设备的输出

[英]Not getting output for multiprocessing netmiko multiple devices

Not getting output for multiple device connection using multiprocessing 使用多处理无法获取多设备连接的输出

I tried the following code: 我尝试了以下代码:

import multiprocessing as mp
from netmiko import ConnectHandler
import netmiko
import re
from datetime import datetime

def get_ip (input):
    return(re.findall(r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)', input))

def make_connection (ip, username, password):
                    device_connect = ConnectHandler(device_type='cisco_nxos', ip=ip, username=username, password=password)
                    output = device_connect.send_command("sh hostname")
                    print(output)
                    device_connect.disconnect()

def get_ips_nexus (file_name):
    for line in open(file_name, 'r').readlines():
        line = get_ip(line)
        for ip in line: 
            ips_nexus.append(ip)

ips_nexus = []
get_ips_nexus("ips-Copy.txt")
print(ips_nexus)
username = 'x.x.x.x'
password = 'xxxxxxxx'
startTime = datetime.now()
processes = []

for ip in ips_nexus:
            print('***********')
            print(ip)
            print('***********')
            p = mp.Process(target=make_connection, args=(ip, username, password))
            processes.append(p)
            p.start()
            print('!!!!!!!!!!!!!!!!!!!!!!!!!')

for p in processes:
    print(p)
    p.join()
print(datetime.now() - startTime

I am getting only print ip not the netmiko connected output of sh hostname. 我只得到打印ip而不是sh主机名的netmiko连接的输出。

I want to achieve multiple device connectivity using multiprocessing only!!! 我只想使用多处理来实现多设备连接!!!

Please help. 请帮忙。

You are missing the if __name__ == "__main__" clause! 您缺少if __name__ == "__main__"子句!
Add it above your for - loop for the processes: 将其添加到for-循环的上方:

if __name__ == "__main__":    # add this
    for ip in ips_nexus:
            print('***********')
            print(ip)
            print('***********')
            p = mp.Process(target=make_connection, args=(ip, username, password))
            processes.append(p)
            p.start()
            print('!!!!!!!!!!!!!!!!!!!!!!!!!')

Why do I need it? 我为什么需要它?

First of all: If you run a code of yours, the python interpreter imports it as a " main " file. 首先:如果您运行自己的代码,则python解释器会将其导入为“ ”文件。 You can try it, by adding print(__name__) . 您可以通过添加print(__name__)来尝试。 Anyway, if you start a new process with multiprocessing , than the python interpreter imports your code again and runs it as an extra-process and as a new skript!!! 无论如何,如果您使用multiprocessing启动新进程,则python解释器会再次导入您的代码,并将其作为额外进程新Skript运行! Well, if it reaches (in your case) this part again: 好吧,如果它再次达到(对于您而言)这部分:

p = mp.Process(target=make_connection, args=(ip, username, password))  

it will start a new process again, imports it again and so on. 它将再次开始一个新过程,再次导入它,依此类推。 In the end you are going to have an infinite loop, which starts your code every time as a new process! 最后,您将有一个无限循环,它每次都会以新进程的形式启动代码! That's the reason why you have to add the ( important ) if __name__ == "__main__" clause! 这就是为什么您必须添加( 重要if __name__ == "__main__"子句的原因! By adding this, __name__ won't be __main__ after the first call of a new process! 通过添加此名称, __name__将不会在首次调用新进程后成为__main__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM