在Netmiko中使用TextFSM

在 netmiko 中使用 TextFSM

安装模板

1
2
$ cd ~
$ git clone https://github.com/networktocode/ntc-templates.git

查看安装完成

1
2
$ cd ~
$ ls ~/ntc-templates/templates/index

Netmiko已配置为自动在〜/ntc-template/templates/index中查找ntc-templates索引文件。另外,可以通过设置以下环境变量来明确告诉Netmiko在哪里寻找TextFSM模板目录(请注意,此目录中必须有一个索引文件):
export NET_TEXTFSM=/path/to/ntc-templates/templates/

使用

查看ntc-templates\index文件,确保其中有正确的命令且目录下存在模板文件。之后在 netmiko 中将use_textfsm = True参数添加到send_command()方法或send_command_timing()方法,即可获得结构化的数据;如果不存在模板,则正常返回字符串。

1
net_connect.send_command("show ip int brief", use_textfsm=True)

OUTPUT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[{'intf': 'FastEthernet0',
'ipaddr': 'unassigned',
'status': 'down',
'proto': 'down'},
{'intf': 'FastEthernet1',
'ipaddr': 'unassigned',
'status': 'down',
'proto': 'down'},
{'intf': 'FastEthernet2',
'ipaddr': 'unassigned',
'status': 'down',
'proto': 'down'},
{'intf': 'FastEthernet3',
'ipaddr': 'unassigned',
'status': 'down',
'proto': 'down'},
{'intf': 'FastEthernet4',
'ipaddr': '10.220.88.20',
'status': 'up',
'proto': 'up'}]

如何编写自定义TextFSM模板

示例

使用一系列正则表达式来定义要从纯文本输出中提取的数据。下面是一个处理思科接口的模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# cisco_asa_show_interface.template
Value Required INTERFACE (\S+)
Value INTERFACE_ZONE (.+?)
Value LINK_STATUS (\w+)
Value PROTOCOL_STATUS (.*)
Value HARDWARE_TYPE ([\w ]+)
Value BANDWIDTH (\d+\s+\w+)
Value DELAY (\d+\s+\w+)
Value DUPLEX (\w+\-\w+)
Value SPEED (\d+\w+\s\w+)
Value DESCRIPTION (.*)
Value ADDRESS ([a-zA-Z0-9]+.[a-zA-Z0-9]+.[a-zA-Z0-9]+)
Value MTU (\d+)
Value IP_ADDRESS (\d+\.\d+\.\d+\.\d+)
Value NET_MASK (\d+\.\d+\.\d+\.\d+)
Value ONEMIN_IN_PPS (\d+)
Value ONEMIN_IN_RATE (\d+)
Value ONEMIN_OUT_PPS (\d+)
Value ONEMIN_OUT_RATE (\d+)
Value ONEMIN_DROP_RATE (\d+)
Value FIVEMIN_IN_PPS (\d+)
Value FIVEMIN_IN_RATE (\d+)
Value FIVEMIN_OUT_PPS (\d+)
Value FIVEMIN_OUT_RATE (\d+)
Value FIVEMIN_DROP_RATE (\d+)

Start
^.*Interface ${INTERFACE} "${INTERFACE_ZONE}", is ${LINK_STATUS}.*protocol is ${PROTOCOL_STATUS}
^\s+Hardware is ${HARDWARE_TYPE} -> Continue
^.*BW ${BANDWIDTH}.*DLY ${DELAY}
^.*\(${DUPLEX}.*Auto-Speed\(${SPEED}\)
^.*Description: ${DESCRIPTION}
^.*MAC address ${ADDRESS}.*MTU ${MTU}
^.*IP address ${IP_ADDRESS}, .*subnet mask ${NET_MASK}
^.*1 minute input rate ${ONEMIN_IN_PPS} pkts/sec,\s+${ONEMIN_IN_RATE} bytes/sec
^.*1 minute output rate ${ONEMIN_OUT_PPS} pkts/sec,\s+${ONEMIN_OUT_RATE} bytes/sec
^.*1 minute drop rate, ${ONEMIN_DROP_RATE}
^.*5 minute input rate ${FIVEMIN_IN_PPS} pkts/sec,\s+${FIVEMIN_IN_RATE} bytes/sec
^.*5 minute output rate ${FIVEMIN_OUT_PPS} pkts/sec,\s+${FIVEMIN_OUT_RATE} bytes/sec
^.*5 minute drop rate, ${FIVEMIN_DROP_RATE} -> Record

通过show interface获取到的原始文本字符串:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# output of show interface
interfaces = '''
Interface GigabitEthernet0/0 "inside", is up, line protocol is up
Hardware is i82540EM rev02, BW 1000 Mbps, DLY 10 usec
Auto-Duplex(Full-duplex), Auto-Speed(1000 Mbps)
Input flow control is unsupported, output flow control is off
MAC address 0800.2735.03c6, MTU 1500
IP address 169.254.1.11, subnet mask 255.255.255.0
0 packets input, 0 bytes, 0 no buffer
Received 0 broadcasts, 0 runts, 0 giants
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
0 pause input, 0 resume input
0 L2 decode drops
1 packets output, 60 bytes, 0 underruns
0 pause output, 0 resume output
0 output errors, 0 collisions, 1 interface resets
0 late collisions, 0 deferred
0 input reset drops, 0 output reset drops
input queue (blocks free curr/low): hardware (511/511)
output queue (blocks free curr/low): hardware (511/510)
Traffic Statistics for "inside":
0 packets input, 0 bytes
1 packets output, 28 bytes
0 packets dropped
1 minute input rate 0 pkts/sec, 0 bytes/sec
1 minute output rate 0 pkts/sec, 0 bytes/sec
1 minute drop rate, 0 pkts/sec
5 minute input rate 0 pkts/sec, 0 bytes/sec
5 minute output rate 0 pkts/sec, 0 bytes/sec
5 minute drop rate, 0 pkts/sec

通过 TextFsm 进行处理:

1
2
3
4
5
6
7
8
9
# import library
import textfsm

# open the template file
with open('cisco_asa_show_interface.template', 'r') as f:
template = textfsm.TextFSM(f)

# run the interface data through the template parser
template.ParseText(interfaces)

输出如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[['GigabitEthernet0/0',
'inside',
'up',
'up',
'i82540EM rev02',
'1000 Mbps',
'10 usec',
'Full-duplex',
'1000 Mbps',
'',
'0800.2735.03c6',
'1500',
'169.254.1.11',
'255.255.255.0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0']]

一些已经写好的华三设备的模板:https://github.com/xdai555/textfsm_hpe_cmw7