葛廷彬的学习博客 记录学习工作中碰到的问题

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


 1、 使用vi编辑器制作一个脚本

vi hhh      ---创建一个名为hhh的文件复制如下代码,粘贴进hhh文件里边,保存退出。

 

2、 代码如下

(                                                                                                        

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\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 `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;      

 

 

 

 

3、 运行这个脚本

操作:sh hhh

 

 

4、 查看

 

 

 

5、 注释

NO:序号

Device: 网卡名称

Status: 网卡状态

Speed:速度

Type: 网卡类型(光口:FIBRE、电口:TP)

Ipaddr: ip地址

Mask: 掩码

Bcast: 广播地址

 

查看linux的网卡状态编辑专用脚本.txt

 

 

 

 

 

 

 


作者:大个的虾 分类:Centos 浏览:63 评论:0