python脚本批量修改华为交换机端口配置
用python写的,主要是telnetlib库的应用,华为交换机批量修改端口配置,把已经UP的端口做一个port sec的mac sticky绑定,DOWN的端口,就clear配置信息。
使用前,先在交换机上 dis int bri,查出交换机的端口状态信息并复制到此脚本同目录下,以 IP.txt这样的格式命名的文件(比如 192.168.0.1对应 192.168.0.1.txt),以下内容记得把IP跟账号密码改成自己的。
import sys,os,telnetlib,reip = '你的IP'
txtfile = ip + '.txt'tel = telnetlib.Telnet(ip)
tel.read_until('Username:')
tel.write('你的账号'+'\n')
tel.read_until('Password:')
tel.write('你的密码'+'\n')
tel.read_until('>')
tel.write('sys'+'\n')file1 = open(txtfile)while True:a = file1.readline()
if a == '':break
int1 = a[:21]if re.search('up',a):
print int1,' up'
tel.read_until(']')
tel.write('int'+' '+ int1 +'\n')
tel.read_until(']')
tel.write('port-security enable'+'\n')
tel.read_until(']')
tel.write('port-security mac-address sticky'+'\n')else:
print int1,' down'
tel.read_until(']')
tel.write('int'+' '+ int1 +'\n')
tel.read_until(']')
tel.write('clear configuration this '+'\n')
tel.read_until('[Y/N] :')
tel.write('Y' + '\n')
tel.read_until(']')
tel.write('undo shutdown'+'\n')tel.close()
【python脚本批量修改华为交换机端口配置】转载于:https://blog.51cto.com/374721/1690839
推荐阅读
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- Python基础|Python基础 - 练习1
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Python(pathlib模块)
- 「按键精灵安卓版」关于全分辨率脚本的一些理解(非游戏app)
- python青少年编程比赛_第十一届蓝桥杯大赛青少年创意编程组比赛细则
- Python数据分析(一)(Matplotlib使用)