代码拉取完成,页面将自动刷新
#!/usr/bin/python3
# -*- coding=utf-8 -*-
from telnetlib import Telnet
import re
import time
def copyasa(username, password, rackno, verno, asano, ctxname = 'sys'):
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\))?#')
asaver = re.compile(r'^ASA Version .*')
#多次回车确认状态
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()
tn.write(b'configure terminal\n\n')
time.sleep(1)
tn.write(b'configure terminal\n\n')
time.sleep(1)
tn.write(b'terminal pager 0\n\n')
time.sleep(1)
tn.write(b'terminal pager 0\n\n')
time.sleep(1)
tn.write(b'changeto system\n\n\n')
time.sleep(1)
tn.write(b'wr mem all\n')
time.sleep(2)
rackreply = tn.expect([],timeout=2)[2].decode().strip()
if ctxname == 'sys':
tn.write(b'\n')
time.sleep(2)
tn.write(b'more system:running-config\n')
rackreplyasat = tn.expect([],timeout=15)[2].decode().strip()
configfiletmp = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + 'tmp' + '.txt'
configtmp = open(configfiletmp, 'w')
configtmp.write(rackreplyasat)
configtmp.close()
configtmpr = open(configfiletmp, 'r')
rackreplylines = configtmpr.readlines()
location = 1
i = 1
for line in rackreplylines:
i = i + 1
if asaver.match(line.strip()):
location = i
asaconfiglst = rackreplylines[location:]
asaconfig = ''.join(str(e) for e in asaconfiglst)
configfile = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + '.txt'
config = open(configfile, 'w')
config.write(asaconfig)
config.close()
tn.close()
else:
tn.write(b'\n')
time.sleep(2)
tn.write(b'more system:running-config\n')
rackreplyasat = tn.expect([],timeout=15)[2].decode().strip()
configfiletmp = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + 'tmp' + '.txt'
configtmp = open(configfiletmp, 'w')
configtmp.write(rackreplyasat)
configtmp.close()
configtmpr = open(configfiletmp, 'r')
rackreplylines = configtmpr.readlines()
location = 1
i = 1
for line in rackreplylines:
i = i + 1
if asaver.match(line.strip()):
location = i
asaconfiglst = rackreplylines[location:]
asaconfig = ''.join(str(e) for e in asaconfiglst)
configfile = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + '.txt'
config = open(configfile, 'w')
config.write(asaconfig)
for ctx in ctxname:
tn.write(b'\n')
time.sleep(2)
cmd = ('more flash:/%s.cfg\n' % ctx).encode()
# print(cmd)
tn.write(cmd)
rackreplyasat = tn.expect([],timeout=15)[2].decode().strip()
# print(rackreplyasat)
configfiletmp = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + ctx + 'tmp' + '.txt'
configtmp = open(configfiletmp, 'w')
configtmp.write(rackreplyasat)
configtmp.close()
configtmpr = open(configfiletmp, 'r')
rackreplylines = configtmpr.readlines()
location = 1
i = 1
for line in rackreplylines:
i = i + 1
if asaver.match(line.strip()):
location = i
asaconfiglst = rackreplylines[location:]
asaconfig = ''.join(str(e) for e in asaconfiglst)
configfile = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'ASA' + asano + ctx + '.txt'
config = open(configfile, 'w')
config.write(asaconfig)
config.close()
tn.close()
if __name__ == "__main__":
copyasa('labtest', 'Cisc0123', 'BJRack03', 'v12', '1', ['Admin', 'C1', 'C2'])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。