在Linux系统中,查看网卡流量有多种方法,以下是一些常用的方式:
网卡流量指的是网络接口(网卡)在特定时间段内传输的数据量,通常以字节(Byte)为单位,可以进一步转换为比特(bit)、千字节(KB)、兆字节(MB)、吉字节(GB)等。
ifconfig
命令(需安装net-tools
)sudo apt-get install net-tools # 如果未安装net-tools
ifconfig eth0 | grep 'RX packets\|TX packets'
eth0
是网卡名称,可能需要根据实际情况修改。ip
命令ip -s link show eth0
vnstat
工具sudo apt-get install vnstat
sudo systemctl start vnstat
sudo systemctl enable vnstat
vnstat -l -i eth0
-l
表示实时模式,-i
指定网卡名称。nload
工具sudo apt-get install nload
nload eth0
eth0
、ens33
等),安装必要的工具(如net-tools
、vnstat
)。以下是一个使用Python脚本结合vnstat
获取网卡流量的示例:
import subprocess
def get_network_traffic(interface):
result = subprocess.run(['vnstat', '-l', '-i', interface], capture_output=True, text=True)
lines = result.stdout.split('\n')
for line in lines:
if 'rx' in line or 'tx' in line:
print(line.strip())
get_network_traffic('eth0')
通过以上方法,你可以有效地查看和分析Linux系统中的网卡流量。