使用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 相关文章推荐
通过mod_python配置运行在Apache上的Django框架
Jul 22 Python
Python中的条件判断语句与循环语句用法小结
Mar 21 Python
Python通过RabbitMQ服务器实现交换机功能的实例教程
Jun 29 Python
Python决策树分类算法学习
Dec 22 Python
python使用socket创建tcp服务器和客户端
Apr 12 Python
Python读取txt文件数据的方法(用于接口自动化参数化数据)
Jun 27 Python
使用Python进行体育竞技分析(预测球队成绩)
May 16 Python
如何利用Pyecharts可视化微信好友
Jul 04 Python
python3利用Axes3D库画3D模型图
Mar 25 Python
selenium框架中driver.close()和driver.quit()关闭浏览器
Dec 08 Python
python中altair可视化库实例用法
Jan 26 Python
python中的getter与setter你了解吗
Mar 24 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
成为好程序员必须避免的5个坏习惯
2014/07/04 PHP
thinkPHP3.x常量整理(预定义常量/路径常量/系统常量)
2016/05/20 PHP
php安装dblib扩展,连接mssql的具体步骤
2017/03/02 PHP
PHP微信H5支付开发实例
2018/07/25 PHP
PDO::getAttribute讲解
2019/01/28 PHP
js 动态添加标签(新增一行,其实很简单,就是几个函数的应用)
2009/03/26 Javascript
javascript 无提示关闭窗口脚本
2009/08/17 Javascript
js中的eventType事件及其浏览器支持性介绍
2013/11/29 Javascript
用原生JS获取CLASS对象(很简单实用)
2014/10/15 Javascript
JavaScript函数详解
2015/02/27 Javascript
深入学习jQuery Validate表单验证
2016/01/18 Javascript
关于function类中定义变量this的简单说明
2016/05/28 Javascript
老生常谈JQuery data方法的使用
2016/09/09 Javascript
如何给ss bash 写一个 WEB 端查看流量的页面
2017/03/23 Javascript
javascript实现文字跑马灯效果
2020/06/18 Javascript
详解template标签用法(含vue中的用法总结)
2021/01/12 Vue.js
vue form表单post请求结合Servlet实现文件上传功能
2021/01/22 Vue.js
[56:17]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第三场 8.22
2019/09/05 DOTA
从零学python系列之数据处理编程实例(二)
2014/05/22 Python
Python中return语句用法实例分析
2015/08/04 Python
Python简单删除列表中相同元素的方法示例
2017/06/12 Python
Python入门之三角函数atan2()函数详解
2017/11/08 Python
解决pyinstaller打包exe文件出现命令窗口一闪而过的问题
2018/10/31 Python
pandas 透视表中文字段排序方法
2018/11/16 Python
浅析python的优势和不足之处
2018/11/20 Python
Python Datetime模块和Calendar模块用法实例分析
2019/04/15 Python
pandas创建DataFrame的7种方法小结
2020/06/14 Python
美国最大的在线生存商店:Survival Frog
2020/12/13 全球购物
2014年小班元旦活动方案
2014/02/16 职场文书
祖国在我心中演讲稿600字
2014/05/04 职场文书
八项规定个人对照检查材料思想汇报
2014/09/25 职场文书
教师个人工作总结范文2014
2014/11/10 职场文书
幼儿园2015年度工作总结
2015/04/01 职场文书
大学生入党群众意见书
2015/06/02 职场文书
PHP中国际化的字符串排序和比较对象详解
2021/08/23 PHP
基于Python编写一个监控CPU的应用系统
2022/06/25 Python