Using a list of switch IP addresses, prompt for username and password and capture troubleshooting information for each switch:
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()
print ‘Collecting troubeshooting log file of ‘ + ip_address
remote_connection.send(‘show clock\n’)
remote_connection.send(‘enable\n’)
remote_connection.send(‘terminal more disable\n’)
remote_connection.send(‘show run\n’)
remote_connection.send(‘terminal more enable\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()
Shell output..
$ python sshclient.py
Enter your username: rwa
Password:
Successful connection 192.168.1.1
Collecting troubeshooting log file of 192.168.1.1
Saving to file called Log file of 192.168.1.1
Successful connection 192.168.1.2
Collecting troubeshooting log file of 192.168.1.2
Saving to file called Log file of 192.168.1.2
Sample of Log file of 192.168.1.1…
core1:1>enable
core1:1#terminal more disable
core1:1#show run
Preparing to Display Configuration…
#
# Fri Apr 20 18:40:19 2018 UTC
# box type : VSP-8284XSQ
# software version : 7.0.0.0_B885
# cli mode : ECLI
#
Content of myswitches.txt:
192.168.1.1
192.168.1.2