当通过next(iter)
进行迭代时,我想基本上将网络组合在一起,我通过计算每行中出现的项目数来做到这一点,如果它有7个项目,则它是条目的开头。
唯一的问题是当我通过line = next(bgp_table_iter)
访问“下一行”并打破循环时,如果“下一行”有7个项目意味着开始一个新条目,当它从{{1开始它再次迭代到下一行,跳过应该有7个项目的原始“下一行”,并且是新条目的开头。
哪一端会跳过其他所有条目。
我怎么能阻止跳过?或者让For循环触发器在中断时转到上一次迭代?
for line in bgp_table_iter:
输出:
def show_ip_bgp():
data = test_output_short.split('RPKI validation codes: V valid, I invalid, N Not found\n\n Network Next Hop Metric LocPrf Weight Path\n')
bgp_table = data[1]
bgp_table = re.sub(" Network Next Hop Metric LocPrf Weight Path\n","",bgp_table)
bgp_table = re.sub("\*","",bgp_table)
bgp_table_list = bgp_table.split('\n')
bgp_table_iter = iter(bgp_table_list)
overall_dict = {}
index = 0
for line in bgp_table_iter:
# print line.split(), len(line.split())
current_line = line.split()
# If 7 items are in the list it's the start of a network
if len(current_line) == 7:
best = True if '>' in current_line[0] else False
network = current_line[1]
attached_ip = current_line[2]
print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
for i in range(0,1000):
line = next(bgp_table_iter)
subline_line = line.split()
best_sub = 'True' if '>' in subline_line[0] else 'False'
if len(subline_line) == 6:
print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
elif len(subline_line) == 5:
print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
elif len(subline_line) == 7:
break
test_output_short = '''BGP table version is 3538854, local router ID is 10.15.1.81
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found
Network Next Hop Metric LocPrf Weight Path
* i 10.0.1.0/24 10.8.111.45 0 115 0 i
*>i 10.8.11.45 0 120 0 i
* i 10.8.11.45 0 120 0 i
* i 10.8.11.45 0 120 0 i
* i 10.0.3.0/29 10.8.111.45 0 115 0 i
*>i 10.8.11.45 0 120 0 i
* i 10.8.11.45 0 120 0 i
* i 10.8.11.45 0 120 0 i
* i 10.8.0.0/16 10.9.0.1 0 50 0 i
* i 10.8.0.1 0 50 0 i
*> 0.0.0.0 0 32768 i
* i 10.9.0.0/16 10.9.0.1 0 50 0 i
* i 10.8.0.1 0 50 0 i
*> 0.0.0.0 0 32768 i
*>i 10.10.2.0/24 10.8.10.2 0 100 0 i
* i 10.8.10.2 0 100 0 i
* i 10.8.10.2 0 100 0 i
* i 10.10.5.0/24 10.8.142.15 0 85 0 i
*>i 10.8.42.15 0 100 0 i
* i 10.8.42.15 0 100 0 i
* i 10.8.42.15 0 100 0 i
*>i 10.10.7.0/24 10.8.40.84 0 100 0 i
* i 10.8.40.84 0 100 0 i
* i 10.8.40.84 0 100 0 i
*>i 10.10.8.0/24 10.8.10.8 0 100 0 i
* i 10.8.110.8 0 85 0 i
* i 10.8.10.8 0 100 0 i
* i 10.8.10.8 0 100 0 i
*>i 10.10.11.0/24 10.8.42.8 0 100 0 i
* i 10.8.42.8 0 100 0 i
* i 10.8.42.8 0 100 0 i
* i 10.9.42.8 0 100 0 i
* i 10.10.12.0/24 10.8.10.12 0 100 0 i
* i 10.8.10.12 0 100 0 i
*>i 10.8.10.12 0 100 0 i'''
更新
建议的修复程序似乎有效,直到我运行测试。它确实使所有网络看起来都像。
def show_ip_bgp(): data = test_output_very_short.split('RPKI验证码:V有效,我无效,N未找到\ n \ n网络下一跳度量标准LocPrf权重路径\ n') bgp_table =数据[1] bgp_table = re.sub(“网络下一跳度量标准LocPrf权重路径\ n”,“”,bgp_table) bgp_table = re.sub(“*”,“”,bgp_table)
>>> show_ip_bgp()
Found Starting Network 10.0.1.0/24 10.8.111.45 Best Path: False
Sub: 10.8.11.45 Best Path: True
Sub: 10.8.11.45 Best Path: False
Sub: 10.8.11.45 Best Path: False
Found Starting Network 10.8.0.0/16 10.9.0.1 Best Path: False
Sub: 10.8.0.1 Best Path: False
Sub: 0.0.0.0 Best Path: True
Found Starting Network 10.10.2.0/24 10.8.10.2 Best Path: True
Sub: 10.8.10.2 Best Path: False
Sub: 10.8.10.2 Best Path: False
Found Starting Network 10.10.7.0/24 10.8.40.84 Best Path: True
Sub: 10.8.40.84 Best Path: False
Sub: 10.8.40.84 Best Path: False
Found Starting Network 10.10.11.0/24 10.8.42.8 Best Path: True
Sub: 10.8.42.8 Best Path: False
Sub: 10.8.42.8 Best Path: False
Sub: 10.9.42.8 Best Path: False
输出:
bgp_table_list = bgp_table.split('\n')
bgp_table_iter = iter(bgp_table_list)
overall_dict = {}
index = 0
for i, line in enumerate(bgp_table_list):
# print line.split(), len(line.split())
current_line = line.split()
# If 7 items are in the list it's the start of a network
if len(current_line) == 7:
best = True if '>' in current_line[0] else False
network = current_line[1]
attached_ip = current_line[2]
print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
for i in range(0,1000):
line = bgp_table_list[i+1]
print i
subline_line = line.split()
best_sub = 'True' if '>' in subline_line[0] else 'False'
if len(subline_line) == 6:
print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
elif len(subline_line) == 5:
print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
elif len(subline_line) == 7:
line = bgp_table_list[i+1]
break
每次显示时索引号都会回到0,打印出要检查的行的索引。
答案 0 :(得分:3)
不要强迫它为iter
,而是继续将其用作list
,然后使用enumerate
在list
中了解您的位置。
编辑:Chaning首先枚举以使用index
,第二个循环的范围以1
而不是0
开头,并使用except子句检查line = bgp_table_list[index+i]
行当它到达文件末尾时。
for index, line in enumerate(bgp_table_list):
# print line.split(), len(line.split())
current_line = line.split()
# If 7 items are in the list it's the start of a network
if len(current_line) == 7:
best = True if '>' in current_line[0] else False
network = current_line[1]
attached_ip = current_line[2]
print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
for i in range(1, 1000):
try:
line = bgp_table_list[index+i]
except IndexError:
break
subline_line = line.split()
best_sub = 'True' if '>' in subline_line[0] else 'False'
if len(subline_line) == 6:
print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
elif len(subline_line) == 5:
print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
elif len(subline_line) == 7:
break
我的输出(不确定是否正确,提供以便您可以判断)
Found Starting Network 10.0.1.0/24 10.8.111.45 Best Path: False
Sub: 10.8.11.45 Best Path: True
Sub: 10.8.11.45 Best Path: False
Sub: 10.8.11.45 Best Path: False
Found Starting Network 10.0.3.0/29 10.8.111.45 Best Path: False
Sub: 10.8.11.45 Best Path: True
Sub: 10.8.11.45 Best Path: False
Sub: 10.8.11.45 Best Path: False
Found Starting Network 10.8.0.0/16 10.9.0.1 Best Path: False
Sub: 10.8.0.1 Best Path: False
Sub: 0.0.0.0 Best Path: True
Found Starting Network 10.9.0.0/16 10.9.0.1 Best Path: False
Sub: 10.8.0.1 Best Path: False
Sub: 0.0.0.0 Best Path: True
Found Starting Network 10.10.2.0/24 10.8.10.2 Best Path: True
Sub: 10.8.10.2 Best Path: False
Sub: 10.8.10.2 Best Path: False
Found Starting Network 10.10.5.0/24 10.8.142.15 Best Path: False
Sub: 10.8.42.15 Best Path: True
Sub: 10.8.42.15 Best Path: False
Sub: 10.8.42.15 Best Path: False
Found Starting Network 10.10.7.0/24 10.8.40.84 Best Path: True
Sub: 10.8.40.84 Best Path: False
Sub: 10.8.40.84 Best Path: False
Found Starting Network 10.10.8.0/24 10.8.10.8 Best Path: True
Sub: 10.8.110.8 Best Path: False
Sub: 10.8.10.8 Best Path: False
Sub: 10.8.10.8 Best Path: False
Found Starting Network 10.10.11.0/24 10.8.42.8 Best Path: True
Sub: 10.8.42.8 Best Path: False
Sub: 10.8.42.8 Best Path: False
Sub: 10.9.42.8 Best Path: False
Found Starting Network 10.10.12.0/24 10.8.10.12 Best Path: False
Sub: 10.8.10.12 Best Path: False
Sub: 10.8.10.12 Best Path: True