用Linux创建一个脚本用于查看网卡实时流量
时间: 2024-02-06 18:08:56 浏览: 31
你可以使用以下脚本来查看网卡实时流量:
```
#!/bin/bash
# This script monitors network traffic on a specified interface
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <interface>"
exit 1
fi
interface=$1
while true; do
rx1=$(cat /sys/class/net/$interface/statistics/rx_bytes)
tx1=$(cat /sys/class/net/$interface/statistics/tx_bytes)
sleep 1
rx2=$(cat /sys/class/net/$interface/statistics/rx_bytes)
tx2=$(cat /sys/class/net/$interface/statistics/tx_bytes)
rx_bytes=$((rx2 - rx1))
tx_bytes=$((tx2 - tx1))
echo "$(date) - RX: $rx_bytes bytes/sec - TX: $tx_bytes bytes/sec"
done
```
将上述脚本保存为一个文件,例如 `netmon.sh`,然后将其赋予执行权限:
```
chmod +x netmon.sh
```
运行脚本时,指定要监视的网卡名称作为参数。例如,要监视 `eth0` 网卡的流量:
```
./netmon.sh eth0
```
脚本将每秒更新一次实时的网络流量数据。按 `Ctrl + C` 终止脚本。