实时查看系统流量的Shell脚本分享
程序员文章站
2023-11-09 11:35:58
复制代码 代码如下:
#!/bin/bash
while [ "1" ]
do
eth=$1
rxpre=...
复制代码 代码如下:
#!/bin/bash
while [ "1" ]
do
eth=$1
rxpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
txpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
sleep 1
rxnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
txnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
clear
echo -e "\t rx `date +%k:%m:%s` tx"
rx=$((${rxnext}-${rxpre}))
tx=$((${txnext}-${txpre}))
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 "$eth \t $rx $tx "
done