Python configures Cisco network devices with Telnet

Source: Internet
Author: User
Tags python script

!! Using the Telnetlib Library
1 topologies


R4, R5 are not routed to 1.1.1.1 and 2.2.2.2 when the script is not running:


The configuration document is placed in the same directory as the Python script:


After running the Python script:
-process=image/watermark,size_16,text_qduxq1rp5y2a5a6i,color_ffffff,t_100,g_se,x_10,y_10,shadow_90,type_ zmfuz3pozw5nagvpdgk= "alt=" Python configures Cisco network devices via Telnet/>


Success!

#conf.py 文件import timefrom telnetlib import Telnetdef cfg(addr,user,pwd,secret,conf):        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‘)        time.sleep(1)        tn.write(‘conf t\n‘)        time.sleep(1)        confp = open(conf,‘r‘)        for cmd in confp:          tn.write(cmd)    #应为读一行的时候已经有换行符了,所以这里就不添加+‘\n‘了          print(cmd)       #用于查看读取的命令          time.sleep(1)    #建议每条命令都休眠一下,不然可能配置不了        confp.close()if __name__ == "__main__":        fp = open(‘./ip.txt‘,‘r‘)    #如果有多台主机要配置同样的命令的话,可以将主机IP都放在一个文档中        for ip in fp:          print("configuring "+ip.strip())          conf = cfg(ip.strip(),‘cisco‘,‘cisco‘,‘cisco‘,‘./conf.txt‘)          print(ip.strip()+‘ was finished!‘)        print(‘done!‘)        fp.close()


#ip. txt//Add as needed
172.16.1.4
172.16.2.5


#conf. txt//Add as needed
IP Route 1.1.1.1 255.255.255.255 f0/0
IP Route 2.2.2.2 255.255.255.255 f0/0
Do write

Python configures Cisco network devices with Telnet

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.