Linux下网络流量监控shell脚本

1977阅读 0评论2011-09-11 Linuxer2011
分类:LINUX

  1. #!/bin/bash
  2. if [ -z "$1" ]
  3. then
  4.     echo "Oops...Please specify network device name."
  5.     echo "Usage:    `basename $0` device"
  6.     echo "eg.     `basename $0` eth0"
  7.     exit 1
  8. else
  9.     dev_name="$1"
  10. fi

  11. /sbin/ifconfig $dev_name 2> /dev/null > /dev/null
  12. if [ $? -ne 0 ]
  13. then
  14.     echo "Oops...Cannot find the specified device."
  15.     exit 1
  16. fi

  17. while true
  18. do
  19.     rece_all1=`/sbin/ifconfig $dev_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
  20.     send_all1=`/sbin/ifconfig $dev_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
  21.     sleep 1
  22.     rece_all2=`/sbin/ifconfig $dev_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`
  23.     send_all2=`/sbin/ifconfig $dev_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`
  24.     rece=`expr $rece_all2 - $rece_all1`
  25.     send=`expr $send_all2 - $send_all1`
  26.     clear
  27.     echo "Last second receive:$(($rece/1024)) KB    Last second Send:$(($send/1024)) KB"
  28.     echo "Total receive:$(($rece_all2/1024)) KB ($(($rece_all2/1024/1024))MB)    Total send:$(($send_all2/1024)) KB ($(($send_all2/1024/1024))MB)"
  29. done
上一篇:Linux下用tar进行系统备份脚本
下一篇:Linux操作系统Vmstat命令列出的属性详解