原 Linux环境下,shell脚本一键获取主机网卡信息(含网卡详情,网卡绑定等内容)
脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | cat > /tmp/get_network_info.sh <<"EOF" #!/bin/bash # 表头 printf "%-20s %-10s %-10s %-18s %-18s %-18s %-10s %-10s %-10s %-10s %-8s %-10s\n" "Interface" "Type" "Type2" "MAC_Address" "IP_Address" "Gateway" "State" "RX" "TX" "Port_Type" "MTU" "General_State" echo "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" for interface in $(ls /sys/class/net/) do if [ ! -d "/sys/class/net/$interface" ]; then continue fi # 基础信息 mac=$(cat /sys/class/net/$interface/address 2>/dev/null || echo "N/A") ip=$(ip -4 addr show $interface | grep -oP '(?<=inet\s)\d+(\.\d+){3}' || echo "N/A") state=$(ip link show $interface | grep -oP '(?<=state\s)\w+' || echo "UNKNOWN") rx_bytes=$(cat /sys/class/net/$interface/statistics/rx_bytes 2>/dev/null || echo "0") tx_bytes=$(cat /sys/class/net/$interface/statistics/tx_bytes 2>/dev/null || echo "0") # 转换 RX 和 TX if (( rx_bytes >= 1024**4 )); then rx=$(awk "BEGIN {printf \"%.2f TB\", $rx_bytes / (1024^4)}") elif (( rx_bytes >= 1024**3 )); then rx=$(awk "BEGIN {printf \"%.2f GB\", $rx_bytes / (1024^3)}") else rx=$(awk "BEGIN {printf \"%.2f MB\", $rx_bytes / (1024^2)}") fi if (( tx_bytes >= 1024**4 )); then tx=$(awk "BEGIN {printf \"%.2f TB\", $tx_bytes / (1024^4)}") elif (( tx_bytes >= 1024**3 )); then tx=$(awk "BEGIN {printf \"%.2f GB\", $tx_bytes / (1024^3)}") else tx=$(awk "BEGIN {printf \"%.2f MB\", $tx_bytes / (1024^2)}") fi type="Virtual" [ -e "/sys/class/net/$interface/device" ] && type="Physical" mtu=$(cat /sys/class/net/$interface/mtu 2>/dev/null || echo "N/A") # 获取 GENERAL.TYPE 信息 general_type=$(nmcli device show $interface 2>/dev/null | grep -i "GENERAL.TYPE" | awk '{print $2}' || echo "N/A") # 获取 GENERAL.STATE 信息 general_state=$(nmcli device show $interface 2>/dev/null | grep -i "GENERAL.STATE" | awk '{print $2,$3,$4,$5,$6}' || echo "N/A") # 获取网关地址 gateway=$(nmcli device show $interface 2>/dev/null | grep -i "IP4.GATEWAY" | awk '{print $2}' || echo "N/A") # 判断光口或电口 port_type="Unknown" if command -v ethtool >/dev/null; then port=$(ethtool $interface 2>/dev/null | grep "Port" | awk '{print $2}') case "$port" in FIBRE) port_type="Optical" ;; TP) port_type="Electrical" ;; esac fi # 打印结果 printf "%-20s %-10s %-10s %-18s %-18s %-18s %-10s %-10s %-10s %-10s %-8s %-10s\n" "$interface" "$type" "$general_type" "$mac" "$ip" "$gateway" "$state" "$rx" "$tx" "$port_type" "$mtu" "$general_state" done ########################### 网卡绑定内容 # 检查 /proc/net/bonding/bond* 是否存在 if ls /proc/net/bonding/bond* &> /dev/null; then echo -e "\n" echo -e "#########网卡绑定内容#########\n" # 输出表头 echo -e "Bond Name Slave Interface MII Status Speed Duplex Link Failure Count Permanent HW addr Slave queue ID" echo -e "-------------------------------------------------------------------------------------------------------------------------------------" # 遍历所有 /proc/net/bonding/bond* 文件 for bond_file in /proc/net/bonding/bond*; do # 获取 bond 文件名(例如:bond0, bond1 等) bond_name=$(basename "$bond_file") # 解析并输出数据 awk -v bond_name="$bond_name" ' BEGIN { iface=""; mii=""; speed=""; duplex=""; link_failure_count=""; hw_addr=""; queue_id=""; } # 捕获字段 /Slave Interface/ { iface=$3; } /MII Status/ { mii=$3; } /Speed/ { speed=$2; } /Duplex/ { duplex=$2; } /Link Failure Count/ { link_failure_count=$4; } /Slave queue ID/ { queue_id=$4; } /Permanent HW addr/ { hw_addr=$NF; } # 打印所有捕获的字段并检查 /Slave queue ID/ { printf "%-15s %-23s %-10s %-10s %-10s %-18s %-25s %-12s\n", bond_name, iface, mii, speed, duplex, link_failure_count, hw_addr, queue_id; } ' "$bond_file" done # 输出表头 echo -e "\n" printf "|------------------------------|-------------------------------------|\n" printf "| %-30s | %-35s |\n" "属性" " 值" printf "|------------------------------|-------------------------------------|\n" # 遍历所有 /proc/net/bonding/bond* 文件 for bond_file in /proc/net/bonding/bond*; do printf "| %-30s | %-35s |\n" "文件" "$bond_file" # 初始化表格变量 bonding_mode="" primary_slave="" active_slave="" mii_status="" mii_polling="" up_delay="" down_delay="" peer_delay="" # 读取文件内容并提取信息 while IFS= read -r line; do case "$line" in *"Bonding Mode"*) bonding_mode="${line#*:}" ;; *"Primary Slave"*) primary_slave="${line#*:}" ;; *"Currently Active Slave"*) active_slave="${line#*:}" ;; *"MII Status"*) mii_status="${line#*:}" ;; *"MII Polling Interval"* ) mii_polling="${line#*:}" ;; *"Up Delay"* ) up_delay="${line#*:}" ;; *"Down Delay"* ) down_delay="${line#*:}" ;; *"Peer Notification Delay"*) peer_delay="${line#*:}" ;; esac done < "$bond_file" # 输出表格数据 printf "| %-28s | %-35s |\n" "Bonding Mode" "$bonding_mode" printf "| %-28s | %-35s |\n" "Primary Slave" "$primary_slave" printf "| %-28s | %-35s |\n" "Currently Active Slave" "$active_slave" printf "| %-28s | %-35s |\n" "MII Status" "$mii_status" printf "| %-28s | %-35s |\n" "MII Polling Interval (ms)" "$mii_polling" printf "| %-28s | %-35s |\n" "Up Delay (ms)" "$up_delay" printf "| %-28s | %-35s |\n" "Down Delay (ms)" "$down_delay" printf "| %-28s | %-35s |\n" "Peer Notification Delay (ms)" "$peer_delay" printf "|------------------------------|-------------------------------------|\n" done fi EOF sudo chmod +x /tmp/get_network_info.sh sudo \cp /tmp/get_network_info.sh /usr/bin/ sudo bash /tmp/get_network_info.sh |
结果示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@mdw tmp]# chmod +x /tmp/get_network_info.sh [root@mdw tmp]# bash /tmp/get_network_info.sh Interface Type Type2 MAC_Address IP_Address Gateway State RX TX Port_Type MTU General_State ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- bond0 Virtual bond 84:16:0c:da:82:90 12.12.11.76 12.12.11.264 UP 226.20 TB 466.42 TB Unknown 1600 100 (connected) bond1 Virtual bond 84:16:0c:da:82:91 12.12.14.112 -- UP 168.28 MB 1.61 GB Unknown 1600 100 (connected) em1 Physical ethernet 24:72:6a:a2:1c:e4 N/A DOWN 0.00 MB 0.00 MB Unknown 1600 20 (unavailable) em2 Physical ethernet 24:72:6a:a2:1c:e6 N/A DOWN 0.00 MB 0.00 MB Unknown 1600 20 (unavailable) em2 Physical ethernet 24:72:6a:a2:1c:e6 N/A DOWN 0.00 MB 0.00 MB Unknown 1600 20 (unavailable) em4 Physical ethernet 24:72:6a:a2:1c:e7 N/A DOWN 0.00 MB 0.00 MB Unknown 1600 20 (unavailable) lo Virtual loopback 00:00:00:00:00:00 127.0.0.1 -- UNKNOWN 116.20 TB 116.20 TB Unknown 66626 10 (unmanaged) p2p1 Physical ethernet 84:16:0c:e0:bb:00 N/A -- UP 118.86 TB 190.06 TB Optical 1600 100 (connected) p2p2 Physical ethernet 84:16:0c:e0:bb:01 N/A -- UP 79.14 MB 824.89 MB Optical 1600 100 (connected) p3p1 Physical ethernet 84:16:0c:da:82:90 N/A -- UP 207.22 TB 266.29 TB Optical 1600 100 (connected) p3p2 Physical ethernet 84:16:0c:da:82:91 N/A -- UP 79.14 MB 824.89 MB Optical 1600 100 (connected) |


