所有网卡常用信息获取集中展示(CentOS6 &CentOS7)

查看所有网卡,状态、光电类型、ip、广播地址、掩码

1、命令如下

(                                                                                                        
string='|%-3s|%-18s|%-10s|%-10s|%-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
"  NO  Device  Status  Type  Speed  Ipaddr  Mask  Bcast; };                 
printf2(){ printf "$string
" $NO $DEVICE $STATUS $TYPE $SPEED $IPADDR $MASK $BCAST; };                 
echo $br;printf1;echo $br;                                                                               
NO=0;                                                                                                    
for i in `ip a|awk -F ':' '/^[0-9]/{print $2}'|sort`; 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;                                                                                                
)

2、使用: 

[root@localhost eversec]# sh hhh 
------------------------------------------------------------------------------------------------------------
|NO |Device            |Status    |Type      |Speed     |Ipaddr          |Mask            |Bcast           |
------------------------------------------------------------------------------------------------------------
|0  |lo                |up        |-         |-         |127.0.0.1       |255.0.0.0       |-               |
|1  |eth0              |up        |TP        |1000Mb/s  |10.233.253.66   |255.255.255.248 |10.233.253.71   |
|2  |eth1              |up        |TP        |1000Mb/s  |172.16.7.74     |255.255.255.248 |172.16.7.79     |
|3  |eth2              |up        |FIBRE     |10000Mb/s |-               |-               |-               |
|4  |eth3              |up        |FIBRE     |10000Mb/s |-               |-               |-               |
|5  |eth1.3003@eth1    |-         |-         |-         |-               |-               |-               |
|6  |eth1.3005@eth1    |-         |-         |-         |-               |-               |-               |
|7  |eth1.3015@eth1    |-         |-         |-         |-               |-               |-               |
|8  |eth1.3115@eth1    |-         |-         |-         |-               |-               |-               |
------------------------------------------------------------------------------------------------------------

 3、注解: 

NO: 序号
Device: 网卡名称
Status: 网卡状态
Speed: 速度 Type: 网卡类型(光口:FIBRE、电口:TP) Ipaddr: ip地址 Mask: 掩码
Bcast: 广播地址
原文地址:https://www.cnblogs.com/pzzning/p/6150527.html