0

关于 python 和 Netmiko 需要帮助使用多个设备进行运行配置备份,例如 ex:Cisco Aruba 和带有单个 python 脚本的 brocade 交换机,为每个供应商读取不同的 .csv 文件。或 CSV 文件应以以下格式显示。

我尝试导入 CSV 格式,下面的代码是否出错。

**device_type        ip                username            password           port**
cisco_ios           192.168.1.2        cisco            C1s??0123$5            22
brocade_fastiron    192.168.1.3        user             B1sc0123$5             22
hp_procurve         192.168.1.4        manager          Hp3s#@#sdsfd           22
import csv
import os
from datetime import date
import smtplib
from netmiko import ConnectHandler
import re
import zipfile
import sys
today = date.today()
target_dir = f"/tftpdata/Region1/{today}"
if not os.path.isdir(target_dir):
    os.makedirs(target_dir)
    print(f"\nCreated {target_dir} folder successfully")
else:
    print(f"\n{target_dir} folder already exists!")
# For Cisco Username and password
usr = "cisco"
pwd = "XXXXXXX"
# For Brocade Username and password
usr1 = "user"
pwd1 = "XXXXXXX"
#For Aruba Username and Password
usr2 = "manager"
pwd2 = "XXXXXXX"
with open(file="iplist.csv", mode="r") as iplist:
    reader = csv.reader(iplist)
    Cisco = [
        {
            "device_type": "cisco_s300",
            "ip": ip.strip(),
            "username": usr,
            "password": pwd,
            "fast_cli": False,
        }
        for ip in iplist
    ]
with open(file="iplist1.csv", mode="r") as iplist:
    reader = csv.reader(iplist)
    Brocade = [
          {
            "device_type": "brocade_fastiron",
            "ip": ip.strip(),
            "username": "usr1",
            "password": "pwd1",
            "secret": "xxxxxxxxx"
            "fast_cli": False,
        }
        for ip in iplist
   ]
with open(file="iplist2.csv", mode="r") as iplist:
    reader = csv.reader(iplist)
    Aruba = [
          {
            "device_type": "hp_procurve",
            "ip": ip.strip(),
            "username": "usr2",
            "password": "pwd2",
            "fast_cli": False,
        }
        for ip in iplist
   ]
DEVICE_LIST = [Cisco, Brocade, Aruba]
for DEVICE in DEVICE_LIST:
    print(f'\n#### Connecting to {DEVICE["ip"]} ####\n')
    with ConnectHandler(**DEVICE) as ssh_conn:
        running_config = ssh_conn.send_command("show running-config")
    with open(
        f'{target_dir}/{DEVICE["ip"]}-running-config-{today}.txt', "w"
    ) as outfile:
        outfile.write(running_config.lstrip())
    print(f'Backup completed for {DEVICE["ip"]}\n')```
4

1 回答 1

1

只需创建函数来获取文件信息和设备配置。

使用 pandas 模块或 CSV 模块读取您的 CSV/excel 文件并遍历行以获取详细信息。

from netmiko import ConnectHandler
import pandas as pd

#获取设备配置的函数

def get_device_conf(IP,user,password,device_type):

    devlist = {'device_type': device_type,
       'ip': IP,
       'username': user,
       'password': password,
     }

    net_connect = ConnectHandler(**devlist)
    output = net_connect.send_command("show running-config")
    #"do what you want to do with this output, you can save to file also"
    net_connect.close()

#从 csv 文件中获取设备详细信息

def get_device_detail(filename):

    df = pd.read_csv(filename)
    for index, row in df.iterrows(): 
        device_type=row["device_type"]
        IP=row["IP"]
        username=row["username"]            
        password=row["password"]
    
        get_device_conf(IP,user,password,device_type) #call the get config function to collect config and perform any action

#调用 get_device_details 函数来运行这两个函数并执行操作

get_device_detail(filename) 

#如果您有多个文件,那么只需创建一个文件名列表并申请一个循环来遍历所有文件。

例子:

list_filenames=["file1.csv","file2.csv","file3.csv"]

for filename in list_filenames:
    get_device_detail(filename)
于 2021-12-06T20:02:15.233 回答