自动备份交换机配置

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/usr/bin/env python
#coding:utf-8
"""
  Author:  Edward.Zhou -- <edgeman_03@163.com>
  Purpose: 批量自动备份思科交换机配置文件
  Created: 2015/5/8
"""

import sys
import os
import telnetlib
import time
import threading
import datetime

#Use for loop to telnet into each routers and execute commands
class Bakconf(threading.Thread):
    def __init__(self,host,username,password):
        threading.Thread.__init__(self)
        self.host=host
        self.username=username
        self.password=password

    def run(self):
        try:
            tn = telnetlib.Telnet(self.host,port=23,timeout=10)
        except:
            print "Can't connection %s"% self.host
            return
        tn.set_debuglevel(5)
        tn.write(self.username +b"\n")
        tn.write("en\n")
        tn.write(self.password + b"\n")
        tn.write("copy startup-config tftp:\n")
        tn.write(tftpser + b"\n")
        tn.write(b"\n")
        time.sleep(1)
        tn.write("exit\n")
        tn.close()

def main():
    username = "**********"
    password = "**********"
    global tftpser
    tftpser="192.168.103.71"

    for host in open(r'sw.txt').readlines():
        dsthost = host.strip('\n')
        bakconf=Bakconf(dsthost, username, password)
        bakconf.start()

    #Backup switch config and tar
    time.sleep(1)
    dtime=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    os.popen('tar -cjf /backup/cisco/switch-'+dtime+'.tar.bz2 '+ '/tftproot')
    os.popen('rm -fr /tftproot/*')
    os.popen('find /backup/cisco/ -mtime +90  -exec rm {} \;')

if __name__=="__main__":
    main()