使用python telnetlib批量备份交换机配置的方法

使用了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批量备份交换机配置的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 使用python Telnet远程登录执行程序的方法

    如下所示: # -*-coding:utf-8 -*- def do_telnet(Host, username, password, finish, commands): import telnetlib '''''Telnet远程登录:Windows客户端连接Linux服务器''' # 连接Telnet服务器 tn = telnetlib.Telnet(Host, port=23, timeout=10) tn.set_debuglevel(2) # 输入登录用户名 tn.read_unti

  • Python实现配置文件备份的方法

    本文实例讲述了Python实现配置文件备份的方法.分享给大家供大家参考.具体如下: 这里平台为Linux: #!/usr/bin/python #Author:gdlinjianying@qq.com import os import time source = ['/etc/sysconfig/network-scripts', '/etc/sysconfig/network', '/etc/resolv.conf'] target_dir = '/opt/' target = target_

  • Python实现telnet服务器的方法

    本文实例讲述了Python实现telnet服务器的方法.分享给大家供大家参考.具体实现方法如下: import threading class myThread(threading.Thread): def __init__(self,conn,add): threading.Thread.__init__(self) self.inputstr = '' self.connection=conn self.address=add def run(self): ii=0 while True:

  • python实现数通设备tftp备份配置文件示例

    环境:[wind2003[open Tftp server] + virtualbox:ubuntn10 server]tftp : Open TFTP Server   ubuntn  python + pyexpect 采用虚拟机原因: pyexpect 不支持windows 注:原打算采用secrueCrt 脚本编写,因实践中发现没有使用linux下pexpect易用,灵活  ,之前习惯使用expect,因tcl[语法]没有python易用.易维护 编写些程序原因:最近出了比较严重故障:因

  • 使用python telnetlib批量备份交换机配置的方法

    使用了telnetlib模块,首先登录到交换机,列出并获取配置文件的名称,然后通过tftp协议将配置文件传输到文件服务器上,为避免配置文件覆盖,将备份的配置文件名称统一加入日期以作区分. 1. 登录方式和口令有好几种,比较懒惰,通过不同列表以做区分,如果每个交换机口令都不相同的话,就需要额外处理了. 2. 交换机的配置文件也有多种类型,也是通过列表进行区分. 3. 有些交换机支持ftp和sftp,但测试发现有些虽然有相应的客户端命令,但传输总有问题.也不能将每个交换机都配置为ftp服务器,不安全

  • python代码 FTP备份交换机配置脚本实例解析

    代码如下 #!/bin/python #coding=utf-8 #python-version=2.75 #使用python2 from ftplib import FTP #引用ftplib库中的FTP功能模块,进行ftp下载使用 import time #引用time模块 import os #引用os模块 """ 使用字典,定义交换机主机,一个字典包含多个键 ,一个键使用一个列表,包含多个主机地址按照实际情况定义 """ dic = {

  • Python实现批量转换文件编码的方法

    本文实例讲述了Python实现批量转换文件编码的方法.分享给大家供大家参考.具体如下: 这里将某个目录下的所有文件从一种编码转换为另一种编码,然后保存 import os import shutil def match(config,fullpath,type): flag=False if type == 'exclude': for item in config['src']['exclude']: if fullpath.startswith(config['src']['path']+o

  • python实现批量改文件名称的方法

    本文实例讲述了python实现批量改文件名称的方法.分享给大家供大家参考.具体分析如下: 发现python中提供了大量的模块函数,有时候一些系统操作在python中非常简单 下面的文件关键是要放到要操作的目录下, 下面是把当前目录下的图片批量命名,从00开始,其中小于10 的我们在名称前面补零,或者可以利用os设置路径 #-*- coding: UTF-8 -*- import os filenames = os.listdir(os.getcwd()) for name in filename

  • python scp 批量同步文件的实现方法

    该脚本用于将源主机列表路径下的所有文件同步于目标主机的/tmp下面 #!/usr/bin/python # -*- coding:utf-8 -*- import pexpect import os import os.path src_path = ['/tmp/', '/opt/', '/root/'] dest_host = "192.168.143.201" dest_path = "/tmp" for path in src_path: file_list

  • python实现批量修改服务器密码的方法

    求:机房.线上有多台主机,为了保障安全,需要定期修改密码.若手动修改,费时费力易出错. 程序应该满足如下需求 : 1.在现有的excel密码表格,在最后一个字段后面生成新的密码,另存为一个新的excel密码文件 2.根据新的excel密码文件,更新服务器密码,将更新后的结果保存到另外一个excel文件. a.原始excel文件字段格式,最后一个字段为原始密码 IP USER PORT pwd b.生成新的密码文件字段格式,最后一个字段为更新密码 IP USER PORT pwd pwd20180

  • 使用virtualenv创建Python环境及PyQT5环境配置的方法

    一.写在前面 从学 Python 的第一天起,我就知道了使用 pip 命令来安装包,从学习爬虫到学习 Web 开发,安装的库越来越多,从 requests 到 lxml,从 Django 到 Flask,各种各样的库都处在一个 Python 环境之中. 这种做法对于我这种懒人来说是再适合不过的了,但是这样也是会有问题的.第一个问题在于 Pycharm 的加载速度变得慢了,因为要导入太多包了,而其中很多包对于很多程序来说根本用不上.第二个问题在于很多模块之间是有版本要求的,都需要特定的版本才能执行

  • python数据批量写入ScrolledText的优化方法

    如下所示: for i in data[::-1]: self.maintenance_text.insert(tk.END, str(i['payload']) + '\n\n') self.maintenance_text.see(tk.END) 改为: str_data = '\n\n'.join([str(i) for i in data[::-1]]) self.maintenance_text.insert(tk.END, str_data) self.maintenance_tex

  • python+ffmpeg批量去视频开头的方法

    用来批量切割视频的开头部分,比如去掉一部电视剧的序幕,看着难受不说数量还很多,很菜鸟的一篇,毕竟我也是一个菜鸟,首先要有ffmpeg这个软件,安装什么的就不说了,网上一搜就出来了,直接给代码,以后丢了也不怕.自用的,没写那么多 #/usr/bin/python #coding:u8 import os pp=os.getcwd() path=''#视频所在目录 time=''#格式为hh:mm:ss[.xxx]的形式 for i in os.listdir(path): os.system("&

  • 浅析python 定时拆分备份 nginx 日志的方法

    一.背景: nginx 的log 不会自动按天备份,而且记录时间格式不统一,此程序专门解决这两个问题: 二.windows 部署方式 1.在 nginx 目录,创建一个 nginx_logs_backup.bat 文件:文件内容如下 python nginx_logs_splter.py --nginxConf=nginx.conf --nginxDir=xxxxx --logPrefixs=access,error 2.在定时任务中加一个定时任务,调用这个 bat 文件: 2.1 开始-程序-

随机推荐