花了几天的时间研究一下Python,终于能折腾出一支可以备份思科设备的脚本。真心佩服那些码农的脑袋瓜。。。以下为备份的脚本

#!/usr/bin/eve python

import pexpect
import sys
import time
import datetime

class CiscoSwitch():

def __init__(self, host, username, password):
    self.username = username
    self.host = host
    self.password = password

def Login(self):
    self.child = pexpect.spawn('telnet '+self.host)
    self.child.expect('Username:')
    self.child.sendline(self.username)
    self.child.expect('Password:')
    self.child.sendline(self.password)
    self.child.expect('#')
    self.child.sendline('terminal length 0')
    self.child.expect('#')
    return (self.child, self.child.before)

def FtpBackupCmd(self,tftpip):
    self.child.sendline('copy running-config tftp:')
    self.child.expect(']?')
    self.child.sendline(tftpip)
    self.child.expect(']?')
    DATE = datetime.datetime.now().strftime('%Y-%m-%d‘)
    self.child.sendline(DATE+'-'+self.host)
    self.child.expect('#')
    return (self.child, self.child.before)

 if __name__ == '__main__':
    for ip in open('/opt/other/ip.txt') :
            Switch = CiscoSwitch(ip,'admin','Password.123')
            (obj,stdout) = Switch.Login()
            (obj,stdout) = Switch.FtpBackupCmd('172.16.200.201')
            print stdout     

Python备份思科设备的脚本_第1张图片