python代码:实现功能获取交换机接口信息并通过TextFSM进行解析。
from netmiko import Netmiko
import textfsm
show_intf_cmd_mapping = {'hp_comware': 'display interface',
}def ssh_device_2_get_intfs(device_type, host, username, password, port):dev_info = {'device_type': device_type,'host': host,'username': username,'password': password,'port': port,}cmd = show_intf_cmd_mapping.get(device_type)if not cmd:raise Exception('暂不支持此类设备的端口采集')with Netmiko(**dev_info) as net_conn:intfs = net_conn.send_command(cmd, use_textfsm=True)print(intfs)if __name__ == '__main__':dev_info = {'device_type': 'hp_comware','host': '192.168.56.14','username': 'admin','password': 'xxx','port': 22,}ssh_device_2_get_intfs(**dev_info)
代码执行后报错如下
应该是此条信息无法匹配查看端口采集信息
查看解析模板在“^\s*Unicast”后添加“^\s*Known-unicast”
添加后
再次运行,解析正常。