Python通过telnet备份网络设备的配置-成都快上网建站

Python通过telnet备份网络设备的配置

本实验使用了telnetlib 库
一、拓扑
Python 通过telnet 备份网络设备的配置
 
二、环境
    Python 2、Cisco-Router、Kali-Linux
 
三、配置
       !!脚本和IP地址文件都在同一个目录下
       3.1、IP 地址
Python 通过telnet 备份网络设备的配置

多端合一响应式网站建设:PC+平板+手机,同一后台修改数据多端同步更新提交您的需求,获取网站建设与营销策划方案报价,我们会在1小时内与您联系!

       
       3.2、Python 备份脚本

#backup.py
import time
from telnetlib import Telnet

def tel(addr,user,pwd,secret):
        tn = Telnet(addr)
        tn.write(user+'\n')
        tn.write(pwd+'\n')
        tn.write('enable\n')
        tn.write(secret+'\n')
        tn.write('terminal length 0\n')#将show run的内容一次性全部显示完
        time.sleep(1)
        tn.write('show run\n')
        time.sleep(1)
        rsp = tn.expect([],timeout=1)[2]
        return rsp

if __name__ == "__main__":
        fp = open('./ip.txt','r')
        for ip in fp:
          print("backing up "+ip.strip())
          conf = tel(ip.strip(),'cisco','cisco','cisco') #第一个cisco 是账户,第二个Cisco是密码,第三个Cisco是enable密码
          print(ip.strip()+' was finished!')
          print(conf)#这里是用于查看函数返回的内容,可以删除
          fw = open(ip.strip(),'w')#每台主机的配置以IP地址为文件名,建议先使用OS模块创建一个目录,然后将所有配置放到目录下
          fw.write(conf)
          fw.close()
        print('done!')
        fp.close()

 
   效果如下:
   Python 通过telnet 备份网络设备的配置


文章名称:Python通过telnet备份网络设备的配置
当前网址:http://kswjz.com/article/ieoeoe.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流