Reputation: 557
Regarding python and Netmiko Need help to take running-config backup with multiple devices like ex:Cisco Aruba and brocade switches with single python script reading different .csv file for each vendor. Or CSV file should be present in below format.
I tried to import the CSV format, am I getting error on below code.
**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')```
Upvotes: 0
Views: 2365
Reputation: 1704
Just create functions to get files info and device config.
Read your CSV/excel file using pandas module or CSV module and iterate over the rows to get the details.
from netmiko import ConnectHandler
import pandas as pd
#Function to get device config
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()
#get device details from csv file
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
#call the get_device_details function to run both of the function and perform the action
get_device_detail(filename)
#If you have multiple files then simply create a list of filenames and apply for a loop to iterate over all the files.
Example:
list_filenames=["file1.csv","file2.csv","file3.csv"]
for filename in list_filenames:
get_device_detail(filename)
Upvotes: 2