使用python telnetlib批量备份交换机配置的方法


Posted in Python onJuly 25, 2019

使用了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()

以上这篇使用python telnetlib批量备份交换机配置的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中用函数作为返回值和实现闭包的教程
Apr 27 Python
python中尾递归用法实例详解
Apr 28 Python
浅析Python中的join()方法的使用
May 19 Python
Python中super关键字用法实例分析
May 28 Python
Python栈算法的实现与简单应用示例
Nov 01 Python
python函数式编程学习之yield表达式形式详解
Mar 25 Python
python模拟表单提交登录图书馆
Apr 27 Python
python pycharm最新版本激活码(永久有效)附python安装教程
Sep 18 Python
Python通过VGG16模型实现图像风格转换操作详解
Jan 16 Python
Python matplotlib实时画图案例
Apr 23 Python
Python 数据的累加与统计的示例代码
Aug 03 Python
Python测试框架:pytest学习笔记
Oct 20 Python
python找出因数与质因数的方法
Jul 25 #Python
HTML的form表单和django的form表单
Jul 25 #Python
Python3 批量扫描端口的例子
Jul 25 #Python
python3 批量获取对应端口服务的实例
Jul 25 #Python
Python实现微信小程序支付功能
Jul 25 #Python
Form表单及django的form表单的补充
Jul 25 #Python
python实现切割url得到域名、协议、主机名等各个字段的例子
Jul 25 #Python
You might like
destoon设置自定义搜索的方法
2014/06/21 PHP
php查找字符串出现次数的方法
2014/12/01 PHP
PHP设计模式之装饰者模式代码实例
2015/05/11 PHP
php实现微信企业号支付个人的方法详解
2017/07/26 PHP
动手学习无线电
2021/03/10 无线电
Prototype Selector对象学习
2009/07/23 Javascript
鼠标悬浮显示二级菜单效果的jquery实现
2014/10/29 Javascript
jquery 设置style:display的方法
2015/01/29 Javascript
js实现浏览器窗口大小被改变时触发事件的方法
2015/02/02 Javascript
js实现遍历含有input的table实例
2015/12/07 Javascript
Bootstrap3学习笔记(三)之表格
2016/05/20 Javascript
详谈JavaScript的闭包及应用
2017/01/17 Javascript
浅谈React碰到v-if
2018/11/04 Javascript
Node.js实现简单的爬取的示例代码
2019/06/25 Javascript
原生js拖拽实现图形伸缩效果
2020/02/10 Javascript
python del()函数用法
2013/03/24 Python
python进阶教程之循环对象
2014/08/30 Python
python通过线程实现定时器timer的方法
2015/03/16 Python
Python语言实现机器学习的K-近邻算法
2015/06/11 Python
python日志记录模块实例及改进
2017/02/12 Python
Python计时相关操作详解【time,datetime】
2017/05/26 Python
详解python3中的真值测试
2018/08/13 Python
python+splinter实现12306网站刷票并自动购票流程
2018/09/25 Python
python实现几种归一化方法(Normalization Method)
2019/07/31 Python
在Pytorch中使用Mask R-CNN进行实例分割操作
2020/06/24 Python
Under Armour安德玛法国官网:美国高端运动科技品牌
2018/06/29 全球购物
Bibloo荷兰:女士、男士和儿童的服装、鞋子和配饰
2019/02/25 全球购物
工作会议方案
2014/05/21 职场文书
2014年无财产无子女离婚协议书范本
2014/10/09 职场文书
研究生简历自我评
2015/03/11 职场文书
会议简报格式范文
2015/07/20 职场文书
Apache Calcite 实现方言转换的代码
2021/04/24 Servers
JavaScript实现简单计时器
2021/06/22 Javascript
详解Golang如何优雅的终止一个服务
2022/03/21 Golang
解决MySQL Varchar 类型尾部空格的问题
2022/04/06 MySQL
Nginx流量拷贝ngx_http_mirror_module模块使用方法详解
2022/04/07 Servers