python - "Python"通过mroutes解析的方式是什么

标签 python networking

我是一名网络工程师,正在尝试从一些导出的数据中编写特定的“mroute”(多播路由)脚本。我试图找出最“Pythonic”的路径来做到这一点。

数据看起来像这样(没有特定于我的网络,只是实验室导出):

(*,224.0.0.0/4) RPF nbr: 96.34.35.36 Flags: C RPF P
  Up: 1w5d

(*,224.0.0.0/24) Flags: D P
  Up: 1w5d

(*,224.0.1.39) Flags: S P
  Up: 1w5d

(96.34.246.55,224.0.1.39) RPF nbr: 96.34.35.36 Flags: RPF
  Up: 1w4d
  Incoming Interface List
    Bundle-Ether434 Flags: F A, Up: 1w4d
  Outgoing Interface List
    BVI100 Flags: F, Up: 1w4d
    TenGigE0/0/0/3 Flags: F, Up: 1w4d
    TenGigE0/0/1/1 Flags: F, Up: 1w4d
    TenGigE0/0/1/2 Flags: F, Up: 1w4d
    TenGigE0/0/1/3 Flags: F, Up: 1w4d
    TenGigE0/1/1/1 Flags: F, Up: 1w4d
    TenGigE0/1/1/2 Flags: F, Up: 1w4d
    TenGigE0/2/1/0 Flags: F, Up: 1w4d
    TenGigE0/2/1/1 Flags: F, Up: 1w4d
    TenGigE0/2/1/2 Flags: F, Up: 1w4d
    Bundle-Ether234 (0/3/CPU0) Flags: F, Up: 2d17h
    Bundle-Ether434 Flags: F A, Up: 1w4d

(*,224.0.1.40) Flags: S P
  Up: 1w5d
  Outgoing Interface List
    TenGigE0/2/1/0 Flags: II, Up: 1w5d

I have tried to replicate C style for loops to move the index incrementer up when I regex certain lines.

The end result is I only want to show a multicast group if it has specific output in the "Outgoing" section.

A horrible example of what I have tried so far (not complete, the data is handed off in a list):

myarray = []
myarray = output.split("\n")

max_count = len(myarray)
i= 0
while (i < max_count):
    if (re.match(r"(^\()", myarray[i])):
        group = myarray[i]
        print group
        i+=1
        while (re.match(r'(?!^\()', myarray[i])):
            if (re.match(r"  Outgoing Interface List", myarray[i])):
                outgoing = myarray[i]
                print outgoing
                i+=1
                while (re.match(r'(?!^\()', myarray[i])):
                    print myarray[i]
                    i+=1
            else:
                i+=1
    else:
        i+=1

感谢您的建议。

最佳答案

使用 for 循环无需使用变量计数器,因为它在循环时已返回序列号。

可能还有一种更简单或更好的方法可以做到这一点,但这只是我想到的一种方法,希望您能得到相同的结果。

myarray = output.split("\n")

for i in range(len(myarray)):
    if re.match('(^\()', myarray[i]):
        group = myarray[i]
        print group
        if (re.match('(?!^\()', myarray[i])):
            if re.match('\s+Outgoing Interface List', myarray[i]):
                outgoing = myarray[i]
                print outgoing
                if re.match('(?!^\()', myarray[i]):
                    print myarray[i]

我的结果是:

(*,224.0.0.0/4)
(*,224.0.0.0/24)
(*,224.0.1.39)
(96.34.246.55,224.0.1.39)
(0/3/CPU0)
(*,224.0.1.40)

关于python - "Python"通过mroutes解析的方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38831931/

相关文章:

networking - 读写 UDP 连接的套接字?

c# - 在多宿主 Windows 10 计算机上接收 UDP 多播消息

linux - SSH 连接丢失

python - PyQt5 项目结构和 PyInstaller ModuleNotFoundError

python - 将一系列数组转换为单个列表

python - OpenAI Gym 自定义环境 : Discrete observation space with real values

python - 如何检测套接字断开连接?/如何超时调用 socket.recv?

python - Unicode 无法正确打印到 cp850 (cp437),打牌套装

python - 以编程方式从存储库进行 pip install 会导致错误 "No such file or directory"

c - gethostbyname() 段错误