1. Create routerlist.csv file like the following.
Name,IP,Group
TORR-MPLS,172.X.X.X,HQs
HQSWT_LAN-248,172.X.X.X,HQs
HQSWT02-2,172.X.X.X,HQs
HQ-Edge02,172.X.X.X,HQs
NY-Edge01,192.X.X.X,NY
NY-Edge02,192.X.X.X,NY
NJ-Edge01,192.X.X.X,NJ
NJ-Edge02,192.X.X.X,NJ
2. Place the tftpserver.exe program on the subfolder named tftpsrv.
3. Python Script.
'''
Created on Oct 18, 2012
@author: chu
Version 0.1
'''
import os, subprocess, telnetlib, time
print "*********************************************************************"
print "** This program will backup Network Equipment configuration. **"
print "** Version 0.1 **"
print "*********************************************************************"
print ""
#define functions
def makefolder (fdname):
if not os.path.isdir(os.path.join(os.getcwdu(),'NetEqtConfig',fdname)):
os.mkdir(os.path.join('NetEqtConfig',fdname))
def cp2tftp(hostip,hostname,user,password,tftpsrvip):
tn = telnetlib.Telnet(hostip)
tn.read_until("Username: ",10)
tn.write(user+ "\n")
tn.read_until("Password: ",10)
tn.write(password + "\n")
tn.read_until("#",10)
tn.write("copy running-config tftp\n")
tn.read_until("[]?",10)
tn.write(tftpsrvip + "\n")
tn.read_until("]?",10)
tn.write(hostname + "\n")
tn.read_until("#",10)
tn.write("exit\n")
tn.close()
#Read Network Equipment list
eqtlist = []
rfile = open('routerlist.csv','rb')
for line in rfile.readlines():
tmplist = [s for s in line.rstrip().split(',')]
eqtlist.append(tmplist)
rfile.close()
#Create Root Folder
if not os.path.isdir('NetEqtConfig'):
os.mkdir('NetEqtConfig')
#Create Subfolders
for n in range(1, len(eqtlist)):
makefolder(eqtlist[n][2])
tftpsrvp = subprocess.Popen([os.path.join(os.getcwd(),'tftpsrv','tftpserver.exe')])
print "TFTP Server is running."
tftpsrvip = "ftfpserver ip address"
user = "routerloginusername"
password = "routerloginpassword"
#Telnet and backup configuration file
print "Downloading Configuration files. Please wait..."
print ""
#for n in range(1, len(eqtlist)):
for n in range(1, len(eqtlist)):
hostname = eqtlist[n][0]
hostip = eqtlist[n][1]
try:
cp2tftp(hostip,hostname,user,password,tftpsrvip)
time.sleep(5)
nfpath = os.path.join(r'.\NetEqtConfig',eqtlist[n][2],hostname+"-"+hostip)
os.rename(hostname,nfpath)
except:
print "Failed to download configuration file from %s!" %hostname
continue
time.sleep(3)
tftpsrvp.kill()
tftpsrvp.wait()
print ""
print "Download finished!"
print "TFTP Server is closed."
nfoldername = 'NetEqtConfig-' + time.strftime("%m%d%Y", time.gmtime())
os.rename('NetEqtConfig',nfoldername)
print ""
print "All network equipment configuration save on %s" %nfoldername
No comments:
Post a Comment