关于 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')```