NetDevOps-华为设备格式化display interface输出

display interface的输出结果为字符串, 不方便机器处理, 所以需要格式化为python的字典格式, 方便处理.

实现代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
from typing import Union
from pprint import pprint
from netmiko import ConnectHandler
from common import exec_cli


def exec_cli(cmd: Union[str, list, tuple], device: dict, timeout=240) -> str:
    """
    功能: 发送单条命令给网络设备并获得网络设备的回显
    参数:
     - cmd: 需要发送给设备的命令, 字符串、列表、元组
     - timeout: 等待提示符的超时时间
     - device: 设备信息
    返回值:
     - 命令执行结果的回显
    """

    with ConnectHandler(**device) as net_connect:
        if isinstance(cmd, str):
            return net_connect.send_command(cmd, cmd_verify=False, read_timeout=timeout)

        if isinstance(cmd, (list, tuple)):
            return net_connect.send_config_set(cmd, cmd_verify=False, read_timeout=timeout)


def get_intf_info_huawei(output):
    """
    功能: 将华为设备display interface输出格式化为字典
    参数:
     - output: display interface命令执行的回显
    返回值:
     - 字典
    """
    l2_intf_regex = r'(?P<intf_name>.*?) current state : (?P<phy>.*?) \(.*?state : (?P<protocol>.*?) \n.*?' \
                    r'Description: (?P<description>.*?)\n.*?Hardware address is (?P<mac>.*?)\n.*?Last ' \
                    r'physical up time   : (?P<last_phy_up_time>.*?)\nLast physical down time : ' \
                    r'(?P<last_phy_down_time>.*?)\n.*?'

    l3_intf_regex1 = r'(?P<intf_name>.*?) current state : (?P<phy>.*?) \(.*?state : (?P<protocol>.*?) \n.*? time : ' \
                     r'(?P<last_pro_up_time>.*?)\n.*?Description: (?P<description>.*?)\n.*?Unit is (?P<mtu>\d+).*?' \
                     r'Internet Address is (?P<ip>.*?)\n.*?.*'

    l3_intf_regex2 = r'(?P<intf_name>.*?) current state : (?P<phy>.*?) \(.*?state : (?P<protocol>.*?) \n.*? time : ' \
                     r'(?P<last_pro_up_time>.*?)\n.*?Description: (?P<description>.*?)\n.*?Unit is (?P<mtu>\d+).*?' \
                     r'Internet Address is (?P<ip>.*?)\n.*?Hardware address is (?P<mac>.*?)\n.*?Last physical up time' \
                     r'   : (?P<last_phy_up_time>.*?)\nLast physical down time : (?P<last_phy_down_time>.*?)\n.*'

    output_lst = output.split('\n\n')
    search_res_lst = []
    res_dict = {}

    for item in output_lst:
        for regex in [l2_intf_regex, l3_intf_regex1, l3_intf_regex2]:  # 正则匹配由粗略到精细
            temp_res = re.search(regex, item, re.I | re.S)
            if temp_res:
                search_res_lst.append(temp_res.groupdict())

    for item in search_res_lst:
        res_dict.update({item.pop('intf_name'): item})  # 由于精细的在粗略的后面, 字典里精细的匹配结果会覆盖前面的粗略的结果

    return res_dict


def main():
    device = {
        "host": "10.0.0.3",
        "username": "localadmin",
        "password": 'Huawei@123',
        "device_type": "huawei",
    }
    output = exec_cli("display interface", device)

    pprint(get_intf_info_huawei(output))


if __name__ == "__main__":
    main()

输出结果格式为字典

{'GE1/0/0': {'description': 'TO_CISCO_SW.www.ocean.com_Et1/0',
             'ip': '10.0.0.3/24',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '2023-12-23 10:02:22',
             'last_pro_up_time': '2023-12-23 10:02:22',
             'mac': '000c-606b-55d5',
             'mtu': '1500',
             'phy': 'UP',
             'protocol': 'UP'},
 'GE1/0/1': {'description': '',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '-',
             'mac': '7092-460f-dd13',
             'phy': 'Administratively DOWN',
             'protocol': 'DOWN'},
 'GE1/0/2': {'description': '',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '-',
             'mac': '7092-460f-dd13',
             'phy': 'Administratively DOWN',
             'protocol': 'DOWN'},
 'GE1/0/3': {'description': '',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '-',
             'mac': '7092-460f-dd13',
             'phy': 'Administratively DOWN',
             'protocol': 'DOWN'},
 'GE1/0/4': {'description': '',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '-',
             'mac': '7092-460f-dd13',
             'phy': 'Administratively DOWN',
             'protocol': 'DOWN'},
 'GE1/0/5': {'description': '',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '-',
             'mac': '7092-460f-dd13',
             'phy': 'Administratively DOWN',
             'protocol': 'DOWN'},
 'GE1/0/6': {'description': '',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '-',
             'mac': '7092-460f-dd13',
             'phy': 'Administratively DOWN',
             'protocol': 'DOWN'},
 'GE1/0/7': {'description': '',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '-',
             'mac': '7092-460f-dd13',
             'phy': 'Administratively DOWN',
             'protocol': 'DOWN'},
 'GE1/0/8': {'description': '',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '-',
             'mac': '7092-460f-dd13',
             'phy': 'Administratively DOWN',
             'protocol': 'DOWN'},
 'GE1/0/9': {'description': '',
             'last_phy_down_time': '2023-12-23 10:02:08',
             'last_phy_up_time': '-',
             'mac': '7092-460f-dd13',
             'phy': 'Administratively DOWN',
             'protocol': 'DOWN'},
 'LoopBack0': {'description': '',
               'ip': '1.1.1.1/32',
               'last_pro_up_time': '2023-12-23 10:02:08',
               'mtu': '1500',
               'phy': 'UP',
               'protocol': 'UP (spoofing)'},
 'MEth0/0/0': {'description': '',
               'last_phy_down_time': '2023-12-23 10:02:09',
               'last_phy_up_time': '-',
               'mac': '7092-460f-dd12',
               'phy': 'Administratively DOWN',
               'protocol': 'DOWN'}}
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值