开发工具【免费下载链接】jcCLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.项目地址https://gitcode.com/gh_mirrors/jc/jc点击查看免费下载ifconfig是 Linux、macOS 与 BSD 系统上查看网络接口状态的经典命令但它的文本输出格式随平台差异巨大难以直接用于脚本处理。jc 的ifconfig解析器jc.parsers.ifconfig把这一传统命令的输出统一转换为结构化 JSON让接口名、MAC 地址、IP 配置、收发统计、甚至光模块传感器数据都能被jq等工具直接消费。读完本文你将掌握该解析器的 CLI 与模块级用法、完整的输出 Schema、跨平台差异的处理细节以及如何利用ipv4/ipv6数组对象获取接口上的全部地址信息。一、解析器概览与使用前提ifconfig解析器版本 2.4不依赖任何ifconfig命令行选项直接解析其默认输出即可。它同时兼容linux、aix、freebsd、darwin四种平台见 jc/parsers/ifconfig.py 中的info.compatible列表测试用例覆盖了 CentOS 7.7、Ubuntu 16.04/18.04、macOS 10.11/10.14 与 FreeBSD 12 的真实输出见 tests/test_ifconfig.py。需要说明的是项目文档明确建议优先使用ip命令替代ifconfig因为ip原生支持 JSON 输出。但当你仍然需要解析遗留系统或脚本中的ifconfig输出时本解析器就是标准解决方案。CLI 用法$ ifconfig | jc --ifconfig或直接让 jc 代为执行命令$ jc ifconfigPython 模块用法import jc result jc.parse(ifconfig, ifconfig_command_output)其中ifconfig_command_output是ifconfig命令的原始文本输出字符串返回值是一个字典列表每个字典对应一个网络接口。二、输出 Schema 全解解析结果是一个 JSON 数组数组内每个元素代表一个网络接口。下面是在 docs/parsers/ifconfig.md 中定义的完整 Schema[ { name: string, type: string, metric: integer, flags: integer, state: [ string ], mtu: integer, mac_addr: string, ipv4_addr: string, # [0] ipv4_mask: string, # [0] ipv4_bcast: string, # [0] ipv6_addr: string, # [0] ipv6_mask: integer, # [0] ipv6_scope: string, # [0] ipv6_scope_id: string, # [0] ipv6_type: string, # [0] rx_packets: integer, rx_bytes: integer, rx_errors: integer, rx_dropped: integer, rx_overruns: integer, rx_frame: integer, tx_packets: integer, tx_bytes: integer, tx_errors: integer, tx_dropped: integer, tx_overruns: integer, tx_carrier: integer, tx_collisions: integer, options: string, options_flags: [ string ], status: string, hw_address: string, media: string, media_flags: [ string ], nd6_options: integer, nd6_flags: [ string ], plugged: string, vendor: string, vendor_pn: string, vendor_sn: string, vendor_date: string, module_temperature: string, module_voltage: string, ipv4: [ { address: string, mask: string, broadcast: string } ], ipv6: [ { address: string, scope_id: string, mask: integer, scope: string, type: string } ], lanes: [ { lane: integer, rx_power_mw: float, rx_power_dbm: float, tx_bias_ma: float } ] } ]字段分组与含义接口标识name接口名、type如Ethernet、flags数值型接口标志、state标志字符串数组如[UP,BROADCAST,RUNNING,MULTICAST]、mtu、metric。地址信息mac_addrMAC 地址、顶层ipv4_addr/ipv4_mask/ipv4_bcast与ipv6_addr/ipv6_mask/ipv6_scope/ipv6_scope_id/ipv6_type是向后兼容字段以及完整的ipv4、ipv6数组对象。收发统计rx_*系列packets、bytes、errors、dropped、overruns、frame与tx_*系列packets、bytes、errors、dropped、overruns、carrier、collisions。BSD/macOS 特有options/options_flags驱动选项、statusactive/inactive、hw_address、media/media_flags、nd6_options/nd6_flagsIPv6 邻居发现选项。光模块传感器FreeBSD 特有plugged、vendor、vendor_pn、vendor_sn、vendor_date、module_temperature、module_voltage以及按 lane 排列的lanes数组rx_power_mw、rx_power_dbm、tx_bias_ma。关于向后兼容字段注释 [0]的重要说明文档中标注[0]的顶层字段ipv4_addr、ipv4_mask、ipv4_bcast、ipv6_addr、ipv6_mask、ipv6_scope、ipv6_scope_id、ipv6_type只保留接口输出中最后一个 IP 地址这是为兼容旧版本而保留的。若要获取接口上的全部IP 地址必须使用ipv4和ipv6数组对象。这一点在源码中有明确体现解析器逐行扫描时会同时把地址信息写入顶层兼容字段后者会被后续匹配覆盖只留最后一个和累积的ipv4_info/ipv6_info列表最终组装为ipv4/ipv6数组见 jc/parsers/ifconfig.py 中 for backwards compatibility 注释后的逻辑。macOSen0接口的 fixture 最能说明问题该接口有 4 个 IPv6 地址顶层ipv6_addr只保留最后一个2600:1700:bab0:d40::24而ipv6数组完整包含全部 4 条见 tests/fixtures/osx-10.14.6/ifconfig.json。三、实战示例基础解析processed 模式$ ifconfig ens33 | jc --ifconfig -p输出-p表示美化打印[ { name: ens33, flags: 4163, state: [ UP, BROADCAST, RUNNING, MULTICAST ], mtu: 1500, type: Ethernet, mac_addr: 00:0c:29:3b:58:0e, ipv4_addr: 192.168.71.137, ipv4_mask: 255.255.255.0, ipv4_bcast: 192.168.71.255, ipv6_addr: fe80::c1cb:715d:bc3e:b8a0, ipv6_mask: 64, ipv6_scope: 0x20, ipv6_type: link, metric: null, rx_packets: 8061, rx_errors: 0, rx_dropped: 0, rx_overruns: 0, rx_frame: 0, tx_packets: 4502, tx_errors: 0, tx_dropped: 0, tx_overruns: 0, tx_carrier: 0, tx_collisions: 0, rx_bytes: 1514413, tx_bytes: 866622, ipv4: [ { address: 192.168.71.137, mask: 255.255.255.0, broadcast: 192.168.71.255 } ], ipv6: [ { address: fe80::c1cb:715d:bc3e:b8a0, scope_id: null, mask: 64, scope: 0x20, type: link } ] } ]原始输出模式-r使用-rraw时所有字段保持文本原样state为逗号连接字符串数值不做类型转换$ ifconfig ens33 | jc --ifconfig -p -r[ { name: ens33, flags: 4163, state: UP,BROADCAST,RUNNING,MULTICAST, mtu: 1500, type: Ethernet, mac_addr: 00:0c:29:3b:58:0e, ipv4_addr: 192.168.71.137, ipv4_mask: 255.255.255.0, ipv4_bcast: 192.168.71.255, ipv6_addr: fe80::c1cb:715d:bc3e:b8a0, ipv6_mask: 64, ipv6_scope: 0x20, ipv6_type: link, metric: null, rx_packets: 8061, rx_errors: 0, rx_dropped: 0, rx_overruns: 0, rx_frame: 0, tx_packets: 4502, tx_errors: 0, tx_dropped: 0, tx_overruns: 0, tx_carrier: 0, tx_collisions: 0, rx_bytes: 1514413, tx_bytes: 866622, ipv4: [ { address: 192.168.71.137, mask: 255.255.255.0, broadcast: 192.168.71.255 } ], ipv6: [ { address: fe80::c1cb:715d:bc3e:b8a0, scope_id: null, mask: 64, scope: 0x20, type: link } ] } ]与 jq 联动的典型场景解析后即可直接交给jq过滤$ ifconfig | jc --ifconfig | jq -r .[] | select(.state | index(UP)) | [.name, .ipv4_addr] | tsv这条命令列出所有处于 UP 状态的接口及其 IPv4 地址是网络巡检脚本的常见起点。四、parse() 函数签名与参数Python 模块入口是parse函数定义于 jc/parsers/ifconfig.pydef parse(data: str, raw: bool False, quiet: bool False) - List[Dict[str, Any]]参数说明参数类型默认值含义datastring必填待解析的ifconfig文本输出rawbooleanFalse为True时返回未做类型转换的原始结构化输出quietbooleanFalse为True时抑制兼容性警告信息返回值字典列表即上面 Schema 定义的结构化数据raw 或 processed 模式。在函数入口处解析器会先调用jc.utils.compatibility()检查运行平台是否在[linux, aix, freebsd, darwin]兼容列表内并调用jc.utils.input_type_check(data)校验输入类型随后所有字段被预置为None保证接口对象结构完整见 jc/parsers/ifconfig.py。五、跨平台解析原理与处理细节三套语法分支解析器的核心是按平台维护了多组正则表达式源码中分为re_linux、re_openbsd、re_freebsd三组见 jc/parsers/ifconfig.py用_bundle_match辅助函数逐行匹配。以 Linux 语法为例接口行形如ens33: flags4163UP,BROADCAST,RUNNING,MULTICAST mtu 1500匹配re_linux_interface后可得到name、type、mac_addrre_linux_state抓取尖括号内的状态与MTU、Metricre_linux_rx/re_linux_tx/re_linux_bytes/re_linux_tx_stats分别抓取收发统计。FreeBSD 的接口行形如cxl3: flags8843... metric 0 mtu 1500因此单独定义了re_freebsd_interface等一组模式OpenBSD 与 FreeBSD 语法相近但也有专门模式。processed 模式下的类型与格式统一_process函数负责把 raw 结果转换为符合 Schema 的规范格式jc/parsers/ifconfig.py关键转换包括数值转换flags、mtu、ipv6_mask、rx_*、tx_*、metric、nd6_options、lane转为整数rx_power_mw、rx_power_dbm、tx_bias_ma转为浮点数。macOS 十六进制掩码转换macOS 的netmask 0xffffff00会被转换为点分十进制255.255.255.0。源码中逐字节拆分十六进制字符串并转十进制拼接见 jc/parsers/ifconfig.py。FreeBSD CIDR 掩码转换新式 FreeBSD 输出如inet 23.246.26.130/25中的 CIDR 前缀通过_convert_cidr_to_quad借助ipaddress.IPv4Network转换为点分掩码jc/parsers/ifconfig.py以便与其他平台输出一致。这也解释了为何utun接口的0xffffffff掩码最终呈现为255.255.255.255见 tests/test_ifconfig.py 的 utun 用例。标志拆分state、options_flags、media_flags、nd6_flags均由逗号分隔字符串拆分为数组。光模块与 lane 数据FreeBSD 特色FreeBSD 12 的ifconfig在光口上会输出 SFP/SFP 光模块诊断信息解析器专门为这些字段定义了正则re_freebsd_plugged模块类型、re_freebsd_vendor_pn_sn_date厂商/PN/SN/生产日期、re_freebsd_temp_volts温度/电压、re_freebsd_lane逐 lane 的光功率与偏置电流。每个 lane 累积到lane_info列表最终组装为lanes数组jc/parsers/ifconfig.py。真实样例FreeBSD 12见 tests/fixtures/freebsd12/ifconfig-extra-fields.outcxl3: flags8843UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST metric 0 mtu 1500 options6ec07bbRXCSUM,TXCSUM,VLAN_MTU,... ether 00:07:43:3d:b7:70 hwaddr 00:07:43:3d:b7:88 media: Ethernet 10Gbase-LR full-duplex,rxpause,txpause status: active nd6 options29PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL plugged: SFP/SFP/SFP28 10G Base-LR (LC) vendor: INNOLIGHT PN: TR-PX13L-N00 SN: INJBL0431986 DATE: 2020-01-04 module temperature: 21.20 C voltage: 3.16 Volts lane 1: RX power: 0.49 mW (-3.10 dBm) TX bias: 23.85 mA对应解析结果tests/fixtures/freebsd12/ifconfig-extra-fields.json中options为6ec07bb、options_flags展开为 15 个字符串的数组lanes数组内rx_power_mw/rx_power_dbm/tx_bias_ma被转为浮点数-3.10 dBm→-3.1。多 lane 场景如 100G 四 lane 光模块则会输出 4 个 lane 对象见 tests/fixtures/freebsd12/ifconfig-extra-fields4.out。六、常见注意事项不支持的选项本解析器不支持任何ifconfig命令行选项只能解析默认输出。兼容字段陷阱依赖ipv4_addr/ipv6_addr等顶层字段时要注意它们只保留最后一个地址获取完整地址列表请使用ipv4/ipv6数组。平台差异Linux 输出含type、mac_addr与收发统计macOS/BSD 输出侧重options、media、status、nd6等信息缺少的字段统一为null不会缺失键名。空数据传入空字符串时返回空数组test_ifconfig_nodata用例验证见 tests/test_ifconfig.py。七、延伸阅读解析器实现jc/parsers/ifconfig.py官方文档docs/parsers/ifconfig.md测试用例tests/test_ifconfig.py跨平台测试样本Linux 见 tests/fixtures/centos-7.7/ifconfig.outmacOS 见 tests/fixtures/osx-10.14.6/ifconfig.outFreeBSD 光模块见 tests/fixtures/freebsd12/ifconfig-extra-fields.outjc 通用使用指南docs/readme.md赞分享开发工具【免费下载链接】jcCLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.项目地址https://gitcode.com/gh_mirrors/jc/jc点击查看免费下载相关推荐使用 jc 将 ethtool 输出转换为 JSON网络接口与光模块信息结构化解析指南使用 jc 将 ethtool 输出转换为 JSON网络接口与光模块信息结构化解析指南 jcJSON Convert是面向命令行工具输出、文件类型和通用字开发工具jc 解析 free 命令输出Linux 内存信息转 JSON 的完整使用指南jc 解析 free 命令输出Linux 内存信息转 JSON 的完整使用指南 jc 是一款把常用命令行工具输出、文件类型与常见字符串转换为 JSON / Y开发工具jc 解析器实战使用 jc --gpg 将 gpg --with-colons 输出转换为 JSONjc 解析器实战使用 jc gpg 将 gpg with colons 输出转换为 JSON 导读 本文介绍 jcJSON Convert项目中的 gpg开发工具上一篇5分钟搞定API文档Langflow自动化工具全攻略下一篇Candle模型服务API服务器与推理服务创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考