使用 Python 将上传线程化到多个设备

Threading an upload to multiple devices with Python

我正在尝试同时将一个文件上传到多个设备,但到目前为止还没有成功。我想做的是创建 2 个工人来跟进包含 ips 列表的 csv,但它重复相同的任务:

import csv
from datetime import datetime
from netmiko import ConnectHandler, file_transfer
from netmiko.ssh_exception import NetmikoTimeoutException, NetmikoAuthenticationException
import threading

def parse_csv(filename) -> list:
    with open(filename, mode="r") as csv_file:
        csv_reader = csv.DictReader(csv_file)
        rows = [row for row in csv_reader]
    return rows

source_file = "/home/dev/file_to_upload" #file i want to upload
dest_file = "uploaded_file"
direction = "put"

def run_commands(ip):
    ios_device = {
        "device_type": "cisco_ios",
        "ip": ip,
        "username": "admin",
        "password": "password1",
        "fast_cli": False,
        "global_delay_factor": 4,
        "file_system": "flash:",
    }

    try:
        file_system = ios_device.pop("file_system")
        print(f'transffering to {ip}:')
        ssh_conn = ConnectHandler(**ios_device)
        ssh_conn.enable()
        transfer_dict = file_transfer(
            ssh_conn,
            source_file=source_file,
            dest_file=dest_file,
            file_system=file_system,
            direction=direction,
        overwrite_file=True,
        )
        print(transfer_dict)

    except ValueError or NetmikoTimeoutException as error:
        print(error)
        
    ssh_conn.disconnect()
    return

def main():
    FILENAME = "ios_update.csv"
    hosts = parse_csv(FILENAME)

    for host in hosts:
        t1 = threading.Thread(target=run_commands(host["ip"]), args=('Thread 1', 1))
        t2 = threading.Thread(target=run_commands(host["ip"]), args=('Thread 2', 2))
        t1.start()
        t2.start()
        t1.join()
        t2.join()
        print(t1)
        print(t2)

if __name__ == "__main__":
    main()

代码有效,但它重复了任务而不是一项一项地执行。在这种情况下正确的做法是什么?谢谢!

with open(filename, mode="r") as csv_file:

我无法测试代码,因为这个文件不存在,但我用下面的代码同时在许多设备上进行了配置条目,我推荐给你。

您可以将其转换为代码中的文件名,而不是单个配置行。希望有用。

def _ssh_(nodeip):
    try:
        device = {
            'device_type': 'cisco',
            'ip': X.X.X.X,
            'username': username,
            'password': password,
       }
       net_connect = Netmiko(**device)
       print(nodeip.strip() + "  " + "success enter")
    except Exception as e:
        print(e)
        f_3.write(nodeip.strip() + "\n")
        return
                
    prompt_config_fnk = net_connect.find_prompt()
    hostname_fnk = prompt_config_fnk.strip("<" + ">")
    print(hostname_fnk)
    net_connect.send_command_timing("enable")
    output = net_connect.send_command_timing("config")
    print("config mode entered")
    net_connect.send_command_timing("acl 2010 ")
    net_connect.send_command_timing("quit")
    net_connect.send_command_timing("save")
                
    print("config done")
    with open("List_OK_2.txt", "a") as f:
        f.write(nodeip + "\n")  
    net_connect.disconnect()
                
with open("ip_list.txt", "r") as f_2:
    ip_list = f_2.readlines()

with open("ssh_unsuccess_2.txt", "w") as f_3:
    myPool = ThreadPool(100)
    result = myPool.map(_ssh_, ip_list)