代码拉取完成,页面将自动刷新
#!/usr/bin/python3
from telnetlib import Telnet
import re
import time
def clearasa(username, password, rackno, asano):
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 = 2006 + int(asano)
tn = Telnet(rackip, portno)
#正则表达式匹配各种状态
# yesorno = re.compile(r'.*Please answer.*')
exc = re.compile(r'.*>.*')
configure = re.compile(r'.*config.*')
priv = re.compile(r'.*#.*')
clearconfig = re.compile(r'ciscoasa(\(config\))?#')
#多次回车确认状态
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()
time.sleep(1)
tn.write(b'\n')
tn.write(b'\n')
tn.write(b'\32\n')
time.sleep(1)
tn.write(b'\n')
tn.write(b'\n')
tn.write(b'\32\n')
#获取位置状态信息
rackreply = tn.expect([],timeout=1)[2].decode().strip()
#基于状态采取行为
while True:
if configure.match(rackreply):
#全局配置模式
tn.write(b'\n')
time.sleep(2)
print('match configure')
break
elif exc.match(rackreply):
#执行模式
tn.write(b'\n')
tn.write(b'en\n')
time.sleep(1)
tn.write(b'\n')
tn.write(b'configure terminal\n')
tn.write(b'n\n')
print('match exec')
break
elif priv.match(rackreply):
#特权模式
tn.write(b'\n')
time.sleep(2)
tn.write(b'\n')
tn.write(b'configure terminal\n')
tn.write(b'n\n')
print('match priv')
break
else:
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'\r\n')
tn.read_very_eager()
print('next')
rackreply = tn.expect([],timeout=1)[2].decode().strip()
#继续回车确认状态
tn.write(b'\n\n\n')
tn.read_very_eager()
#获取状态信息
rackreply = tn.expect([],timeout=1)[2].decode().strip()
if clearconfig.match(rackreply):
#如果是空配置,就保持此状态
tn.close()
print(tn.read_all())
else:
#如果有配置就清空配置重启
tn.write(b'configure terminal\n')
tn.write(b'changeto system\n\n\n')
tn.write(b'clear config all\n\n\n')
tn.close()
print(tn.read_all())
print(rackno + ': ASA' + str(asano) + 'cleared')
if __name__ == "__main__":
clearasa('labtest', 'Cisc0123', 'BJRack01', 1)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。