.问题:显示网卡的数据包流量
代码:
#!/bin/bash
# 获取所有网卡名称
interfaces=$(ifconfig -a | grep -o -E '^[A-Za-z0-9]+' | sed '/^lo$/d')
echo "Traffic information for all interfaces:"
echo "--------------------------------------"
# 遍历每个网卡并显示流量信息
for interface in $interfaces; do
echo "Interface: $interface"
echo "-----------------------"
# 获取网卡的流量信息
rx_bytes=$(cat /sys/class/net/$interface/statistics/rx_bytes)
tx_bytes=$(cat /sys/class/net/$interface/statistics/tx_bytes)
# 转换为人类可读的单位
rx_human=$(numfmt --to=iec $rx_bytes)
tx_human=$(numfmt --to=iec $tx_bytes)
echo "Received: $rx_human"
echo "Transmitted: $tx_human"
echo "-----------------------"
done
结果: