备份交换机文件到tftp服务器,使用Python批量备份Cisco和H3C交换机配置到TFTP服务器...

我的代码如下,已经转为python3格式:

#!/usr/bin/python3

# -*- coding: utf-8 -*-

import sys

import os

import telnetlib

import getpass

import datetime

import pexpect

import re

host = “”

user = “netadmin”

password = “sw434.as”

enpw = “QWE102@.on”

h3cpw = “123”

tftpserver = “172.16.103.43”

now = datetime.datetime.now()

def main():

for host in open(“sw.txt”, “r”).readlines():

dsthost = host.strip(‘\n’)

try:

tn = telnetlib.Telnet(dsthost, port=23, timeout=15)

tn.read_until(“Username:”)

tn.write(user + “\n”)

except:

print(“Can’t connection %s” % dsthost)

continue

try:

tn.read_until(“Password:”)

tn.write(password + “\n”)

result = tn.read_some()

rex_h3c_bin_1 = r’%Wrong password’

login_Failed_H3C_1 = re.search(rex_h3c_bin_1, result)

rex_h3c_bin_2 = r’%Username or password is invalid.’

login_Failed_H3C_2 = re.search(rex_h3c_bin_2, result)

except:

print(“Connection error %s” % dsthost)

continue

# print(login_Failed_H3C_1, login_Failed_H3C_2)

if ((login_Failed_H3C_1 is None) and (login_Failed_H3C_2 is None)):

# print(“cisco”)

try:

tn.write(“en\n”)

tn.read_until(“Password:”)

tn.write(enpw + “\n”)

tn.read_until(“#”)

tn.write(“copy running-config tftp:\n”)

tn.write(tftpserver + “\n”)

tn.write(now.strftime(“%Y/%m/%d”) + “/” + host + “\n”)

tn.read_until(“#”)

tn.close

print(now.strftime(“%Y/%m/%d”) + ” ” + dsthost + ” Backup successful.”)

except:

print(“Connection error %s” % dsthost)

continue

else:

# print(“H3c”)

try:

tn.write(h3cpw + “\n”)

tn.read_until(“>”)

tn.write(“tftp ” + tftpserver + ” put flash:/startup.cfg” + ” ” + now.strftime(

“%Y/%m/%d”) + “/” + host + “\n”)

tn.read_until(“>”)

tn.close

print(now.strftime(“%Y/%m/%d”) + ” ” + dsthost + ” Backup successful(h3c).”)

except:

print(“Connection error %s” % dsthost)

continue

if __name__ == “__main__”:

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值