我有一个大的Expect脚本输出文件,其中包含设备列表及其TACACS服务器IP地址。
我想遍历该文件,并为每个设备确定它是否得到:只有新的TACACS IP地址;或新旧;或者,都没有。 (旧的只是不可能的,因为我在所有设备上添加了新功能;但是,包括未来的要求会很好......)
对于"安全"我用虚拟数据替换了实际的设备名称和IP地址。此外,"打印"取代"写"的陈述声明只是故障排除的遗留物。
Here is the script:
#!/usr/bin/python
import re
#####################################################
new1 = "tacacs-server host 1.1.1.1"
new2 = "tacacs-server host 2.2.2.2"
new1_in_block = 0
new2_in_block = 0
has_new_ip = 0
#####################################################
old1 = "tacacs-server host 2.2.2.2"
old2 = "tacacs-server host 3.3.3.3"
old3 = "tacacs-server host 4.4.4.4"
old4 = "tacacs-server host 5.5.5.5"
old5 = "tacacs-server host 6.6.6.6"
old6 = "tacacs-server host 7.7.7.7"
has_old_ip = 0
#
tacacs_servers = open ("test_block3.txt","r+")
#
complete = open ("complete_output.txt","r+")
#
not_complete = open ("not_complete_output.txt","r+")
######################################################
not_checked = open ("not_checked.txt","r+")
#####################################################
for line in tacacs_servers:
if 'show' in line:
DeviceName = (re.search(".*#",line).group())
DeviceName = DeviceName.replace('#',' ')
if 'show' not in line:
if str(new1) or str(new2) in line:
has_new_ip = 1
elif str(new1) or str(new2) not in line:
has_new_ip = 0
if 'spawn' in line:
if has_new_ip == 0:
print (DeviceName)
print ("NOT COMPLETE\n")
elif has_new_ip == 1:
print (DeviceName)
print ("COMPLETE\n")
new1= new2 = has_new_ip = 0
old1 = old2 = old3 = old4 = old5 = old6 = has_old_ip = 0
Here is test_block3.txt:
ABC-DEF#show run | include tacacs-server host
tacacs-server host 1.1.1.1
GHI-JKL#spawn ssh -q user@MN-OPQ
MN-OPQ#show run | include tacacs-server host
tacacs-server host 1.1.1.1
tacacs-server host 3.3.3.3
tacacs-server host 2.2.2.2
MN-OPQ# #spawn ssh -q user@AZXY-123
AZXY-123# show run | include tacacs-server host
AZXY-123# spawn ssh -q user@P44AR-1212
Here is the current Python Script output:
ABC-DEF
COMPLETE
MN-OPQ
COMPLETE
AZXY-123
COMPLETE
答案 0 :(得分:0)
当然不是最优雅的方式,但是:
import re
f=open('list.txt','r')
devices_list={}
device_name=None
old_ip_list=['1.1.1.1','2.2.2.2']
new_ip_list=['3.3.3.3']
for line in f:
match=re.search('^(.*)\s*#\s*show\s+run',line)
if match!=None:
device_name=match.groups()[0].strip()
devices_list[device_name]=[]
continue
match=re.search('tacacs-server\s+host\s+(\S+)',line)
if match!=None:
host=match.groups()[0].strip()
devices_list[device_name].append(host)
print devices_list
devices_list_new={}
new_list=[]
old_list=[]
both_list=[]
neither_list=[]
for device in devices_list.keys():
devices_list_new[device]=[]
for ip in devices_list[device]:
if ip in old_ip_list:
devices_list_new[device].append('old')
elif ip in new_ip_list:
devices_list_new[device].append('new')
for device in devices_list_new.keys():
if 'old' in devices_list_new[device] and 'new' in devices_list_new[device]:
both_list.append(device)
elif 'new' in devices_list_new[device]:
new_list.append(device)
elif 'old' in devices_list_new[device]:
old_list.append(device)
else:
neither_list.append(device)
print 'New:'
for device in new_list:
print device
print 'Old:'
for device in old_list:
print device
print 'Both:'
for device in both_list:
print device
print 'Neither:'
for device in neither_list:
print device