提示: /proc/net/dev 文件显示的数字就是网络总流量,单位是B(字节)。 可以用当前时刻的数字减去上一秒的数字来求出每秒的网络流量。单位根据实际情况显示为KB或者MB。 而实时显示,只能使用while 做死循环,而且显示结果要一直在屏幕的第一行,可以使用clear实现。
参考脚本:
{{{密码回复可见}}}
参考脚本:
{{{密码回复可见}}}
0
#!/bin/sh
while [ "1" ]
do
eth=$1
RXp=`cat /proc/net/dev |grep $eth|tr : " " |awk '{print $2}'`
TXp=`cat /proc/net/dev |grep $eth|tr : " " |awk '{print $10}'`
sleep 1
RXn=`cat /proc/net/dev |grep $eth|tr : " " |awk '{print $2}'`
TXn=`cat /proc/net/dev |grep $eth|tr : " " |awk '{print $10}'`
RX=`expr $RXn - $RXp`
TX=`expr $TXn - $TXp`
clear
echo -e "$eth RX $RX bye TX $TX bye"
done
while [ "1" ]
do
eth=$1
RXp=`cat /proc/net/dev |grep $eth|tr : " " |awk '{print $2}'`
TXp=`cat /proc/net/dev |grep $eth|tr : " " |awk '{print $10}'`
sleep 1
RXn=`cat /proc/net/dev |grep $eth|tr : " " |awk '{print $2}'`
TXn=`cat /proc/net/dev |grep $eth|tr : " " |awk '{print $10}'`
RX=`expr $RXn - $RXp`
TX=`expr $TXn - $TXp`
clear
echo -e "$eth RX $RX bye TX $TX bye"
done
0
铭哥我脚本中
RX=$((${RXnext}-${RXpre}))
TX=$((${TXnext}-${TXpre}))
改成
RX=$((RXnext-RXpre))
TX=$((TXnext-TXpre))
才可以正常运行,是不是语法有错误?
RX=$((${RXnext}-${RXpre}))
TX=$((${TXnext}-${TXpre}))
改成
RX=$((RXnext-RXpre))
TX=$((TXnext-TXpre))
才可以正常运行,是不是语法有错误?
0
#!/bin/bash
while true;do
s1=`grep 'eth0' /proc/net/dev|awk '{print $10}'`
sleep 1
s2=`grep 'eth0' /proc/net/dev|awk '{print $10}'`
s3=$[$s2-$s1]
echo $s3
done
一直显示 clear放哪呢
while true;do
s1=`grep 'eth0' /proc/net/dev|awk '{print $10}'`
sleep 1
s2=`grep 'eth0' /proc/net/dev|awk '{print $10}'`
s3=$[$s2-$s1]
echo $s3
done
一直显示 clear放哪呢
0
1 #!/bin/bash
2 ##
3 ##
4 ##
5
6 old=0
7 while :;do
8 a=(`cat /proc/net/dev|grep "eth0"`)
9 b=$[ ${a[9]} - $old ]
10 clear
11 echo "$[$b/1024] kb/s"
12 old=${a[9]}
13 sleep 1
14 done
2 ##
3 ##
4 ##
5
6 old=0
7 while :;do
8 a=(`cat /proc/net/dev|grep "eth0"`)
9 b=$[ ${a[9]} - $old ]
10 clear
11 echo "$[$b/1024] kb/s"
12 old=${a[9]}
13 sleep 1
14 done
0
#!/bin/bash
PrevF=`awk -F ":" '{print $2}' /proc/net/dev | tail -1 | awk '{print $1}'`
while :; do
sleep 1
NOWF=`awk -F ":" '{print $2}' /proc/net/dev | tail -1 | awk '{print $1}'`
NetFlow=$[$NOWF-$PrevF]
PrevF=NOWF
echo $NetFlow
done
PrevF=`awk -F ":" '{print $2}' /proc/net/dev | tail -1 | awk '{print $1}'`
while :; do
sleep 1
NOWF=`awk -F ":" '{print $2}' /proc/net/dev | tail -1 | awk '{print $1}'`
NetFlow=$[$NOWF-$PrevF]
PrevF=NOWF
echo $NetFlow
done
0
- #!/bin/bash
- while true;do
- all_last=`cat /proc/net/dev | awk '/wlan0/{print $2}'`
- sleep 1 && clear
- all_now=`cat /proc/net/dev | awk '/wlan0/{print $2}'`
- speed=$[$all_now-$all_last]
- echo $speed B/s
- done
0
本帖最后由 考鸡蛋 于 2015-12-26 22:49 编辑
less /tmp/log/network.log
2015-12-26 22:36:04 eth0 Receive 270 Transmit 770
2015-12-26 22:36:05 eth0 Receive 120 Transmit 716
2015-12-26 22:36:06 eth0 Receive 392 Transmit 1046
2015-12-26 22:36:07 eth0 Receive 346 Transmit 1014
2015-12-26 22:36:08 eth0 Receive 240 Transmit 716
2015-12-26 22:36:09 eth0 Receive 180 Transmit 716
2015-12-26 22:36:10 eth0 Receive 180 Transmit 716
2015-12-26 22:36:11 eth0 Receive 120 Transmit 716
2015-12-26 22:36:12 eth0 Receive 120 Transmit 716
2015-12-26 22:36:13 eth0 Receive 180 Transmit 716
2015-12-26 22:36:14 eth0 Receive 120 Transmit 716
2015-12-26 22:36:15 eth0 Receive 120 Transmit 838
2015-12-26 22:36:16 eth0 Receive 346 Transmit 870
2015-12-26 22:36:17 eth0 Receive 512 Transmit 1812
2015-12-26 22:36:18 eth0 Receive 120 Transmit 716
2015-12-26 22:36:19 eth0 Receive 60 Transmit 504
2015-12-26 22:36:20 eth0 Receive 180 Transmit 716
2015-12-26 22:36:21 eth0 Receive 180 Transmit 716
2015-12-26 22:36:23 eth0 Receive 180 Transmit 626
2015-12-26 22:36:24 eth0 Receive 346 Transmit 918
2015-12-26 22:36:25 eth0 Receive 60 Transmit 716
2015-12-26 22:36:26 eth0 Receive 240 Transmit 716
单位根据实际情况显示为KB或者MB,没有头绪!!!!
- #!/bin/bash
- while :
- do
-
- a1=`grep 'eth0' /proc/net/dev | awk -F ' ' '{print $2}'`
- a2=`grep 'eth0' /proc/net/dev | awk -F ' ' '{print $10}'`
-
- sleep 1
-
- b1=`grep 'eth0' /proc/net/dev | awk -F ' ' '{print $2}'`
- b2=`grep 'eth0' /proc/net/dev | awk -F ' ' '{print $10}'`
- c1=$(($b1-$a1))
- c2=$(($b2-$a2))
-
- #输出到屏幕,注意clear不能置于echo下,否则屏幕只闪烁看不到输出
- #clear
- #echo `date '+%F %H:%M:%S'` eth0 Receive $c1 Transmit $c2
- #
- #
- #将输出重定向到文件
- [ -d /tmp/log ]|| mkdir /tmp/log
- cd /tmp/log
-
- echo `date '+%F %H:%M:%S'` eth0 Receive $c1 Transmit $c2 >> ./network.log
- done
2015-12-26 22:36:04 eth0 Receive 270 Transmit 770
2015-12-26 22:36:05 eth0 Receive 120 Transmit 716
2015-12-26 22:36:06 eth0 Receive 392 Transmit 1046
2015-12-26 22:36:07 eth0 Receive 346 Transmit 1014
2015-12-26 22:36:08 eth0 Receive 240 Transmit 716
2015-12-26 22:36:09 eth0 Receive 180 Transmit 716
2015-12-26 22:36:10 eth0 Receive 180 Transmit 716
2015-12-26 22:36:11 eth0 Receive 120 Transmit 716
2015-12-26 22:36:12 eth0 Receive 120 Transmit 716
2015-12-26 22:36:13 eth0 Receive 180 Transmit 716
2015-12-26 22:36:14 eth0 Receive 120 Transmit 716
2015-12-26 22:36:15 eth0 Receive 120 Transmit 838
2015-12-26 22:36:16 eth0 Receive 346 Transmit 870
2015-12-26 22:36:17 eth0 Receive 512 Transmit 1812
2015-12-26 22:36:18 eth0 Receive 120 Transmit 716
2015-12-26 22:36:19 eth0 Receive 60 Transmit 504
2015-12-26 22:36:20 eth0 Receive 180 Transmit 716
2015-12-26 22:36:21 eth0 Receive 180 Transmit 716
2015-12-26 22:36:23 eth0 Receive 180 Transmit 626
2015-12-26 22:36:24 eth0 Receive 346 Transmit 918
2015-12-26 22:36:25 eth0 Receive 60 Transmit 716
2015-12-26 22:36:26 eth0 Receive 240 Transmit 716
单位根据实际情况显示为KB或者MB,没有头绪!!!!
0
#!/bin/sh
while true
do
one=`cat /proc/net/dev |grep eth0 | awk '{print $2}'`
sleep 1
two=`cat /proc/net/dev |grep eth0 | awk '{print $2}'`
Av=$[ two - one ]
echo "The Average is $Av B."
clear
done
while true
do
one=`cat /proc/net/dev |grep eth0 | awk '{print $2}'`
sleep 1
two=`cat /proc/net/dev |grep eth0 | awk '{print $2}'`
Av=$[ two - one ]
echo "The Average is $Av B."
clear
done
0
#!/bin/bash
while :
do
eth0a=`grep eth0 /proc/net/dev |awk -F ' ' {'print $2'}`
eth0b=`grep eth0 /proc/net/dev |awk -F ' ' {'print $10'}`
sleep 1
eth0aa=`grep eth0 /proc/net/dev |awk -F ' ' {'print $2'}`
eth0bb=`grep eth0 /proc/net/dev |awk -F ' ' {'print $10'}`
eth0in=$((($eth0aa-$eth0a)/1024))
eth0out=$((($eth0bb-$eth0b)/1024))
clear
echo "eth0 in $eth0in kb/s eth0 out $eth0out kb/s"
done
while :
do
eth0a=`grep eth0 /proc/net/dev |awk -F ' ' {'print $2'}`
eth0b=`grep eth0 /proc/net/dev |awk -F ' ' {'print $10'}`
sleep 1
eth0aa=`grep eth0 /proc/net/dev |awk -F ' ' {'print $2'}`
eth0bb=`grep eth0 /proc/net/dev |awk -F ' ' {'print $10'}`
eth0in=$((($eth0aa-$eth0a)/1024))
eth0out=$((($eth0bb-$eth0b)/1024))
clear
echo "eth0 in $eth0in kb/s eth0 out $eth0out kb/s"
done
编辑回复