基本的原理就是用netsh interface ipv4 show address命令列出所有网卡的地址信息,然后用sed过滤掉无用的内容,具体实现如下:
$ netsh interface ipv4 show address |\
sed -E -e '/(DHCP enabled|Subnet Prefix|InterfaceMetric|Default Gateway|Gateway Metric)/d' |\
sed -e '/Configuration/{ N ; s/\n/ / }' |\
sed -E -e 's/(Configuration for interface|IP Address)//g' -e '/^$/d'
输出:
“Npcap Loopback Adapter” : 169.254.154.214
“本地连接 2”
“本地连接” : 192.168.10.150
“VirtualBox Host-Only Network” : 192.168.56.1
“Loopback Pseudo-Interface 1” : 127.0.0.1

本文介绍了一种使用Netsh命令结合Sed工具来获取并过滤计算机中各网络接口IPv4地址信息的方法。通过特定的正则表达式匹配与删除不必要的输出内容,实现了对网络接口配置信息的有效提取。
8069





