Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import pexpect as px
- import subprocess
- import time
- class IP(object):
- def __init__(self, oct4 = 1, oct3 = 2, oct2 = 201, oct1 = 10, mask=16):
- self.oct4 = oct4
- self.oct3 = oct3
- self.oct2 = oct2
- self.oct1 = oct1
- self.mask = mask
- def __str__(self):
- return "%d.%d.%d.%d"%(self.oct1, self.oct2, self.oct3, self.oct4)
- def with_mask(self, mask=None):
- mask = mask or self.mask
- return "%s/%s"%(str(self),mask)
- def wait_for(ip):
- while subprocess.call(["ping","-c","1",str(ip)]):
- time.sleep(1)
- print ".",
- init_ip = "10.90.90.90"
- tftp = "10.90.90.10"
- def fname(num=None):
- if not num:
- return "3528.conf"
- else:
- return "3528_%d.conf"%num
- for num in range(28,45):
- print "------- %.3d -------"%num
- print "Waiting for ping."
- wait_for(init_ip)
- ch = px.spawn("telnet %s"%init_ip)
- ch.expect("UserName:")
- print "Telnet to %s success"%init_ip
- ch.sendline("")
- ch.expect("DES-3528:admin#")
- print "Starting to download %s"%firmware
- ch.sendline("download firmware_fromTFTP %s src_file %s" % (tftp ,firmware) )
- ch.expect("DES-3528:admin#")
- print "Rebooting...",
- ch.sendline("reboot")
- ch.expect(px.EOF)
- print "Link closed. Waiting for ping."
- wait_for(init_ip)
- ch = px.spawn("telnet %s"%init_ip)
- ch.expect("UserName:")
- print "Telnet to %s success"%init_ip
- ip = IP(num)
- subprocess.call("cp",fname(),fname(num))
- subprocess.call("sed","-i",fname(num),"s,%s/,%s/,"%IP(),IP(num))
- ch.sendline("")
- ch.expect("DES-3528:admin#")
- print "Starting to loading config %s"%fname(num)
- ch.sendline("download cfg_fromTFTP %s src_file %s"%(tftp,fname(num)))
- print "Waiting for switch on new ip"
- wait_for(ip)
- time.sleep(5)
- ch = px.spawn("telnet %s"%init_ip)
- ch.expect("UserName:")
- ch.sendline("archim")
- ch.expect("PassWord:")
- ch.sendline("password")
- ch.expect("DES-3528:admin#")
- ch.sendline("save")
- ch.expect("DES-3528:admin#")
- print "All done"
Add Comment
Please, Sign In to add comment