基本的原理就是用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