使用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程序设计入门(1)基本语法简介
Jun 13 Python
Python升级导致yum、pip报错的解决方法
Sep 06 Python
Python实现利用163邮箱远程关电脑脚本
Feb 22 Python
在PyCharm环境中使用Jupyter Notebook的两种方法总结
May 24 Python
Python将list中的string批量转化成int/float的方法
Jun 26 Python
python求最大连续子数组的和
Jul 07 Python
Pandas时间序列:重采样及频率转换方式
Dec 26 Python
pytorch方法测试详解——归一化(BatchNorm2d)
Jan 15 Python
python能做哪方面的工作
Jun 15 Python
python如何实现递归转非递归
Feb 25 Python
Python djanjo之csrf防跨站攻击实验过程
May 14 Python
python 爬取华为应用市场评论
May 29 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
SONY SRF-M100的电路分析
2021/03/02 无线电
ThinkPHP行为扩展Behavior应用实例详解
2014/07/22 PHP
PHP使用PDO连接ACCESS数据库
2015/03/05 PHP
PHP中把对象转换为关联数组代码分享
2015/04/09 PHP
PHP实现表单提交时去除斜杠的方法
2016/12/26 PHP
php处理静态页面:页面设置缓存时间实例
2017/06/22 PHP
PHP实现实时生成并下载超大数据量的EXCEL文件详解
2017/10/23 PHP
js下获取div中的数据的原理分析
2010/04/07 Javascript
HTML5附件拖拽上传drop & google.gears实现代码
2011/04/28 Javascript
提高javascript效率 一次判断,而不要次次判断
2012/03/30 Javascript
js控制输入框获得和失去焦点时状态显示的方法
2015/01/30 Javascript
超漂亮的Bootstrap 富文本编辑器summernote
2016/04/05 Javascript
jQuery图片加载显示loading效果
2016/11/04 Javascript
jQuery实现select模糊查询(反射机制)
2017/01/14 Javascript
jQuery Mobile漏洞会有跨站脚本攻击风险
2017/02/12 Javascript
Easyui ueditor 整合解决不能编辑的问题(推荐)
2017/06/25 Javascript
浅谈react.js中实现tab吸顶效果的问题
2017/09/06 Javascript
vue todo-list组件发布到npm上的方法
2018/04/04 Javascript
Vue.js获取手机系统型号、版本、浏览器类型的示例代码
2020/05/10 Javascript
vue实现登录功能
2020/12/31 Vue.js
Python挑选文件夹里宽大于300图片的方法
2015/03/05 Python
Python实现线程池代码分享
2015/06/21 Python
Python实现分割文件及合并文件的方法
2015/07/10 Python
对Python 数组的切片操作详解
2018/07/02 Python
对python3新增的byte类型详解
2018/12/04 Python
Python reshape的用法及多个二维数组合并为三维数组的实例
2020/02/07 Python
解决Python发送Http请求时,中文乱码的问题
2020/04/30 Python
澳大利亚工具仓库:Tools Warehouse
2018/10/15 全球购物
五年级语文教学反思
2014/01/30 职场文书
高一学生期末评语
2014/04/25 职场文书
2015年上半年计生工作总结
2015/03/30 职场文书
大学迎新生欢迎词
2015/09/29 职场文书
长辈生日祝福语大全(72句)
2019/08/09 职场文书
温馨祝福晨语:美丽的一天从我的问候开始
2019/11/28 职场文书
vue基于Teleport实现Modal组件
2021/05/31 Vue.js
浅谈Python数学建模之数据导入
2021/06/23 Python