您的位置:首页 > 编程语言 > Python开发

python脚本批量修改华为交换机端口配置

2015-09-02 15:53 761 查看
用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,re

ip = '你的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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息