python备份cisco交换机_Python备份思科设备的脚本

花了几天的时间研究一下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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值