前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 通过telnet 配置思科

Python 通过telnet 配置思科

作者头像
py3study
发布2020-01-07 15:15:18
8770
发布2020-01-07 15:15:18
举报
文章被收录于专栏:python3

!!使用了 telnetlib 库 1 拓扑

Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备

未运行脚本的时候,R4、R5是没有到1.1.1.1和2.2.2.2的路由的:

Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备

配置文档放在跟python脚本同一个目录下:

Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备

运行python脚本之后:

Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备
Python 通过telnet 配置思科网络设备

成功!

代码语言:javascript
复制
#conf.py 文件
import time
from telnetlib import Telnet

def 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 //按需添加 172.16.1.4 172.16.2.5

#conf.txt //按需添加 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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档