使用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自动扫雷实现方法
Jul 25 Python
Python简单的制作图片验证码实例
May 31 Python
Django使用Mysql数据库已经存在的数据表方法
May 27 Python
selenium+python实现自动化登录的方法
Sep 04 Python
python3利用Socket实现通信的方法示例
May 06 Python
深入浅析Python中的迭代器
Jun 04 Python
python多维数组分位数的求取方式
Mar 03 Python
selenium+python配置chrome浏览器的选项的实现
Mar 18 Python
jupyter notebook快速入门及使用详解
Nov 13 Python
基于Python的身份证验证识别和数据处理详解
Nov 14 Python
新手必备Python开发环境搭建教程
May 28 Python
python的列表生成式,生成器和generator对象你了解吗
Mar 16 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中利用post传递字符串重定向的实现代码
2011/04/21 PHP
php多文件上传功能实现原理及代码
2013/04/18 PHP
php抓取页面的几种方法详解
2013/06/17 PHP
php对数组排序代码分享
2014/02/24 PHP
php获取域名的google收录示例
2014/03/24 PHP
8个PHP数组面试题
2015/06/23 PHP
Avengerls vs KG BO3 第三场2.18
2021/03/10 DOTA
offsetHeight在OnLoad中获取为0的现象
2013/07/22 Javascript
火狐textarea输入法的bug的触发及解决
2013/07/24 Javascript
js的匿名函数使用介绍
2013/12/11 Javascript
利用window.name实现windowStorage代码分享
2014/01/02 Javascript
更快的异步执行(setTimeout多浏览器)
2014/08/12 Javascript
JavaScript 程序错误Cannot use 'in' operator to search的解决方法
2017/07/10 Javascript
详解vue-cli@2.x项目迁移日志
2019/06/06 Javascript
JQuery中的常用事件、对象属性与使用方法分析
2019/12/23 jQuery
js实现文章目录索引导航(table of content)
2020/05/10 Javascript
Vue3新特性之在Composition API中使用CSS Modules
2020/07/13 Javascript
解决vue组件没显示,没起作用,没报错,但该显示的组件没显示问题
2020/09/02 Javascript
python中使用sys模板和logging模块获取行号和函数名的方法
2014/04/15 Python
Python下载指定页面上图片的方法
2016/05/12 Python
使用python实现生成用户信息
2017/03/20 Python
python win32 简单操作方法
2017/05/25 Python
使用python搭建服务器并实现Android端与之通信的方法
2019/06/28 Python
python常见字符串处理函数与用法汇总
2019/10/30 Python
python函数装饰器之带参数的函数和带参数的装饰器用法示例
2019/11/06 Python
3种适用于Python的疯狂秘密武器及原因解析
2020/04/29 Python
Agoda台湾官网:国内外订房2折起
2018/03/20 全球购物
医学生求职自荐信
2013/10/25 职场文书
送货司机岗位职责
2013/12/11 职场文书
社区健康教育实施方案
2014/03/18 职场文书
党员群众路线对照检查材料
2014/08/31 职场文书
数学考试作弊检讨书300字
2015/02/16 职场文书
优秀团员个人总结
2015/02/26 职场文书
八月迷情观后感
2015/06/11 职场文书
关于军训的感想
2015/08/07 职场文书
CSS3 制作的彩虹按钮样式
2021/04/11 HTML / CSS