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

 

 

http://blog.chinaunix.net/uid-25524253-id-2783110.html

你可能感兴趣的:(linux,流量监控)