Here is a python script which will backup the running-config of multiple ERS switches listed in a text file. This script will send Ctrl + Y code sequence to the switch and run the copy command to a local TFTP server.
Python file sshclientcfg.py:
import paramiko
import time
import getpass
username = raw_input(‘Enter your username: ‘)
password = getpass.getpass()
f = open (‘myswitches.txt’)
for line in f:
ip_address = line.strip()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print ‘Successful connection’, ip_address
remote_connection = ssh_client.invoke_shell()
remote_connection.send(“\x19”)
print ‘Collecting running-config of ‘ + ip_address
remote_connection.send(‘copy running-config tftp address 192.168.1.100 filename ‘ + ip_address + ‘.asc\n’)
time.sleep(20)
readoutput = remote_connection.recv(655350)
saveoutput = open(‘Log file of ‘ + ip_address, ‘w’)
print ‘Saving to file called Log file of ‘ + ip_address + ‘\n’
saveoutput.write(readoutput)
saveoutput.write(‘\n’)
saveoutput.close
ssh_client.close()
Text file Myswitches.txt:
192.168.1.5
192.168.1.4
Output of script:
$ python sshclientcfg.py
Enter your username: admin
Password:
Successful connection 192.168.1.5
Collecting running-config of 192.168.1.5
Saving to file called Log file of 192.168.1.5
Successful connection 192.168.1.4
Collecting running-config of 192.168.1.4
Saving to file called Log file of 192.168.1.4
Note: The log file captured with the IP of the switch shows the output from the session which is useful to verify if it worked as expected or there was an error. The running-config file generated by the switch will be sent to the TFTP server so look there for the ASCII file.
The line below is used to provide Ctrl + Y response if prompted.
remote_connection.send(“\x19”)