Linux命令行监控网口流量


1.ifconfig [网卡名]

ifconfig eth0

.

2. /proc/net/dev

cat /proc/net/dev

.

3. 使用watch命令配合 ifconfig, cat /proc/net/dev, 避免手动一次次执行

watch -n 1 "ifconfig eth0"
# or
watch -n 1 "cat /proc/net/dev"

.

4. shell脚本

#!/bin/bash
ethn=$1
while true
do
 RX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
 sleep 1
 RX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
 
 clear
 echo -e "\t RX `date +%k:%M:%S` TX"

 RX=$((${RX_next}-${RX_pre}))
 TX=$((${TX_next}-${TX_pre}))
 
 if [[ $RX -lt 1024 ]];then
 RX="${RX}B/s"
 elif [[ $RX -gt 1048576 ]];then
 RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
 else
 RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
 fi
 
 if [[ $TX -lt 1024 ]];then
 TX="${TX}B/s"
 elif [[ $TX -gt 1048576 ]];then
 TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
 else
 TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
 fi
 
 echo -e "$ethn \t $RX $TX "
done

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM