Below script will take the backup of cisco devices in the folder where you have stored your python script. Please make sure ip.txt file(which contains router IP address) is also in same folder.
Below is the sample ip.txt file.
import paramiko
import time
import datetime
ssh1 = paramiko.SSHClient()
ssh1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
now = datetime.datetime.now()
#open file in read mode
#ip.txt is a text file which contains routers IP address. Make sure it is stored in same folder #where the python script is.
f = open("ip.txt","r")
lines = f.readlines()
f.close()
#open file in write mode and remove spaces between rows.
f = open("ip.txt","w")
for line in lines:
if
line!=""+"\n":
f.write(line)
f.close()
ips = [i.strip() for i in open("ip.txt")]
for ip in ips:
try:
filename_prefix = (ip)
ssh1.connect(ip,username='test', password='test')
ssh =
ssh1.invoke_shell()
time.sleep(5)
print ip
ssh.send("\nterminal length 0\n")
ssh.send("show run\n")
time.sleep(5)
output =
ssh.recv(65535)
print
output
filename =
"%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" %
(filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)
fp=open(filename,"w")
fp.write(output)
fp.close()
except:
response =
'Failed'
print (ip + " is not reachable")
Hi,
ReplyDeleteYour posts are awesome !! I tried your script and it worked well. Can i get one small modification ? I want to prompt username & password instead of hard code it. Can i get a modified script ?