命令使用:
1 [[email protected] home]# ifconfig -a |grep ‘Link encap‘ -A1|grep -v -|awk -v AA=1 -v A1=0 ‘BEGIN{print"---------------------------------------------------------\n|Device\t|Ipaddr\t\t|Bcast\t\t|Mask\t\t|\n---------------------------------------------------------"}{if(NR==AA){ AA=AA+2;A1=$1;}else {if($2~/[0-9]/){print A1":\t"substr($2,6)"\t"substr($3,7)"\t"substr($4,6)}else{print A1":"}}}‘ 2 --------------------------------------------------------- 3 |Device |Ipaddr |Bcast |Mask | 4 --------------------------------------------------------- 5 eth0: 192.168.237.200 192.168.237.255 255.255.255.0 6 lo: 127.0.0.1 55.0.0.0 7 pan0: 8 tap0: 192.168.0.1 192.168.0.255 255.255.255.0
命令解释:
第一步:
ifconfig -a |grep ‘Link encap‘ -A1|grep -v ‘-‘初步处理:
1 [[email protected] home]# ifconfig -a |grep ‘Link encap‘ -A1|grep -v ‘-‘ 2 eth0 Link encap:Ethernet HWaddr 00:0C:29:2D:46:65 3 inet addr:192.168.237.200 Bcast:192.168.237.255 Mask:255.255.255.0 4 lo Link encap:Local Loopback 5 inet addr:127.0.0.1 Mask:255.0.0.0 6 pan0 Link encap:Ethernet HWaddr BE:BA:2D:2A:09:D7 7 BROADCAST MULTICAST MTU:1500 Metric:1 8 tap0 Link encap:Ethernet HWaddr 0E:76:08:C3:C6:42 9 inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
第二步:
awk处理:
1 awk 2 -v AA=1 #定义赋值变量AA 3 -v A1=0 #定义赋值变量A1 4 ‘ 5 BEGIN{print"---------------------------------------------------------\n|Device\t|Ipaddr\t\t|Bcast\t\t|Mask\t\t|\n---------------------------------------------------------"} #打印开头描述 6 { 7 if(NR==AA) #判断NR行数是否和AA相等,主要意图是判断行数是否为奇数行 8 { AA=AA+2;A1=$1;} #奇数行AA+2赋值为下一个奇数行,赋值奇数行的设备名到A1 9 else 10 { 11 if($2~/[0-9]/) #判断偶数行的第二个域是否含有数字,含有数字应该就是配置了IP地址 12 {print A1":\t"substr($2,6)"\t"substr($3,7)"\t"substr($4,6)} #有IP配置,打印“设备名(奇数行时的A1设备名赋值)、ip地址、广播地址、掩码” 13 else 14 {print A1":"} #没有配置IP,只打印“设备名(奇数行时的A1设备名赋值)” 15 } 16 } 17 ‘
时间: 2024-10-22 14:25:41