1 Star 0 Fork 46

北冥有鱼在飞/qytang_Python

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
2016.02.06 copy sw 4.54 KB
Copy Edit Raw Blame History
现任明教教主-乾颐堂 authored 2016-02-06 08:54 . new file
#!/usr/bin/python3
from telnetlib import Telnet
import re
import time
def copysw(username, password, rackno, verno, swno):
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 = 2010 + int(swno)
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'Router(#|>)')
passwd = re.compile(r'Password: ')
swsig = re.compile(r'^Current configuration.*')
#多次回车确认状态
tn.write(b'\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.read_very_eager()
time.sleep(1)
tn.write(b'\r\n')
tn.read_very_eager()
time.sleep(1)
tn.write(b'\n')
#获取位置状态信息
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:
tn.write(b'\n')
yesornoreply = tn.expect([],timeout=1)[2].decode().strip()
if clearconfig.match(yesornoreply):
break
else:
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'\n')
tn.write(b'no\n\n\n')
tn.write(b'\n')
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
time.sleep(1)
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
tn.write(b'\n')
break
elif configure.match(rackreply):
#全局配置模式
tn.write(b'\n')
tn.write(b'end\n')
time.sleep(1)
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'\n')
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
time.sleep(1)
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'\n')
time.sleep(1)
# print('match exec')
break
elif priv.match(rackreply):
#特权模式
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'\n')
time.sleep(1)
# print('match priv')
break
else:
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'\n')
time.sleep(1)
# print(rackno + ' Router ' + str(routerno) + 'next')
#继续回车确认状态
tn.write(b'\r\n')
time.sleep(1)
tn.write(b'\n')
#拷贝配置
tn.write(b'\r\n')
tn.write(b'end\n')
time.sleep(1)
tn.write(b'end\n')
time.sleep(1)
tn.write(b'\n')
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
time.sleep(1)
tn.write(b'enable\n')
time.sleep(1)
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
tn.write(b'cisco\n\n')
time.sleep(1)
tn.write(b'\r\n')
tn.write(b'\n')
tn.write(b'terminal length 0\n\n')
time.sleep(1)
tn.write(b'terminal length 0\n\n')
time.sleep(1)
tn.write(b'\r\n')
tn.write(b'\n')
time.sleep(1)
tn.read_very_eager()
rackreply = tn.expect([],timeout=5)[2].decode().strip()
tn.write(b'show run\n')
rackreplyswt = tn.expect([],timeout=20)[2].decode().strip()
# configfile = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'SW' + swno + '.txt'
# config = open(configfile, 'w')
# config.write(rackreply)
# config.close()
# tn.close()
configfiletmp = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'SW' + swno + 'tmp' + '.txt'
configtmp = open(configfiletmp, 'w')
configtmp.write(rackreplyswt)
configtmp.close()
configtmpr = open(configfiletmp, 'r')
rackreplylines = configtmpr.readlines()
location = 1
i = 1
for line in rackreplylines:
i = i + 1
if swsig.match(line.strip()):
location = i
swconfiglst = rackreplylines[location:]
swconfig = ''.join(str(e) for e in swconfiglst)
configfile = '/python/cgi-bin/tmpconfig/' + verno + '/' + 'SW' + swno + '.txt'
config = open(configfile, 'w')
config.write(swconfig)
config.close()
tn.close()
if __name__ == "__main__":
copysw('labtest', 'Cisc0123', 'BJRack02', 've', '3')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/grant5/qytang_Python.git
git@gitee.com:grant5/qytang_Python.git
grant5
qytang_Python
qytang_Python
master

Search