查找多播传出接口

时间:2017-02-07 16:50:49

标签: python regex multicast

尝试使用以下方法进行正则表达,但到目前为止都失败了。

尝试捕获路由器中的多播错误。该错误是一种多播,其中出接口是Bundle-Ether(仅限以太束),并且与物理线路卡无关。

示例输出如下:

(192.168.72.140,232.95.240.51) RPF nbr: 10.0.34.89 Flags: RPF
  Up: 4w4d
  Incoming Interface List
    Bundle-Ether20 Flags: A, Up: 4w4d
  Outgoing Interface List
    Bundle-Ether5 (0/0/CPU0) Flags: F NS, Up: 4w4d

(192.168.137.7,232.95.240.68) RPF nbr: 10.0.34.242 Flags: RPF
  Up: 20:50:13
  Incoming Interface List
    Bundle-Ether5 Flags: A, Up: 20:50:13
  Outgoing Interface List
    Bundle-Ether20 (0/2/CPU0) Flags: F NS, Up: 20:50:13

(192.168.137.12,232.95.240.71) RPF nbr: 10.0.34.242 Flags: RPF
  Up: 4w4d
  Incoming Interface List
    Bundle-Ether5 Flags: A, Up: 23:09:53
  Outgoing Interface List
    Bundle-Ether12 (0/8/CPU0) Flags: F NS, Up: 4w4d
    Bundle-Ether20 Flags: F NS, Up: 23:09:37
    Bundle-Ether429 (0/7/CPU0) Flags: F NS, Up: 4w4d

您会注意到,最后一个多播(S,G = 192.168.137.12,232.95.240,71)在传出接口中有一个Bundle-Ether,它直接从接口转到“Flags”。在这种情况下“Bundle-Ether20 Flags:”。虽然“Bundle-Ether429(0/7 / CPU0)标志:”界面没有碰到错误。

试图弄清楚我是如何只捕获遇到这个bug的多播并忽略其余的,因为我的输出可能是数千行。我应该将输出拆分为单独的行并运行循环来处理每一行(并且有条件匹配的嵌套循环)?希望有一个正则表达式解决方案,最好是在python中。

1 个答案:

答案 0 :(得分:0)

这适用于您提供的当前输入。如果整个格式保持不变,它将起作用。如果格式发生变化,您可以对其进行微调。让我们使输入更加多样化并测试我们的代码:

输入文件:file.log

(192.168.72.140,232.95.240.51) RPF nbr: 10.0.34.89 Flags: RPF
  Up: 4w4d
  Incoming Interface List
    Bundle-Ether20 Flags: A, Up: 4w4d
  Outgoing Interface List
    Bundle-Ether5 (0/0/CPU0) Flags: F NS, Up: 4w4d

(192.168.137.7,232.95.240.68) RPF nbr: 10.0.34.242 Flags: RPF
  Up: 20:50:13
  Incoming Interface List
    Bundle-Ether5 Flags: A, Up: 20:50:13
  Outgoing Interface List
    Bundle-Ether2011 Flags: F NS, Up: 23:09:37

(192.168.137.12,232.95.240.71) RPF nbr: 10.0.34.242 Flags: RPF
  Up: 4w4d
  Incoming Interface List
    Bundle-Ether5 Flags: A, Up: 23:09:53
  Outgoing Interface List
    Bundle-Ether12 (0/8/CPU0) Flags: F NS, Up: 4w4d
    Bundle-Ether20 Flags: F NS, Up: 23:09:37
    Bundle-Ether429 (0/7/CPU0) Flags: F NS, Up: 4w4d
    Bundle-Ether204 Flags: F NS, Up: 23:09:37

代码:

import re

with open(file.log, 'r') as f:
    for entry in f.read().split('\n\n'):
        req2=[]
        req = entry.split()
        req2.append(req[0])
        i = req.index('Outgoing')+3
        req2.append(req[i:])
        res = re.findall(r'Bundle-Ether\d+\s*[^(]+?(?=Bundle-Ether|$)',' '.join(req2[1]))
        if res:
            req3= [req2[0]] + res
            print req3

输出:

['(192.168.137.7,232.95.240.68)', 'Bundle-Ether2011 Flags: F NS, Up: 23:09:37']
['(192.168.137.12,232.95.240.71)', 'Bundle-Ether20 Flags: F NS, Up: 23:09:37 ', 'Bundle-Ether204 Flags: F NS, Up: 23:09:37']