Python backs up configuration of network devices via Telnet

Source: Internet
Author: User

This experiment uses the Telnetlib library
First, the topology


Second, the environment
Python 2, Cisco-router, Kali-linux

Third, the configuration
!! Scripts and IP address files are in the same directory
3.1. IP Address


3.2. Python Backup Script

#backup.pyimport timefrom telnetlib import Telnetdef 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 rspif __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()


The effect is as follows:

Python backs up configuration of network devices via Telnet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.