我想从单个文本文件中提取网络设备信息,然后将那些选定的信息保存到json中。该文本文件包含来自多供应商设备的信息的混合,每个设备以略有不同的格式显示,并且全部以非结构格式显示。
该文件的输出是3个供应商设备(AAA,BBB和CCC)中多个设备的输出。以下是包含混合供应商设备输出的文件。每个输出都会开始,并以供应商名称,设备名称和IP地址来区分。每个输出都以“ Completed ...”语句结尾。我要提取的数据是本地端口,邻居/远程/信息端口和远程系统名称。
示例:
Vendor: AAA
Name:rtr1.bac.it
IP:10.1.1.1
Local Interface Chassis Id Port info System Name
ge-0/0/1 00:aa:01:02:03:f0 ge-0/1/1 rtr3.bac.it
ge-1/0/1 00:aa:02:02:03:f0 ge-1/0/2 rtr3.bac.it
ge-1/0/2 00:aa:03:02:03:f0 ge-2/0/1 rtr2.bac.it
ge-2/0/3 00:aa:04:02:03:f0 ge-2/0/3 rtr1.bac.it
xe-0/0/0 00:aa:05:02:03:f0 69238784 rtr5.bac.it
Completed...
Vendor: BBB
Name:rtr10.bac.it
IP:10.1.1.2
Local Intf Neighbor Dev Neighbor Intf Exptime (sec)
-------------------------------------------------------------------------------
GigabitEthernet1/0/0 rtr1.bac.it GigabitEthernet2/0/0 110
GigabitEthernet2/0/0 rtr1.bac.it 628 103
GigabitEthernet2/1/0 sw10.bac.dev GigabitEthernet1/0/0 113
GigabitEthernet4/0/0 ted1.dev GigabitEthernet2/0/1 110
Completed...
Vendor: CCC
Name:rtr5.bac.it
IP:10.1.1.5
Device System Information:-
===============================================================================
NB = nearest-bridge NTPMR = nearest-non-tpmr NC = nearest-customer
===============================================================================
Lcl Port Scope Remote Chassis ID Index Remote Port Remote System Name
-------------------------------------------------------------------------------
1/1/1 NB 00:00:01:A6:B4:44 51 1/1/1, 10-Gig * rt1.bac.it
1/1/12 NB 10:E0:9C:90:30:01 11 9/0/1, 10/100/* sw2.bac.dev
7/7/1 NB 00:21:05:AC:70:40 15 2/2/2, 10-Gig * rt1.bac.it
2/2/1 NB G0:1C:2D:22:37:F0 99 xe-6/1/0 rtr3.bac.it
1/4/1 NB G0:1C:2D:22:37:F0 19 Link 2 dev1 * rtr3.bac.it
===============================================================================
* indicates that the corresponding row element may have been truncated.
Number of neighbors : 8
Completed...
Vendor: AAA
Name:rtr10.bac.it
IP:10.1.1.10
Local Interface Chassis Id Port info System Name
ge-1/0/1 00:aa:01:02:03:f0 ge-4/6/1 rtr1.bac.it
ge-2/0/1 00:aa:02:02:03:f0 ge-1/0/4 rtr3.bac.it
ge-3/1/2 00:aa:03:02:03:f0 ge-1/1/1 rtr10.bac.it
ge-1/0/3 00:aa:04:02:03:f0 ge-3/0/3 rtr10.bac.it
ge-1/0/0 00:aa:05:02:03:f0 ge-1/0/3 rtr3.bac.it
Completed...
Vendor: AAA
Name:rtr3.bac.it
IP:10.1.1.3
Local Interface Chassis Id Port info System Name
ge-1/0/1 00:aa:01:02:03:f0 ge-4/6/1 rtr1.bac.it
ge-2/0/1 00:aa:02:02:03:f0 ge-1/0/4 rtr3.bac.it
ge-3/1/2 00:aa:03:02:03:f0 ge-1/1/1 rtr10.bac.it
ge-1/0/3 00:aa:04:02:03:f0 ge-3/0/3 rtr10.bac.it
ge-1/0/0 00:aa:05:02:03:f0 ge-1/0/3 rtr3.bac.it
Completed...
Vendor: BBB
Name:rtr11.bac.it
IP:10.1.1.11
Local Intf Neighbor Dev Neighbor Intf Exptime (sec)
-------------------------------------------------------------------------------
GigabitEthernet4/0/0 rtr1.bac.it GigabitEthernet2/0/0 110
GigabitEthernet5/0/0 rtr1.bac.it 628 103
GigabitEthernet6/1/0 sw10.bac.dev GigabitEthernet1/0/0 113
GigabitEthernet7/0/0 ted1.dev GigabitEthernet2/0/1 110
Completed...
Vendor: BBB
Name:rtr20.bac.it
IP:10.1.1.20
Local Intf Neighbor Dev Neighbor Intf Exptime (sec)
-------------------------------------------------------------------------------
GigabitEthernet1/0/0 rtr1.bac.it GigabitEthernet2/0/0 110
GigabitEthernet2/0/0 rtr1.bac.it 628 103
GigabitEthernet2/1/0 sw10.bac.dev GigabitEthernet1/0/0 113
GigabitEthernet4/0/0 ted1.dev GigabitEthernet2/0/1 110
Completed...
Vendor: CCC
Name:rtr7.bac.it
IP:10.1.1.17
Device System Information:-
===============================================================================
NB = nearest-bridge NTPMR = nearest-non-tpmr NC = nearest-customer
===============================================================================
Lcl Port Scope Remote Chassis ID Index Remote Port Remote System Name
-------------------------------------------------------------------------------
1/1/1 NB 00:00:01:A6:B4:44 51 1/1/1, 10-Gig * rt1.bac.it
1/1/12 NB 10:E0:9C:90:30:01 11 9/0/1, 10/100/* sw2.bac.dev
7/7/1 NB 00:21:05:AC:70:40 15 2/2/2, 10-Gig * rt1.bac.it
2/2/1 NB G0:1C:2D:22:37:F0 99 xe-6/1/0 rtr3.bac.it
1/4/1 NB G0:1C:2D:22:37:F0 19 Link 2 dev1 * rtr3.bac.it
===============================================================================
* indicates that the corresponding row element may have been truncated.
Number of neighbors : 8
Completed...
如果每个供应商的输出保存在单独的文件中,我可以提取每个供应商的信息,但是这里的问题是所有供应商的输出都在单个文件中,并且每个设备输出的提取数据略有不同由于上述样本的数据结构不同。我不确定该怎么做。
逻辑:
if vendor 'AAA' in textfile:
process AAA executed to extract the info require
elif vendor 'BBB' in textfile :
process BBB executed to extract the info require
elif vendor 'CCC' in textfile :
process CCC executed to extract the info require
save process AAA, BBB and CCC onto json file.
我期望像下面这样的json文件
scandevice [
device name1
localport = xxx
neighborport = xxx
systemname = xxx
device name2
localport = xxx
neighborport = xxx
systemname = xxx
device name3
localport = xxx
neighborport = xxx
systemname = xxx
device name4
localport = xxx
neighborport = xxx
systemname = xxx
]
我正在考虑读取文件并将其转换为列表。从列表中,我检查它是AAA,BBB还是CCC供应商,并为每个供应商专门针对循环运行。
我在想类似下面的内容...但这将创建3个json文件...我仍然找不到正确的解决方案。请帮助和建议。谢谢
with open('ne_output.txt', 'r') as f:
for line in f:
data = [line.strip().split() for line in ff]
if 'AAA' in line:
print("This is AAA vendor")
dict = {"scanAAA" :[]}
for x in data:
**some operation here**
dict["scanAAA"].append()
with open('myfileAAA.json', 'w') as f:
json.dump(dict, f)
elif 'BBB' in line:
print("This is BBB vendor")
dict = {"scanBBB" :[]}
for x in data:
**some operation here**
dict["scanBBB"].append()
with open('myfileBBB.json', 'w') as f:
json.dump(dict, f)
elif 'CCC' in line:
print("This is CCC vendor")
dict = {"scanCCC" :[]}
for x in data:
**some operation here**
dict["scanCCC"].append()
with open('myfileCCC.json', 'w') as f:
json.dump(dict, f)