When I needed to write a program to access Cisco switches (Catalyst 2960 etc.) with ssh and automatically acquire the config, and collected information, it seems that it can be realized with a python library called netmiko, so I built an environment and created a program.
In addition, since the version of python that can be installed with yum in the default repository on CentOS 7 is 2 series and netmiko does not work as it is, the procedure for installing python3 is also described.
Add an IUS repository.
# yum install -y https://centos7.iuscommunity.org/ius-release.rpm
Execute the following command to check the minor versions that can be installed.
# yum search python3
Install the latest python3.6 (main unit + library, development, package management, etc.) at the moment.
# yum install -y python36u python36u-libs python36u-devel python36u-pip
# python -V
Python 3.6.8
# pip -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
Install the netmiko library.
# pip install netmiko
Read the csv file that describes the IP address and password separated by commas as a sample program Write the python code that saves running-config in the specified directory.
devices.csv
192.168.1.254,password1234
172.16.1.254,password5678
backup.py
#!/usr/bin/env python
from netmiko import ConnectHandler
import csv
import datetime
#Date acquisition process to include the date in the config backup file
now = datetime.datetime.now()
today = now.strftime('%Y%m%d')
with open('devices.csv') as f:
reader = csv.reader(f)
#Read one line of csv file, get IP address and password, and turn the process
for row in reader:
cisco_ios = {
'device_type': 'cisco_ios', #For ssh, specify the left
'ip': row[0], #First column of csv file
'username': 'user_name', #Actual username
'password': row[1], #Second column of csv file
'port' : 22, #port number
'secret': row[1], #enable secret password
'verbose': False,
}
net_connect = ConnectHandler(**cisco_ios)
#Move to privileged mode
net_connect.enable()
#Save running config to startup config with write memory command
net_connect.send_command('wr')
# running-Get config
output = net_connect.send_command('show running-config')
#Get the host name
prompt = net_connect.find_prompt()
hostname = prompt[:-1]
#Exclude unnecessary lines at the beginning
list = output.split('\n')
list = list[3:]
config = '\n'.join(list)
#File name creation
file = 'store/' + hostname + '-' + today + '.txt'
#Save the file in the store directory
with open(file, 'w') as backup:
backup.write(config)
print(hostname + ' : ' + 'succeeded')
net_connect.disconnect()
Also, create a save destination directory for the config file.
# mkdir store
If you place the py file, csv file, and store directory in the same directory and execute the py file, the running config will be saved in the store directory in the format of "hostname-date.txt".
# ls
backup.py devices.csv store
# python backup.py
switch1 : succeeded
switch2 : succeeded
# ls store/
switch1-20200205.txt switch2-20200205.txt
Recommended Posts