在 neimiko 中使用 TextFSM
安装模板
$ cd ~
$ git clone https://github.com/networktocode/ntc-templates?__s=XXXXXXXX
查看安装完成
$ cd ~
$ ls ~/ntc-templates/templates/index
Netmiko已配置为自动在〜/ntc-template/templates/index中查找ntc-templates索引文件。另外,可以通过设置以下环境变量来明确告诉Netmiko在哪里寻找TextFSM模板目录(请注意,此目录中必须有一个索引文件):
export NET_TEXTFSM=/path/to/ntc-templates/templates/
使用
查看ntc-templatesindex
文件,确保其中有正确的命令且目录下存在模板文件。之后在 netmiko 中将use_textfsm = True
参数添加到send_command()
方法或send_command_timing()
方法,即可获得结构化的数据;如果不存在模板,则正常返回字符串。
net_connect.send_command("show ip int brief", use_textfsm=True)
OUTPUT:
[{'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模板
示例
使用一系列正则表达式来定义要从纯文本输出中提取的数据。下面是一个处理思科接口的模板:
# 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+sw+)
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
获取到的原始文本字符串:
# 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 进行处理:
# 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)
输出如下:
[['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']]