You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python解析EAPI返回JSON结果求助:pyeapi交换机接口状态解析指导

如何解析pyeapi返回的接口状态JSON结果?

Hey,我懂你现在的处境——刚上手pyeapi和以太网交换机交互,拿到返回的嵌套JSON结果就懵了,不知道怎么挖出来自己需要的信息对吧?别慌,我来一步步带你拆解这个结果。

首先先回顾下你的代码和返回结果:

你的代码

import pyeapi
node = pyeapi.connect_to('evs')
eth_48 = node.enable('show interfaces eth48 status')
print(eth_48[0])

返回的结果示例

{'command': 'show interfaces eth48 status', 'result': {u'interfaceStatuses': {u'Ethernet48': {u'description': u'', u'adminStatus': u'up', u'operStatus': u'up', u'mediaType': u'1000BASE-T', u'linkStatus': u'connected'}}}}

接下来我们逐层解析这个嵌套结构:


步骤1:拿到命令执行的核心结果

node.enable()返回的是一个列表,每个元素对应一条命令的执行结果,你用eth_48[0]已经拿到了第一条命令的结果字典,这一步没问题。接下来我们先取出result字段,这里面才是接口状态的实际数据:

result_data = eth_48[0]['result']

步骤2:定位到接口状态集合

result_data里的interfaceStatuses字段是所有接口状态的容器,键就是接口的名称(比如Ethernet48),值是该接口的详细状态:

interface_statuses = result_data['interfaceStatuses']

步骤3:提取目标接口的详细信息

直接用接口名称作为键,就能拿到Ethernet48的所有状态数据:

eth48_details = interface_statuses['Ethernet48']

步骤4:提取具体的状态字段

现在你就可以按需提取管理状态、运行状态、描述等信息了。这里推荐用.get()方法来取值,这样如果某个字段不存在,会返回你指定的默认值,避免抛出KeyError:

# 提取常用字段
admin_status = eth48_details.get('adminStatus', 'Unknown')
oper_status = eth48_details.get('operStatus', 'Unknown')
description = eth48_details.get('description', '无描述')
link_status = eth48_details.get('linkStatus', 'Unknown')
media_type = eth48_details.get('mediaType', 'Unknown')

# 打印结果
print(f"Ethernet48 接口状态:")
print(f"- 管理状态:{admin_status}")
print(f"- 运行状态:{oper_status}")
print(f"- 链路状态:{link_status}")
print(f"- 接口描述:{description}")
print(f"- 介质类型:{media_type}")

完整的可运行示例代码

import pyeapi

# 连接到交换机
node = pyeapi.connect_to('evs')
# 执行命令获取接口状态
eth_48 = node.enable('show interfaces eth48 status')

# 安全解析结果(避免空值或字段不存在报错)
if eth_48:
    command_result = eth_48[0]
    result_data = command_result.get('result', {})
    interface_statuses = result_data.get('interfaceStatuses', {})
    eth48_details = interface_statuses.get('Ethernet48', {})

    # 提取需要的字段
    admin_status = eth48_details.get('adminStatus', 'Unknown')
    oper_status = eth48_details.get('operStatus', 'Unknown')
    description = eth48_details.get('description', '无描述')
    link_status = eth48_details.get('linkStatus', 'Unknown')
    media_type = eth48_details.get('mediaType', 'Unknown')

    # 格式化输出
    print("=== Ethernet48 接口状态信息 ===")
    print(f"管理状态:{admin_status}")
    print(f"运行状态:{oper_status}")
    print(f"链路状态:{link_status}")
    print(f"接口描述:{description}")
    print(f"介质类型:{media_type}")
else:
    print("命令执行失败,未获取到有效结果")

额外小提示

  • 如果需要批量获取所有接口的状态,把命令改成show interfaces status,然后遍历interfaceStatuses的键值对即可:
    all_interfaces = node.enable('show interfaces status')[0]['result']['interfaceStatuses']
    for intf_name, intf_details in all_interfaces.items():
        print(f"\n接口 {intf_name}:")
        print(f"  运行状态:{intf_details.get('operStatus', 'Unknown')}")
    
  • 始终用.get()代替直接下标取值,能让你的代码更健壮,应对返回结果可能存在的字段缺失情况。

内容的提问来源于stack exchange,提问作者dcrearer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:56:07