提示: /proc/net/dev 文件显示的数字就是网络总流量,单位是B(字节)。 可以用当前时刻的数字减去上一秒的数字来求出每秒的网络流量。单位根据实际情况显示为KB或者MB。 而实时显示,只能使用while 做死循环,而且显示结果要一直在屏幕的第一行,可以使用clear实现。
参考脚本:
{{{密码回复可见}}}
参考脚本:
{{{密码回复可见}}}
0
本帖最后由 wsw13640218682 于 2016-2-15 10:11 编辑
#!/bin/bash
#network
#Steven.Wang
while : ; do
time=`date +%m'-'%d" "%k':'%M`
rx_bef=`ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-`
tx_bef=`ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-`
sleep 2
rx_aft=`ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-`
tx_aft=`ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-`
rx_result=$[(rx_aft-rx_bef)/256]
tx_result=$[(tx_aft-tx_bef)/256]
echo "$time Now_In_Speed: "$rx_result"kbps Now_OUt_Speed: "$tx_result"kbps"
sleep 2
done
#!/bin/bash
#network
#Steven.Wang
while : ; do
time=`date +%m'-'%d" "%k':'%M`
rx_bef=`ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-`
tx_bef=`ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-`
sleep 2
rx_aft=`ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-`
tx_aft=`ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-`
rx_result=$[(rx_aft-rx_bef)/256]
tx_result=$[(tx_aft-tx_bef)/256]
echo "$time Now_In_Speed: "$rx_result"kbps Now_OUt_Speed: "$tx_result"kbps"
sleep 2
done
0
#!/bin/bash
while :
do
m=`cat /proc/net/dev |grep eth0 |awk -F' ' '{print $2}'`
echo $m
m1=$[$m/1024]
sleep 1
n=`cat /proc/net/dev |grep eth0 |awk -F' ' '{print $2}'`
n1=$[$n/1024]
n3=$[$n1-$m1]
echo $n
echo "您已经使用了$n3 kb的流量"
sleep 2
clear
done
while :
do
m=`cat /proc/net/dev |grep eth0 |awk -F' ' '{print $2}'`
echo $m
m1=$[$m/1024]
sleep 1
n=`cat /proc/net/dev |grep eth0 |awk -F' ' '{print $2}'`
n1=$[$n/1024]
n3=$[$n1-$m1]
echo $n
echo "您已经使用了$n3 kb的流量"
sleep 2
clear
done
0
sh:
- #!/bin/bash
- ## 单位转换
- kb=1024
- mb=1048576
- gb=1073741824
- ## 处理命令行参数
- if [[ $# -gt 1 || ($# -eq 1 && $1 != "-h") ]]; then
- echo "usage: `basename $0` [-h]"
- exit 1
- fi
- ## 用数组存储不同网络设备的数据
- flow_sec(){
- arr_dev=($(cat /proc/net/dev |awk '/[0-9]/{print $1}'))
- arr_rec1=($(cat /proc/net/dev |awk '/[0-9]/{print $2}'))
- arr_tra1=($(cat /proc/net/dev |awk '/[0-9]/{print $10}'))
- sleep $1
- arr_rec2=($(cat /proc/net/dev |awk '/[0-9]/{print $2}'))
- arr_tra2=($(cat /proc/net/dev |awk '/[0-9]/{print $10}'))
- ## 必须在间隔时间后清屏
- clear
- }
- ## 显示人类可读的单位
- human_fmt(){
- if [[ $1 -lt $kb ]]; then
- tmp="$1"B
- elif [[ $1 -ge $kb && $1 -lt $mb ]]; then
- tmp=`expr $1 / $kb`KB
- elif [[ $1 -ge $mb && $1 -lt $gb ]]; then
- tmp=`expr $1 / $mb`MB
- else
- tmp=`expr $1 / $gb`GB
- fi
- }
- ## 主循环
- while :; do
- idx=0
- flow_sec 1
- echo "DEVICE RECEIVE TRANSMIT"
- for i in ${arr_dev[@]}; do
- rec=`expr ${arr_rec2[$idx]} - ${arr_rec1[$idx]}`
- tra=`expr ${arr_tra2[$idx]} - ${arr_tra1[$idx]}`
- if [[ $1 == "-h" ]]; then
- human_fmt $rec; rec=$tmp
- human_fmt $tra; tra=$tmp
- echo "$i $rec, $tra"
- else
- echo "$i $rec, $tra"
- fi
- let idx++
- done
- done
0
#!/bin/bash
##
while :;do
a=`cat /proc/net/dev|grep eth0|awk '{print $10}'`
sleep 1
b=`cat /proc/net/dev|grep eth0|awk '{print $10}'`
c=$[$b-$a]
clear
echo "$c"
done
##
while :;do
a=`cat /proc/net/dev|grep eth0|awk '{print $10}'`
sleep 1
b=`cat /proc/net/dev|grep eth0|awk '{print $10}'`
c=$[$b-$a]
clear
echo "$c"
done
0
#!/bin/bash
while :
do
m=`cat /proc/net/dev |grep eth0 |awk -F' ' '{print $2}'`
echo $m
m1=$[$m/1024]
sleep 1
n=`cat /proc/net/dev |grep eth0 |awk -F' ' '{print $2}'`
n1=$[$n/1024]
n3=$[$n1-$m1]
echo $n
echo "您已经使用了$n3 kb的流量"
sleep 2
clear
done
while :
do
m=`cat /proc/net/dev |grep eth0 |awk -F' ' '{print $2}'`
echo $m
m1=$[$m/1024]
sleep 1
n=`cat /proc/net/dev |grep eth0 |awk -F' ' '{print $2}'`
n1=$[$n/1024]
n3=$[$n1-$m1]
echo $n
echo "您已经使用了$n3 kb的流量"
sleep 2
clear
done
0
#!/bin/bash
#输出每秒的网卡流量,并实时显示,并且结果要一直显示在屏幕第一行
function result(){
nat_old=`cat /proc/net/dev|grep 'eth0'|awk '{print $2}'`
nat_kbo=`expr $nat_old / 1024`
t_old=`date +%s`
#echo "`date +%s` 流量 is " $nat_kb MB
t_new=`date -d "+1 sec" +%s`
nat_new=`cat /proc/net/dev|grep 'eth0'|awk '{print $2}'`
nat_kbn=`expr $nat_new / 1024`
t_between=`expr $t_new - $t_old`
nat_between=`expr $nat_new - $nat_old`
nat_new=`expr $nat_between / $t_between`
clear
echo "time at `date +%s` nat is" $nat_new kb.
}
while :
do
result
done
#输出每秒的网卡流量,并实时显示,并且结果要一直显示在屏幕第一行
function result(){
nat_old=`cat /proc/net/dev|grep 'eth0'|awk '{print $2}'`
nat_kbo=`expr $nat_old / 1024`
t_old=`date +%s`
#echo "`date +%s` 流量 is " $nat_kb MB
t_new=`date -d "+1 sec" +%s`
nat_new=`cat /proc/net/dev|grep 'eth0'|awk '{print $2}'`
nat_kbn=`expr $nat_new / 1024`
t_between=`expr $t_new - $t_old`
nat_between=`expr $nat_new - $nat_old`
nat_new=`expr $nat_between / $t_between`
clear
echo "time at `date +%s` nat is" $nat_new kb.
}
while :
do
result
done
编辑回复