0
我的机器有多个以太网设备,如eth0
,eth1
等。其中一个IP的IP范围为192.168.x.x
。我如何使用shell脚本获取设备名称? (优选地使用ip
命令而不是ifconfig
)Shell脚本:从子网掩码获取eth名称
喜欢
eth0 192.168.2.3
或 eth3 192.168.5.6
我的机器有多个以太网设备,如eth0
,eth1
等。其中一个IP的IP范围为192.168.x.x
。我如何使用shell脚本获取设备名称? (优选地使用ip
命令而不是ifconfig
)Shell脚本:从子网掩码获取eth名称
喜欢
eth0 192.168.2.3
或 eth3 192.168.5.6
可以尝试这;
ip -o -4 a | awk '$2 ~ "eth" { gsub(/\/.*/, "", $4); print $2" "$4}'
到所有接口;
ip -o -4 a | awk ' { gsub(/\/.*/, "", $4); print $2" "$4}'
如:
[email protected]:/tmp/$ ip -o -4 a | awk ' { gsub(/\/.*/, "", $4); print $2" "$4}'
lo 127.0.0.1
eth0 x.x.x.x
docker0 x.x.x.x
[email protected]:/tmp/$ ip -o -4 a | awk '$2 ~ "eth" { gsub(/\/.*/, "", $4); print $2" "$4}'
eth0 x.x.x.x
人的ip说:以 '\' 字符
-o,-oneline 输出在一行上的每个记录,代替换行符。当您想要 用wc(1)或grep(1)输出计数记录时,这很方便。
-4 a:只显示TCP/IP IPv4