使用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实现批量把SVG格式转成png、pdf格式的代码分享
Aug 21 Python
在Python的Flask框架中验证注册用户的Email的方法
Sep 02 Python
Python内置函数reversed()用法分析
Mar 20 Python
解决python matplotlib imshow无法显示的问题
May 24 Python
python3实现随机数
Jun 25 Python
Python-while 计算100以内奇数和的方法
Jun 11 Python
Python文件路径名的操作方法
Oct 30 Python
Python函数的默认参数设计示例详解
Dec 01 Python
使用pygame写一个古诗词填空通关游戏
Dec 03 Python
Python如何对XML 解析
Jun 28 Python
基于python实现图片转字符画代码实例
Sep 04 Python
Python+Appium新手教程
Apr 17 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
PHP自动生成后台导航网址的最佳方法
2013/08/27 PHP
Laravel5中contracts详解
2015/03/02 PHP
PHP中的输出echo、print、printf、sprintf、print_r和var_dump的示例代码
2020/12/01 PHP
简单的JS多重继承示例
2008/03/13 Javascript
Confirmer JQuery确认对话框组件
2010/06/09 Javascript
JavaScript Date对象 日期获取函数
2010/12/19 Javascript
15个jquery常用方法、小技巧分享
2015/01/13 Javascript
jQuery实现的漂亮表单效果代码
2015/08/18 Javascript
node.js cookie-parser之parser.js
2016/06/06 Javascript
angularjs+bootstrap菜单的使用示例代码
2017/03/07 Javascript
详解Angular之constructor和ngOnInit差异及适用场景
2017/06/22 Javascript
微信小程序input框中加入小图标的实现方法
2018/06/19 Javascript
vue-cli 3.x 配置Axios(proxyTable)跨域代理方法
2018/09/19 Javascript
vuejs router history 配置到iis的方法
2018/09/20 Javascript
Node.js原生api搭建web服务器的方法步骤
2019/02/15 Javascript
JS实现继承的几种常用方式示例
2019/06/22 Javascript
layui table表格数据的新增,修改,删除,查询,双击获取行数据方式
2019/11/14 Javascript
vue通过v-html指令渲染的富文本无法修改样式的解决方案
2020/05/20 Javascript
Python 正则表达式(转义问题)
2014/12/15 Python
Python实现针对中文排序的方法
2017/05/09 Python
基于python时间处理方法(详解)
2017/08/14 Python
Python使用回溯法子集树模板解决迷宫问题示例
2017/09/01 Python
Python实现pdf文档转txt的方法示例
2018/01/19 Python
python批量替换多文件字符串问题详解
2018/04/22 Python
浅谈Python traceback的优雅处理
2018/08/31 Python
Python字符串匹配之6种方法的使用详解
2019/04/08 Python
美国眼镜网:GlassesUSA
2017/09/07 全球购物
耐克中国官方商城:Nike中国
2018/10/18 全球购物
CAT鞋加拿大官网:CAT Footwear加拿大
2020/08/05 全球购物
以下的初始化有什么区别
2013/12/16 面试题
员工薪酬福利制度
2014/01/17 职场文书
公司成本主管岗位责任制
2014/02/21 职场文书
房展策划方案
2014/06/07 职场文书
中学生2014国庆节演讲稿:不屈的民族
2014/09/21 职场文书
学院党的群众路线教育实践活动第一阶段情况汇报
2014/10/25 职场文书
如何用PHP实现分布算法之一致性哈希算法
2021/05/26 PHP