Netmiko与textFSM联动失效无法匹配

文章讲述了在使用Netmiko库连接并尝试通过TextFSM解析华为设备接口状态命令输出时,遇到的问题。由于平台标识不匹配,`use_textfsm`参数未生效,导致返回结果仍为字符串而非预期的列表。解决方案包括直接指定匹配的模板或修改ntc_templates的index文件,将platform从huawei_vrp更改为huawei,或者调整设备类型为huawei_vrpv8。

问题描述

from netmiko import ConnectHandler
import json

SW1 = {
    'device_type': 'huawei',
    'ip': '192.168.171.12',
    'username': 'aa',
    'password': '123',
}
connect = ConnectHandler(**SW1)
print("Sucessfully connected to " + SW1['ip'])
interfaces = connect.send_command('display interface brief',use_textfsm=True)

print(type(interfaces))
print(json.dumps(interfaces,indent=2))

执行结果

Sucessfully connected to 192.168.171.12
<class 'str'>     #这里可以看到没有使用textfsm进行解析,也即use_textfsm没有起作用,正确应该返回list类型
"PHY: Physical\n*down: administratively down\n(l): loopback\n(s): spoofing\n(b): BFD down\n(e): ETHOAM down\n(dl): DLDP down\n(d): Dampening Suppressed\nInUti/OutUti: input utility/output utility\nInterface                   PHY   Protocol InUti OutUti   inErrors  outErrors\nEthernet0/0/1               up    up          0%     0%          0          0\nEthernet0/0/2               down  down        0%     0%          0          0\nEthernet0/0/3               down  down        0%     0%          0          0\nEthernet0/0/4               down  down        0%     0%          0          0\nEthernet0/0/5               down  down        0%     0%          0          0\nEthernet0/0/6               down  down        0%     0%          0          0\nEthernet0/0/7               down  down        0%     0%          0          0\nEthernet0/0/8               down  down        0%     0%          0          0\nEthernet0/0/9               down  down        0%     0%          0          0\nEthernet0/0/10              down  down        0%     0%          0          0\nEthernet0/0/11              down  down        0%     0%          0          0\nEthernet0/0/12              down  down        0%     0%          0          0\nEthernet0/0/13              down  down        0%     0%          0          0\nEthernet0/0/14              down  down        0%     0%          0          0\nEthernet0/0/15              down  down        0%     0%          0          0\nEthernet0/0/16              down  down        0%     0%          0          0\nEthernet0/0/17              down  down        0%     0%          0          0\nEthernet0/0/18              down  down        0%     0%          0          0\nEthernet0/0/19              down  down        0%     0%          0          0\nEthernet0/0/20              down  down        0%     0%          0          0\nEthernet0/0/21              down  down        0%     0%          0          0\nEthernet0/0/22              down  down        0%     0%          0          0\nGigabitEthernet0/0/1        down  down        0%     0%          0          0\nGigabitEthernet0/0/2        down  down        0%     0%          0          0\nMEth0/0/1                   down  down        0%     0%          0          0\nNULL0                       up    up(s)       0%     0%          0          0\nVlanif1                     up    up          --     --          0          0"

分析

正确返回的数据类型应该是list,但是这里依然是str,说明use_textfsm没有起作用

解决方法

前提是已正确配置好NET_TEXTFSM环境变量
方法1:直接指定匹配的模板

interfaces = connect.send_command('display interface brief',use_textfsm=True,textfsm_template='huawei_vrp_display_interface_brief.textfsm')

方法2:修改index文件

lib/python3.10/site-packages/ntc_templates/templates$ vi index
Template, Hostname, Platform, Command
huawei_vrp_display_traffic-filter_applied-record.textfsm, .*, huawei_vrp, dis[[play]] traffic-filter applied-record
huawei_vrp_display_snmp-agent_community_read.textfsm, .*, huawei_vrp, dis[[play]] snm[[p-agent]] c[[ommunity]] (r[[ead]]|w[[rite]])
huawei_vrp_display_ip_routing-table_verbose.textfsm, .*, huawei_vrp, dis[[play]] ip(v6)? routi[[ng-table]] ve[[rbose]]
huawei_vrp_display_interface_description.textfsm, .*, huawei_vrp, dis[[play]] inter[[face]] des[[cription]]
huawei_vrp_display_service-set_id_id.textfsm, .*, huawei_vrp, dis[[play]] service-set (id|name) \S+
huawei_vrp_display_interface_brief.textfsm, .*, huawei_vrp, dis[[play]] inter[[face]] br[[ief]]
huawei_vrp_display_service-set_all.textfsm, .*, huawei_vrp, dis[[play]] service-set all
huawei_vrp_display_ipv6_neighbors.textfsm, .*, huawei_vrp, dis[[play]] ipv6 n[[eighbors]]
huawei_vrp_display_lldp_neighbor.textfsm, .*, huawei_vrp, dis[[play]] lldp nei[[ghbor]]
huawei_vrp_display_temperature.textfsm, .*, huawei_vrp, dis[[play]] tem[[perature]]
huawei_vrp_display_nat_server.textfsm, .*, huawei_vrp, dis[[play]] na[[t]] ser[[ver]]
huawei_vrp_display_sn_license.textfsm, .*, huawei_vrp, dis[[play]] sn l[[icence]]
huawei_vrp_display_arp_brief.textfsm, .*, huawei_vrp, dis[[play]] arp br[[ief]]
huawei_vrp_display_interface.textfsm, .*, huawei_vrp, dis[[play]] inter[[face]]\s*((?!brief|counters|description).)*$
huawei_vrp_display_isis_peer.textfsm, .*, huawei_vrp, disp[[lay]] isis p[[eer]]
huawei_vrp_display_port_vlan.textfsm, .*, huawei_vrp, dis[[play]] port vl[[an]]
huawei_vrp_display_acl_all.textfsm, .*, huawei_vrp, dis[[play]] acl(\si[[pv6]])? a[[ll]]
huawei_vrp_display_arp_all.textfsm, .*, huawei_vrp, disp[[lay]] ar[[p]] all
huawei_vrp_display_startup.textfsm, .*, huawei_vrp, dis[[play]] star[[tup]]
huawei_vrp_display_version.textfsm, .*, huawei_vrp, dis[[play]] ver[[sion]]

可以看到index文件中的platformhuawei_vrp,而代码中我们是huawei。所以修改index中的huawei_vrphuawei,或者修改代码中的device_typehuawei_vrpv8。因为netmiko支持的os为huaweihuawei_vrpv8

SW1 = {
    'device_type': 'huawei_vrpv8',
    'ip': '192.168.171.12',
    'username': 'aa',
    'password': '123',
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值