【shell】shell脚本获取linux所有网口及连接状态

工作上需要配置管理多个服务器,而服务器上基本上都接着有多根网线。

以前通过ethtool 网卡名称单个查询,操作比较重复且容易“吐血”

[root@server01 /]# ethtool enp4s0f0
Settings for enp4s0f0:
        Supported ports: [ FIBRE ]
        Supported link modes:   10000baseT/Full 
        Supported pause frame use: Symmetric
        Supports auto-negotiation: No
        Supported FEC modes: Not reported
        Advertised link modes:  10000baseT/Full 
        Advertised pause frame use: Symmetric
        Advertised auto-negotiation: No
        Advertised FEC modes: Not reported
        Speed: Unknown!
        Duplex: Unknown! (255)
        Port: Other
        PHYAD: 0
        Transceiver: external
        Auto-negotiation: off
        Supports Wake-on: d
        Wake-on: d
        Current message level: 0x00000007 (7)
                               drv probe link
        Link detected: no ----只是为了查看这个字段(yes为已接网线,no为未接)

遂编写脚本一次性查询所有网卡及其连接状态,如下所示:

[root@server01 /]# ./get_eth_status.sh 
All INTERFACE NETWORK: enp11s0 enp4s0f0 enp4s0f1 enp9s0f0 enp9s0f1
1 enp11s0       Link detected: no
2 enp4s0f0      Link detected: no
3 enp4s0f1      Link detected: no
4 enp9s0f0      Link detected: yes
5 enp9s0f1      Link detected: yes

代码如下:

#/bin/bash
int_list=$(ls /sys/class/net/ | grep -v "`ls /sys/devices/virtual/net/`")
num=0
echo "All INTERFACE NETWORK: "$int_list
for i in ${int_list}
do
  if  [[ $i == docke* || $i == lo* || $i == vir* ]]
  then
    continue
  else
    num=`expr ${num} + 1`
    printf ${num}" "${i}"\t"
    echo `ethtool ${i}| grep dete`
  fi
done

 

posted on 2022-04-22 15:11  卓某  阅读(1231)  评论(0编辑  收藏  举报

导航