利用Python telnetlib实现交换机配置批量备份
本文介绍如何使用Python的telnetlib模块登录交换机,获取配置文件列表,并通过TFTP协议将配置备份至服务器。为解决文件覆盖问题,脚本自动在文件名中加入日期后缀。尽管TFTP不支持目录创建,但通过命名区分仍可实现有效备份,适用于多种交换机环境的批量运维场景。
Python telnetlib备份方案概述
本方案基于Python的telnetlib模块,首先建立与交换机的Telnet连接以完成登录操作,随后列出并提取配置文件的名称。接着,利用TFTP协议将获取到的配置文件传输至文件服务器。为了防止后续备份覆盖原有文件,脚本在保存备份文件时统一追加了当前日期作为后缀,以此实现文件的区分与存储。
登录认证与配置类型处理
针对登录方式及口令的管理,文中采用了较为简便的处理策略,即通过不同的列表来区分各类设备。如果面临每个交换机口令均不相同的复杂情况,则需要在此基础上增加额外的处理逻辑以适配差异化的认证需求。
交换机的配置文件存在多种类型,本方法同样通过预设列表对这些类型进行区分和处理,确保不同类型的配置能被正确识别和备份。
传输协议选择与命名策略
虽然部分交换机支持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模块批量备份交换机配置的具体方法,旨在为读者提供参考,欢迎持续关注以获取更多技术支持。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









