如何解析VLAN简短输出(Cisco)

时间:2019-03-28 13:23:08

标签: python networking cisco

我正在从IOS思科交换机中检索数据。输出如下:

VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Gi1/0/3, Gi1/0/4, Gi1/0/5               
10   ADMIN_SWT                        active
11   ADMIN_WIFI                       active    Gi1/0/31, Gi1/0/45, Gi1/0/46
12   ADMIN_SRV-DATA                   active

想法是将此输出解析为CSV文件。你知道怎么做吗?

2 个答案:

答案 0 :(得分:0)

您可以解析Mylist中的每个条目并将其全部添加到Pandas DataFrame。

这是我的解决方法:

import pandas as pd

Mylist = ['VLAN Name Status Ports', '1 default active Gi1/0/3, Gi1/0/4, Gi1/0/5, Gi1/0/6, Gi1/0/7, Gi1/0/8, Gi1/0/9, Gi1/0/10, Gi1/0/11, Gi1/0/12, Gi1/0/17, Gi1/0/19, Gi1/0/21, Gi1/0/22, Gi1/0/24, Gi1/0/30, Gi1/0/32, Gi1/0/42, Gi1/0/51, Gi1/0/52', '10 ADMIN_SWT active ', '11 ADMIN_WIFI']

results = []
ports_list = []
output_list = []

# save the headers
headers = Mylist.pop(0).split(" ")

# convert string to list of lists
for l in Mylist:
  for k in l.split(" "):
    # if this is a port add all the port to one cell 
    if '/' in k:    
      ports_list.append(k)
    else:
      output_list.append(k)
  output_list.append(' '.join(ports_list))
  results.append(output_list)
  # clear the temp lists
  output_list = []
  ports_list = []

# make sure every sublist has only number of elements in headers length
for item in results:
  if len(item) < len(headers):
    item.append("")
  elif len(item) > len(headers):
    # remove all the unnecessary elements
    del item[len(headers)-1:-1]

df = pd.DataFrame(results, columns = headers)
df.to_csv('out.csv')

根据您的“我的清单”示例,这是输出:

  VLAN        Name  Status                                              Ports
0    1     default  active  Gi1/0/3, Gi1/0/4, Gi1/0/5, Gi1/0/6, Gi1/0/7, G...
1   10   ADMIN_SWT  active
2   11  ADMIN_WIFI

答案 1 :(得分:0)

Template Text Parser可以帮助您产生所需的结果,示例代码:

from ttp import ttp

template = """
<input load="text">
VLAN Name                             Status    Ports
---- -------------------------------- --------- -------------------------------
1    default                          active    Gi1/0/3, Gi1/0/4, Gi1/0/5               
10   ADMIN_SWT                        active
11   ADMIN_WIFI                       active    Gi1/0/31, Gi1/0/45, Gi1/0/46
                                                Gi1/0/47, Gi1/0/48, Gi1/0/49
12   ADMIN_SRV-DATA                   active
</input>

<group>
{{ vid | DIGIT | _start_ }}  {{ name }}   {{ status }}
{{ vid | DIGIT | _start_ }}  {{ name }}   {{ status }}    {{ ports | ORPHRASE | replace(",", "") | joinmatches(" ") }}
                                                {{ ports | ORPHRASE | replace(",", "") | joinmatches(" ") }}
</group>

<output 
format="csv" 
returner="terminal"
headers="vid,name,status,ports"
/>
    """

parser = ttp(template=template)
parser.parse()
print(parser.result(format="json")[0])

下面的输出将被打印到命令行终端窗口:

vid,name,status,ports
1,default,active,Gi1/0/3 Gi1/0/4 Gi1/0/5
10,ADMIN_SWT,active,
11,ADMIN_WIFI,active,Gi1/0/31 Gi1/0/45 Gi1/0/46 Gi1/0/47 Gi1/0/48 Gi1/0/49
12,ADMIN_SRV-DATA,active,

请注意组中的最后一行,它的前导空格应与要解析的实际文本的数量完全相同。