Python如何可以批量的备份交换机配置

简介: 可以使用telnetlib模块

使用telnetlib模块,首先登录到交换机,列出并获取配置文件的名称,然后通过tftp协议将配置文件传输到文件服务器上,为避免配置文件覆盖,将备份的配置文件名称统一加入日期以作区分。

  1. 登录方式和口令有好几种,比较懒惰,通过不同列表以做区分,如果每个交换机口令都不相同的话,就需要额外处理了。
  2. 交换机的配置文件也有多种类型,也是通过列表进行区分。
  3. 有些交换机支持ftp和sftp,但测试发现有些虽然有相应的客户端命令,但传输总有问题。也不能将每个交换机都配置为ftp服务器,不安全也不方便。最后采用tftp解决。tftp比较简单,没有办法创建目录以区分不同日期的备份。好在配置文件已经加入了日期做区分,马马虎虎可以运行了。
import telnetlib,sys

from datetime import date
today=date.today()
print(today)
ipaddrset1=['192.168.1.19','192.168.1.29','192.168.1.59']
ipaddrset2=['192.168.1.39','192.168.1.49','192.168.1.69','192.168.1.56','192.168.1.6','192.168.1.9','192.168.1.24',
      '192.168.1.72','192.168.1.73','192.168.1.74','192.168.1.75','192.168.1.76','192.168.1.41','192.168.1.16','192.168.1.32',]
ipaddrset3=['192.168.1.51','192.168.1.52','192.168.1.53','192.168.1.54','192.168.1.55',
      '192.168.1.15','192.168.1.16','192.168.1.22','192.168.1.23','192.168.1.25','192.168.1.26','192.168.1.27',
      '192.168.1.28','192.168.1.7']
hostname='192.168.8.201'
tn=telnetlib.Telnet(hostname)
print(tn.read_until(b'Username:').decode('ascii'))
tn.write(b'**********\n')
print(tn.read_until(b'Password:').decode('ascii'))
tn.write(b'************\n')
print(tn.read_until(b'>').decode('ascii'))
for ipaddr in ipaddrset1:
  telnet_dest="telnet "+ipaddr
  tn.write(telnet_dest.encode('ascii')+b'\n')
  tn.read_until(b'Password:').decode('ascii')
  tn.write(b'**********\n')
  tn.read_until(b'>').decode('ascii')
  tn.write(b'dir\n')
  tn.read_until(b'>').decode('ascii')
  fn=str(today)+"_"+str(ipaddr)+"_vrpcfg.zip \n"
  cmdli="tftp 192.168.5.33 put vrpcfg.zip " +str(fn)
  tn.write(cmdli.ede('ascii'))
  tmp=tn.read_until(b'>').decode('ascii')
  if "successfully" in tmp:
    print(str(ipaddr)+" backup successfully!")
  else:
    print(str(ipaddr)+" backup NOT successfully!")
  tn.write(b'quit\n')
  tn.read_until(b'>')
for ipaddr in ipaddrset2:
  telnet_dest="telnet "+ipaddr
  tn.write(telnet_dest.encode('ascii')+b'\n')
  tn.read_until(b'Password:').decode('ascii')
  tn.write(b'**********\n')
  tn.read_until(b'>').decode('ascii')
  tn.write(b'dir\n')
  tn.read_until(b'>').decode('ascii')
  fn=str(today)+"_"+str(ipaddr)+"_startup.cfg \n"
  cmdli="tftp 192.168.5.33 put startup.cfg " +str(fn)
  tn.write(cmdli.encode('ascii'))
  tmp=tn.read_until(b'>').decode('ascii')
  if "successfully" in tmp:
    print(str(ipaddr)+" backup successfully!")
  else:
    print(str(ipaddr)+" backup NOT successfully!")
  tn.write(b'quit\n')
  tn.read_until(b'>')
for ipaddr in ipaddrset3:
  telnet_dest="telnet "+ipaddr
  tn.write(telnet_dest.encode('ascii')+b'\n')
  tn.read_until(b'Password:').decode('ascii')
  tn.write(b'************\n')
  tn.read_until(b'>').decode('ascii')
  tn.write(b'dir\n')
  tn.read_until(b'>').decode('ascii')
  fn=str(today)+"_"+str(ipaddr)+"_startup.cfg \n"
  cmdli="tftp 192.168.5.33 put startup.cfg " +str(fn)
  tn.write(cmdli.encode('ascii'))
  tmp=tn.read_until(b'>').decode('ascii')
  if "successfully" in tmp:
    print(str(ipaddr)+" backup successfully!")
  else:
    print(str(ipaddr)+" backup NOT successfully!")
  tn.write(b'quit\n')
  tn.read_until(b'>')

tn.write(b'exit\n')
tn.close()
相关文章
|
4天前
|
网络性能优化 网络虚拟化 网络架构
配置接口限速示例(盒式交换机)
接口限速简介 接口限速对通过整个端口的全部报文流量速率进行限制,不对具体流量进行区分,可以实现给某个接口分配固定的带宽,控制方式单一,配置简单。 入方向与出方向的接口限速属于并列关系,用户可以根据需要同时配置,也可以单独配置。
|
1天前
|
C++ Python
vs配置python环境 - 蓝易云
以上就是在Visual Studio中配置Python环境的步骤,希望对你有所帮助。
7 1
|
1天前
|
机器学习/深度学习 Java 数据挖掘
selenium的配置与基本使用(1),2024年最新网易Python面试必问
selenium的配置与基本使用(1),2024年最新网易Python面试必问
|
2天前
|
前端开发 Unix Linux
Sublime Text 3配置 Python 开发环境
【5月更文挑战第7天】本篇 Huazie 介绍了 Sublime Text 3 配置 Python 开发环境的相关内容,感兴趣的朋友赶紧配置起来,有任何问题可以随时评论区沟通。
22 1
Sublime Text 3配置 Python 开发环境
|
4天前
盒式交换机堆叠配置
盒式交换机堆叠配置
6 0
|
4天前
|
网络协议 网络性能优化 网络虚拟化
【亮剑】介绍了华为三层交换机的配置命令,包括基本配置(系统启动、接口配置、基础设置)、路由协议(OSPF、BGP)配置和高级功能(VLAN、ACL、QoS)配置
【4月更文挑战第30天】本文介绍了华为三层交换机的配置命令,包括基本配置(系统启动、接口配置、基础设置)、路由协议(OSPF、BGP)配置和高级功能(VLAN、ACL、QoS)配置。通过这些命令,网络工程师可以有效地管理设备、优化网络性能并解决网络问题。熟练掌握这些命令对于提升网络运行效率至关重要。
|
4天前
|
网络安全 数据安全/隐私保护 Python
【专栏】如何使用 Python 编写脚本批量备份交换机配置
【4月更文挑战第28天】本文介绍如何使用 Python 编写脚本批量备份交换机配置。主要步骤包括了解交换机命令和接口,安装 `paramiko` 库,获取交换机登录信息。脚本实现分为建立 SSH 连接,执行备份命令并保存结果。示例脚本中,定义了 `backup_switch_config` 函数遍历交换机列表进行备份,每次备份后等待一段时间。此方法能有效提高网络管理效率。
|
4天前
|
运维 监控 Serverless
Serverless 应用引擎产品使用之阿里函数计算中在自定义环境下用debian10运行django,用官方层的python3.9,配置好环境变量后发现自定义层的django找不到了如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
23 3
|
4天前
|
网络虚拟化
交换机配置
交换机配置
|
4天前
|
IDE Linux 开发工具
Python安装与配置
Python安装与配置
31 0