使用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 相关文章推荐
精确查找PHP WEBSHELL木马的方法(1)
Apr 12 Python
Python 时间操作例子和时间格式化参数小结
Apr 24 Python
python代码 if not x: 和 if x is not None: 和 if not x is None:使用介绍
Sep 21 Python
浅谈numpy库的常用基本操作方法
Jan 09 Python
Django实现支付宝付款和微信支付的示例代码
Jul 25 Python
Python函数的参数常见分类与用法实例详解
Mar 30 Python
Python使用LDAP做用户认证的方法
Jun 20 Python
Django打印出在数据库中执行的语句问题
Jul 25 Python
Python猴子补丁Monkey Patch用法实例解析
Mar 23 Python
Jupyter Notebook 实现正常显示中文和负号
Apr 24 Python
解决python 执行sql语句时所传参数含有单引号的问题
Jun 06 Python
python实现学生成绩测评系统
Jun 22 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实现paypal整合方法
2010/11/28 PHP
PHP程序开发范例学习之表单 获取文本框的值
2011/08/08 PHP
php文件上传的简单实例
2013/10/19 PHP
采用memcache在web集群中实现session的同步会话
2014/07/05 PHP
PHP读书笔记_运算符详解
2016/07/01 PHP
搜索附近的人PHP实现代码
2018/02/11 PHP
Jquery Ajax学习实例7 Ajax所有过程事件分析示例
2010/03/23 Javascript
jquery插件如何使用 jQuery操作Cookie插件使用介绍
2012/12/15 Javascript
有关于JS构造函数的重载和工厂方法
2013/04/07 Javascript
中文输入法不触发onkeyup事件的解决办法
2014/07/09 Javascript
asp知识整理笔记3(问答模式)
2015/09/27 Javascript
javascript中闭包(Closure)详解
2016/01/06 Javascript
AngularJS变量及过滤器Filter用法分析
2016/11/22 Javascript
JQuery异步提交表单与文件上传功能示例
2017/01/12 Javascript
捕获未处理的Promise错误方法
2017/10/13 Javascript
Vue 2.0学习笔记之Vue中的computed属性
2017/10/16 Javascript
JS实现的文件拖拽上传功能示例
2018/05/21 Javascript
nuxt 每个页面head标签内容设置方式
2020/11/05 Javascript
Python中的闭包实例详解
2014/08/29 Python
python银行系统实现源码
2019/10/25 Python
使用Rasterio读取栅格数据的实例讲解
2019/11/26 Python
Python实现鼠标自动在屏幕上随机移动功能
2020/03/14 Python
Python接口自动化测试的实现
2020/08/28 Python
CSS3绘制六边形的简单实现
2016/08/25 HTML / CSS
Volcom法国官网:美国冲浪滑板品牌
2017/05/25 全球购物
新英格兰最大的特色礼品连锁店:The Paper Store
2018/07/23 全球购物
性能服装:HYLETE
2018/08/14 全球购物
澳大利亚网上书店:QBD
2021/01/09 全球购物
枚举与#define宏的区别
2014/04/30 面试题
JS原生实现轮播图的几种方法
2021/03/23 Javascript
汽车销售顾问求职自荐信
2014/01/01 职场文书
2014年母亲节寄语
2014/05/07 职场文书
工作说明书格式
2014/07/29 职场文书
车辆安全隐患排查制度
2015/08/05 职场文书
学校标语口号大全
2015/12/26 职场文书
一文搞懂Python Sklearn库使用
2021/08/23 Python