所有网卡常用信息获取集中展示(CentOS6 &CentOS7)
查看所有网卡,状态、光电类型、ip、广播地址、掩码
1、命令如下
(
string='|%-3s|%-14s|%-8s|%-15s|%-10s|%-16s|%-16s|%-16s|';
br="`echo $string|grep -Eo '[0-9]+'|awk '{s=s+$1}END{s=s+NR;for(i=0;i<=s;i++){printf "-"};print""}';`";
printf1(){ printf "$string\\n" NO Device Status Type Speed Ipaddr Mask Bcast; };
printf2(){ printf "$string\\n" $NO $DEVICE $STATUS $TYPE $SPEED $IPADDR $MASK $BCAST; };
echo $br;printf1;echo $br;
NO=0;
for i in $(ifconfig -a |egrep 'Link encap|mtu' |awk '{print $1}'|sed 's/:$//g'); do
DEVICE=$i;
STATUS=`ip a|grep ":.$i:"|awk '{print /LOWER_UP/?"UP":"DOWN"}'`;
TYPE=` ethtool $i |grep 'Supported ports' |sed 's/]//g' |awk -F '[' '{print $2}'|sed 's/ //g'`;
SPEED=` ethtool $i |awk '/Speed/{print $NF}'`;
IPADDR=`ifconfig $i |grep -Eo '([0-9]+\\.){3}[0-9]{1,3}'|awk 'NR==1'`;
MASK=` ifconfig $i |grep -Eo '([0-9]+\\.){3}[0-9]{1,3}'|awk '/^255/'`;
BCAST=` ifconfig $i |grep -Eo '([0-9]+\\.){3}[0-9]{1,3}'|awk 'NR>1&&!/^255/'`;
[ "x"$STATUS == "x" ] && STATUS='-';
[ "x"$TYPE == "x" ] && TYPE='-';
[ "x"$SPEED == "x" ] && SPEED='-';
[ "x"$IPADDR == "x" ] && IPADDR='-';
[ "x"$BCAST == "x" ] && BCAST='-';
[ "x"$MASK == "x" ] && MASK='-';
printf2;
NO=`echo $NO|awk '{print $1+1}'`;
done 2>/dev/null;
echo $br;
route -n|grep ^0.0.0.0|awk '{print "GATEWAY: ",$2," "$NF}';
echo $br;
)\r
2、使用:
[root@TiaoBan-1 ISO]# sh ifconfig.sh
-----------------------------------------------------------------------------------------------------------
|NO |Device |Status |Type |Speed |Ipaddr |Mask |Bcast |
-----------------------------------------------------------------------------------------------------------
|0 |eth0 |UP |TP |1000Mb/s |172.18.1.16 |255.255.255.0 |172.18.1.255 |
|1 |eth1 |DOWN |TP |Unknown! |192.168.200.252 |255.255.255.0 |192.168.200.255 |
|2 |lo |UP |- |- |127.0.0.1 |255.0.0.0 |- |
-----------------------------------------------------------------------------------------------------------
GATEWAY: 172.18.1.1 eth0
-----------------------------------------------------------------------------------------------------------
3、注解:
NO: 序号
Device: 网卡名称
Status: 网卡状态
Speed: 速度
Type: 网卡类型(光口:FIBRE、电口:TP)
Ipaddr: ip地址
Mask: 掩码
Bcast: 广播地址
GATEWAY: 网关地址