代码拉取完成,页面将自动刷新
同步操作将从 现任明教教主-乾颐堂/qytang_Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#!/usr/bin/python3
from telnetlib import Telnet
import re
import time
def preconfig(username, password, rackno, verno, devno):
if rackno == 'BJRack01':
rackip = '172.17.100.111'
elif rackno == 'BJRack02':
rackip = '172.17.100.112'
elif rackno == 'BJRack03':
rackip = '172.17.100.113'
portno = 2000 + devno
tn = Telnet(rackip, portno)
#正则表达式匹配各种状态
yesorno = re.compile(r'.*Please answer.*')
exc = re.compile(r'.*>')
configure = re.compile(r'.*\(config.*\)#')
priv = re.compile(r'.*#')
clearconfigrouter = re.compile(r'Router(#|>)')
clearconfigswitch = re.compile(r'Switch(#|>)')
passwd = re.compile(r'Password: ')
#多次回车确认状态
tn.write(b'\r\n')
tn.read_very_eager()
time.sleep(1)
tn.write(b'\r\n')
tn.read_very_eager()
time.sleep(1)
tn.write(b'\r\n')
tn.read_very_eager()
#devno与设备名称的对应表
if devno == 1:
devname = 'R1'
elif devno == 2:
devname = 'R2'
elif devno == 3:
devname = 'R3'
elif devno == 4:
devname = 'R4'
elif devno == 5:
devname = 'R5'
elif devno == 6:
devname = 'R6'
elif devno == 11:
devname = 'SW1'
elif devno == 12:
devname = 'SW2'
elif devno == 13:
devname = 'SW3'
elif devno == 14:
devname = 'SW4'
elif devno == 15:
devname = 'SW5'
elif devno == 16:
devname = 'SW6'
#获取位置状态信息
rackreply = tn.expect([],timeout=1)[2].decode().strip()
#基于状态采取行为
while True:
tn.write(b'\n')
rackreply = tn.expect([],timeout=1)[2].decode().strip()
if yesorno.match(rackreply):
#yes or no初始化状态
tn.write(b'no\n\n\n')
time.sleep(5)
tn.write(b'\n')
while True:
yesornoreply = tn.expect([],timeout=1)[2].decode().strip()
if clearconfig.match(yesornoreply):
break
else:
tn.write(b'\n')
print('match yes or no')
break
elif configure.match(rackreply):
#全局配置模式
tn.write(b'\n')
tn.write(b'\r\n')
tn.write(b'end\n')
time.sleep(1)
tn.write(b'\n')
tn.write(b'\r\n')
tn.write(b'end\n')
time.sleep(1)
print('match configure')
break
elif exc.match(rackreply):
#执行模式
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'en\n')
enablereply = tn.expect([],timeout=1)[2].decode().strip()
if passwd.match(enablereply):
tn.write(b'cisco\n\n')
tn.write(b'\n')
print('match exec')
break
elif priv.match(rackreply):
#特权模式
tn.write(b'\n')
time.sleep(2)
print('match priv')
break
else:
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'\n')
time.sleep(1)
print(rackno + ' ' + verno + ' ' + devname + ' next')
tn.write(b'\r\n')
tn.write(b'\n')
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
time.sleep(1)
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
time.sleep(1)
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
time.sleep(1)
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
if rackno == 'BJRack03' and devname == 'SW5':
devname = '3SW5'
preconfigfile = '/python/cgi-bin/preconfig/' + verno + '/' + devname + '.txt'
preconfig = open(preconfigfile, 'rb')
tn.write(preconfig.read())
time.sleep(5)
tn.write(b'end\n')
time.sleep(1)
tn.write(b'wr\n\n\n\n')
preconfig.close()
time.sleep(5)
if rackno == 'BJRack03' and devname == 'SW5':
devname = '3SW5'
preconfigfile = '/python/cgi-bin/preconfig/' + verno + '/' + devname + '.txt'
preconfig = open(preconfigfile, 'rb')
tn.write(preconfig.read())
time.sleep(5)
tn.write(b'end\n')
time.sleep(1)
tn.write(b'wr\n\n\n\n')
preconfig.close()
time.sleep(5)
if rackno == 'BJRack03' and devname == 'SW5':
devname = '3SW5'
preconfigfile = '/python/cgi-bin/preconfig/' + verno + '/' + devname + '.txt'
preconfig = open(preconfigfile, 'rb')
tn.write(preconfig.read())
time.sleep(5)
tn.write(b'end\n')
time.sleep(1)
tn.write(b'wr\n\n\n\n')
preconfig.close()
time.sleep(5)
# if rackno == 'BJRack03' and devname == 'SW5':
# devname = '3SW5'
# preconfigfile = '/python/cgi-bin/preconfig/' + verno + '/' + devname + '.txt'
# preconfig = open(preconfigfile, 'rb')
# tn.write(preconfig.read())
# time.sleep(1)
# tn.write(b'end\n')
# time.sleep(1)
# tn.write(b'wr\n\n\n\n')
# preconfig.close()
print(verno + ' ' + devname + ' configed')
tn.close()
print(tn.read_all())
if __name__ == "__main__":
preconfig('labtest', 'Cisc0123', 'BJRack01', 'v12', 1)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。