Python网络自动化,涉及到执行命令,返回输出;提取信息,然后做进一部步的处理。
这里将演示,如何将命令(show arp, show ip interface brief)的输出结果,先提取,然后处理成表格。
这里信息提取,介绍2种方式:
- 正则表达式提取
- 文本多次切割
"""
Extract a DF from output of a show command in on network devices. like show arp, show ip interface brief, show mac address-table
"""
import pandas as pd
import re
pd.set_option ( 'display.max_columns', None )
pd.set_option ( 'display.width', 1000 )
# 1. Get table data: 1) get table data from output of a show * command on a network devices. Or 2) read from a file or string (like this example
table_data ="""
Protocol Address Age(min) Hardware Type Interface
Internet 10.1.1.13 12 28d0.f56a.c646 arpa GigabitEthernet 0/1
Internet 10.1.1.14 -- c0a4.7645.b7bd arpa GigabitEthernet 0/1
Internet 172.19.30.10 0 7085.c463.aacc arpa VLAN 2000
Internet 172.19.30.30 0 7085.c463.aad3 arpa VLAN 2000
Internet 172.19.30.254 -- c0a4.7645.b7bd arpa VLAN 2000
Internet 172.19.40.53 <---> <Incomplete> arpa VLAN 3000
Internet 172.19.40.253 15 c0a4.7645.b80b arpa VLAN 3000
Internet 172.19.40.254 -- c0a4.7645.b7bd arpa VLAN 3000
Internet 172.19.41.204 22 1082.3d77.abae arpa VLAN 4000
Internet 172.19.41.254 -- c0a4.7645.b7bd arpa VLAN 4000
Internet 172.19.42.3 <---> <Incomplete> arpa VLAN 4001
Internet 172.19.42.10 26 c8cd.55da.3dd9 arpa VLAN 4001
Internet 172.19.42.11 15 d431.27ca.e0ab arpa VLAN 4001
Internet 172.19.42.201 8 f074.8dc7.88ef arpa VLAN 4001
Internet 172.19.42.202 2 c8cd.5530.ee5d arpa VLAN 4001
Internet 172.19.42.203 23 c8cd.5530.b62d arpa VLAN 4001
Internet 172.19.42.254 -- c0a4.7645.b7bd arpa VLAN 4001
Internet 172.19.43.254 -- c0a4.7645.b7bd arpa VLAN 4002
Total number of ARP entries: 18
Interface: 6 Static: 0 Dynamic: 12 Trust: 0
"""
print("\n >>>>> 1. The table data is: \n", table_data)
# 2. extract table from the output: 2.1 pattern based extraction. or 2.2 Keywords based split
row_list = []
# 2.1 extract table from the output: 2.1 pattern based extraction
arp_table_column_pattern = re.compile(r'(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)')
arp_pattern = re.compile(r'(\S+)\s+(\d+\.\d+\.\d+\.\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+\s*\S+)')
lines = table_data.split('\n')
for line in lines:
if 'rotocol' in line:
column_name = arp_table_column_pattern.match(line).groups()
if column_name:
#print(column_name)
row_list.append(column_name)
if 'Internet' in line:
#print(line)
arp_records = arp_pattern.match(line).groups()
if arp_records:
#print(arp_records)
row_list.append(arp_records)
print("\n >>>>> 2.1 The tuple list is: \n", row_list)
# 2. extract table from the output: 2.1 pattern based extraction. or 2.2 Keywords based split
# 2.2 extract table from the output: 2.2 Keywords based split
'''
# 切割文本为list的list。如果内层的list(行)的长度与列名的长度不一致,则为无效条目。否则,将该list(行)给row_list
# 本例中,‘\n’换行符为行分隔符, '\s{2,}' or '\s+ '为字段分隔符。 请根据您自己的数据和分隔符修改代码集成到您的代码中。
print("\n >>>>> 2. Start to split the table string... ")
row_list = []
#for values in ([row.split("\t") for row in data.split("\n")]):
# if len(values) == len(columns_names):
for values in ([re.split('\s{2,}',row) for row in data.split("\n")]):
#for values in ([re.split('\s+ ', row) for row in data.split("\n")]):
if len(values) == 6:
row_list.append(values)
else:
print(" >>>>> The invalid row is: ", values)
print(" >>>>> 2.2 The split list [list] is: \n", row_list)
'''
# 3. Transfer list [tuple] or list [list] to DataFrame.转换list的list为Pandas的DataFrame
#row_list_df = pd.DataFrame(row_list, columns=columns_names)
row_list_df = pd.DataFrame(row_list)
row_list_df.columns = row_list_df.iloc[0].tolist()
row_list_df = row_list_df[1:]
print("\n >>>>> 3. The DataFrame is: \n", row_list_df)
row_list_df.to_csv('test1.csv')
# 4. Transfer list [list] to Dictionary list。转换list的list为字典列表
# value_dict_list = [dict(zip(columns_names, value)) for value in row_list if len(value) == len(columns_names)]
# value_dict_list = [dict(zip(columns_names, value)) for value in row_list]
value_dict_list = row_list_df.to_dict("records")
print("\n >>>>> 4. The Dictionary list: \n", *value_dict_list, sep="\n")
pd.DataFrame(value_dict_list).to_csv('test2.csv')
“”"
结果
>>>>> 1. The table data is:
Protocol Address Age(min) Hardware Type Interface
Internet 10.1.1.13 12 28d0.f56a.c646 arpa GigabitEthernet 0/1
Internet 10.1.1.14 -- c0a4.7645.b7bd arpa GigabitEthernet 0/1
Internet 172.19.30.10 0 7085.c463.aacc arpa VLAN 2000
Internet 172.19.30.30 0 7085.c463.aad3 arpa VLAN 2000
Internet 172.19.30.254 -- c0a4.7645.b7bd arpa VLAN 2000
Internet 172.19.40.53 <---> <Incomplete> arpa VLAN 3000
Internet 172.19.40.253 15 c0a4.7645.b80b arpa VLAN 3000
Internet 172.19.40.254 -- c0a4.7645.b7bd arpa VLAN 3000
Internet 172.19.41.204 22 1082.3d77.abae arpa VLAN 4000
Internet 172.19.41.254 -- c0a4.7645.b7bd arpa VLAN 4000
Internet 172.19.42.3 <---> <Incomplete> arpa VLAN 4001
Internet 172.19.42.10 26 c8cd.55da.3dd9 arpa VLAN 4001
Internet 172.19.42.11 15 d431.27ca.e0ab arpa VLAN 4001
Internet 172.19.42.201 8 f074.8dc7.88ef arpa VLAN 4001
Internet 172.19.42.202 2 c8cd.5530.ee5d arpa VLAN 4001
Internet 172.19.42.203 23 c8cd.5530.b62d arpa VLAN 4001
Internet 172.19.42.254 -- c0a4.7645.b7bd arpa VLAN 4001
Internet 172.19.43.254 -- c0a4.7645.b7bd arpa VLAN 4002
Total number of ARP entries: 18
Interface: 6 Static: 0 Dynamic: 12 Trust: 0
>>>>> 2.1 The tuple list is:
[('Protocol', 'Address', 'Age(min)', 'Hardware', 'Type', 'Interface'), ('Internet', '10.1.1.13', '12', '28d0.f56a.c646', 'arpa', 'GigabitEthernet 0/1'), ('Internet', '10.1.1.14', '--', 'c0a4.7645.b7bd', 'arpa', 'GigabitEthernet 0/1'), ('Internet', '172.19.30.10', '0', '7085.c463.aacc', 'arpa', 'VLAN 2000'), ('Internet', '172.19.30.30', '0', '7085.c463.aad3', 'arpa', 'VLAN 2000'), ('Internet', '172.19.30.254', '--', 'c0a4.7645.b7bd', 'arpa', 'VLAN 2000'), ('Internet', '172.19.40.53', '<--->', '<Incomplete>', 'arpa', 'VLAN 3000'), ('Internet', '172.19.40.253', '15', 'c0a4.7645.b80b', 'arpa', 'VLAN 3000'), ('Internet', '172.19.40.254', '--', 'c0a4.7645.b7bd', 'arpa', 'VLAN 3000'), ('Internet', '172.19.41.204', '22', '1082.3d77.abae', 'arpa', 'VLAN 4000'), ('Internet', '172.19.41.254', '--', 'c0a4.7645.b7bd', 'arpa', 'VLAN 4000'), ('Internet', '172.19.42.3', '<--->', '<Incomplete>', 'arpa', 'VLAN 4001'), ('Internet', '172.19.42.10', '26', 'c8cd.55da.3dd9', 'arpa', 'VLAN 4001'), ('Internet', '172.19.42.11', '15', 'd431.27ca.e0ab', 'arpa', 'VLAN 4001'), ('Internet', '172.19.42.201', '8', 'f074.8dc7.88ef', 'arpa', 'VLAN 4001'), ('Internet', '172.19.42.202', '2', 'c8cd.5530.ee5d', 'arpa', 'VLAN 4001'), ('Internet', '172.19.42.203', '23', 'c8cd.5530.b62d', 'arpa', 'VLAN 4001'), ('Internet', '172.19.42.254', '--', 'c0a4.7645.b7bd', 'arpa', 'VLAN 4001'), ('Internet', '172.19.43.254', '--', 'c0a4.7645.b7bd', 'arpa', 'VLAN 4002')]
>>>>> 3. The DataFrame is:
Protocol Address Age(min) Hardware Type Interface
1 Internet 10.1.1.13 12 28d0.f56a.c646 arpa GigabitEthernet 0/1
2 Internet 10.1.1.14 -- c0a4.7645.b7bd arpa GigabitEthernet 0/1
3 Internet 172.19.30.10 0 7085.c463.aacc arpa VLAN 2000
4 Internet 172.19.30.30 0 7085.c463.aad3 arpa VLAN 2000
5 Internet 172.19.30.254 -- c0a4.7645.b7bd arpa VLAN 2000
6 Internet 172.19.40.53 <---> <Incomplete> arpa VLAN 3000
7 Internet 172.19.40.253 15 c0a4.7645.b80b arpa VLAN 3000
8 Internet 172.19.40.254 -- c0a4.7645.b7bd arpa VLAN 3000
9 Internet 172.19.41.204 22 1082.3d77.abae arpa VLAN 4000
10 Internet 172.19.41.254 -- c0a4.7645.b7bd arpa VLAN 4000
11 Internet 172.19.42.3 <---> <Incomplete> arpa VLAN 4001
12 Internet 172.19.42.10 26 c8cd.55da.3dd9 arpa VLAN 4001
13 Internet 172.19.42.11 15 d431.27ca.e0ab arpa VLAN 4001
14 Internet 172.19.42.201 8 f074.8dc7.88ef arpa VLAN 4001
15 Internet 172.19.42.202 2 c8cd.5530.ee5d arpa VLAN 4001
16 Internet 172.19.42.203 23 c8cd.5530.b62d arpa VLAN 4001
17 Internet 172.19.42.254 -- c0a4.7645.b7bd arpa VLAN 4001
18 Internet 172.19.43.254 -- c0a4.7645.b7bd arpa VLAN 4002
>>>>> 4. The Dictionary list:
{'Protocol': 'Internet', 'Address': '10.1.1.13', 'Age(min)': '12', 'Hardware': '28d0.f56a.c646', 'Type': 'arpa', 'Interface': 'GigabitEthernet 0/1'}
{'Protocol': 'Internet', 'Address': '10.1.1.14', 'Age(min)': '--', 'Hardware': 'c0a4.7645.b7bd', 'Type': 'arpa', 'Interface': 'GigabitEthernet 0/1'}
{'Protocol': 'Internet', 'Address': '172.19.30.10', 'Age(min)': '0', 'Hardware': '7085.c463.aacc', 'Type': 'arpa', 'Interface': 'VLAN 2000'}
{'Protocol': 'Internet', 'Address': '172.19.30.30', 'Age(min)': '0', 'Hardware': '7085.c463.aad3', 'Type': 'arpa', 'Interface': 'VLAN 2000'}
{'Protocol': 'Internet', 'Address': '172.19.30.254', 'Age(min)': '--', 'Hardware': 'c0a4.7645.b7bd', 'Type': 'arpa', 'Interface': 'VLAN 2000'}
{'Protocol': 'Internet', 'Address': '172.19.40.53', 'Age(min)': '<--->', 'Hardware': '<Incomplete>', 'Type': 'arpa', 'Interface': 'VLAN 3000'}
{'Protocol': 'Internet', 'Address': '172.19.40.253', 'Age(min)': '15', 'Hardware': 'c0a4.7645.b80b', 'Type': 'arpa', 'Interface': 'VLAN 3000'}
{'Protocol': 'Internet', 'Address': '172.19.40.254', 'Age(min)': '--', 'Hardware': 'c0a4.7645.b7bd', 'Type': 'arpa', 'Interface': 'VLAN 3000'}
{'Protocol': 'Internet', 'Address': '172.19.41.204', 'Age(min)': '22', 'Hardware': '1082.3d77.abae', 'Type': 'arpa', 'Interface': 'VLAN 4000'}
{'Protocol': 'Internet', 'Address': '172.19.41.254', 'Age(min)': '--', 'Hardware': 'c0a4.7645.b7bd', 'Type': 'arpa', 'Interface': 'VLAN 4000'}
{'Protocol': 'Internet', 'Address': '172.19.42.3', 'Age(min)': '<--->', 'Hardware': '<Incomplete>', 'Type': 'arpa', 'Interface': 'VLAN 4001'}
{'Protocol': 'Internet', 'Address': '172.19.42.10', 'Age(min)': '26', 'Hardware': 'c8cd.55da.3dd9', 'Type': 'arpa', 'Interface': 'VLAN 4001'}
{'Protocol': 'Internet', 'Address': '172.19.42.11', 'Age(min)': '15', 'Hardware': 'd431.27ca.e0ab', 'Type': 'arpa', 'Interface': 'VLAN 4001'}
{'Protocol': 'Internet', 'Address': '172.19.42.201', 'Age(min)': '8', 'Hardware': 'f074.8dc7.88ef', 'Type': 'arpa', 'Interface': 'VLAN 4001'}
{'Protocol': 'Internet', 'Address': '172.19.42.202', 'Age(min)': '2', 'Hardware': 'c8cd.5530.ee5d', 'Type': 'arpa', 'Interface': 'VLAN 4001'}
{'Protocol': 'Internet', 'Address': '172.19.42.203', 'Age(min)': '23', 'Hardware': 'c8cd.5530.b62d', 'Type': 'arpa', 'Interface': 'VLAN 4001'}
{'Protocol': 'Internet', 'Address': '172.19.42.254', 'Age(min)': '--', 'Hardware': 'c0a4.7645.b7bd', 'Type': 'arpa', 'Interface': 'VLAN 4001'}
{'Protocol': 'Internet', 'Address': '172.19.43.254', 'Age(min)': '--', 'Hardware': 'c0a4.7645.b7bd', 'Type': 'arpa', 'Interface': 'VLAN 4002'}
进程已结束,退出代码为 0