IT猫扑网:您身边最放心的安全下载站! 最新更新| 软件分类| 专题汇总| 手机版

您当前所在位置:IT猫扑网 > 操作系统 > LINUX > 三个监控linux网卡流量脚本

三个监控linux网卡流量脚本

时间:2015-06-28 00:00 来源:IT猫扑网|http://www.itmop.com/ 作者:网管联盟 我要评论(0)

脚本1:
#!/bin/bash
e0_in_old=$(ifconfig eth0 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |head -n1)
e0_out_old=$(ifconfig eth0 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |tail -n1)>e0_total_old=$(($e0_in_old + $e0_out_old))
e1_in_old=$(ifconfig eth1 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |head -n1)
e1_out_old=$(ifconfig eth1 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |tail -n1)
e1_total_old=$(($e1_in_old + $e1_out_old))
while true
do
sleep 1
e0_in_new=$(ifconfig eth0 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |head -n1)
e0_out_new=$(ifconfig eth0 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |tail -n1)
e0_total_new=$(($e0_in_new + $e0_out_new))
e1_in_new=$(ifconfig eth1 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |head -n1)
e1_out_new=$(ifconfig eth1 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |tail -n1)
e1_total_new=$(($e1_in_new + $e1_out_new))
e0_in=$((($e0_in_new - $e0_in_old) * 8))
e0_out=$((($e0_out_new - $e0_out_old) * 8))
e0_total=$((($e0_total_new - $e0_total_old) * 8))
e1_in=$((($e1_in_new - $e1_in_old) * 8))
e1_out=$((($e1_out_new - $e1_out_old) * 8))
e1_total=$((($e1_total_new - $e1_total_old) * 8))
echo -e ð0 IN: $e0_in t b/s OUT: $e0_out t b/s TOTAL: $e0_total t b/s&
echo -e ð1 IN: $e1_in t b/s OUT: $e1_out t b/s TOTAL: $e1_total t b/sn&
e0_in_old=$e0_in_new
e0_out_old=$e0_out_new
e0_total_old=$e0_total_new
e1_in_old=$e1_in_new
e1_out_old=$e1_out_new
e1_total_old=$e1_total_new
done
 
exit 0
 
 
脚本2:
#!/bin/bash
#  osdba 2008.10.22 monitor the interface's network traffic.
if [ $# -ne 3 ];then
   echo Useage : $0 interface interval count
   echo Example: $0 eth0 2 10
   exit
fi
eth=$1
count=$3
interval=$2
inbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $2}')
if [ -z &$inbytesfirst& ];then
    echo The network interface $eth is not exits!
    exit 1;
fi
outbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $10}')
inpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $3}')
outpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $11}')
sleep $interval&s&
i=0
while [ &$i& -lt &$count& ]
do
   inbytesend=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $2}')
   outbytesend=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $10}')
   inpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $3}')
   outpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $11}')
   bytesin=$(($inbytesend-$inbytesfirst))
   bytesout=$(($outbytesend-$outbytesfirst))
   packetsin=$(($inpacketsend-$inpacketsfirst))
   packetsout=$(($outpacketsend-$outpacketsfirst))
   if [ &$bytesin& -lt &0& ];then
      bytesin=$((4294967295-$inbytesfirst+$inbytesend))
      #echo bytesin $bytesin $inbytesfirst $inbytesend
   fi
   if [ &$bytesout& -lt &0& ];then
      bytesout=$((4294967295-$outbytesfirst+$outbytesend))
      #echo bytesout $bytesout $outbytesfirst $outbytesend
   fi
   if [ &$packetsin& -lt &0& ];then
      packetsin=$((4294967295-$inpacketsfirst+$inpacketsend))
      #echo packetsin $packetsin $inpacketsfirst $inpacketsend
   fi
   if [ &$packetsout& -lt &0& ];then
      packetsout=$((4294967295-$outpacketsfirst+$outpacketsend))
      #echo packetsout $packetsout $outpacketsfirst $outpacketsend
   fi
   bytesin=$(($bytesin/$interval))
   bytesout=$(($bytesout/$interval))
   packetsin=$(($packetsin/$interval))
   packetsout=$(($packetsout/$interval))
   sumbytesin=$(($sumbytesin+$bytesin))
   sumbytesout=$(($sumbytesout+$bytesout))
   sumpacketsin=$(($sumpacketsin+$packetsin))
   sumpacketsout=$(($sumpacketsout+$packetsout))
   if [ $(($i%20)) -eq 0 ];then
      echo & ifname   | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s&
      echo &--------- | ---------- ----------- | ----------- ------------ | ------------ -------------&
   fi
   echo $eth $bytesin $bytesout $packetsin $packetsout |awk '{printf(&%9s | %10d %11d | %11d %12d | %12d %13dn&,$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'
   inbytesfirst=$inbytesend
   outbytesfirst=$outbytesend
   inpacketsfirst=$inpacketsend
   outpacketsfirst=$outpacketsend
  
   i=$(($i+1))
   sleep $interval&s&
done
sumbytesin=$(($sumbytesin/$i))
sumbytesout=$(($sumbytesout/$i))
sumpacketsin=$(($sumpacketsin/$i))
sumpacketsout=$(($sumpacketsout/$i))
echo &--------- | ---------- ----------- | ----------- ------------ | ------------ -------------&
echo Average $sumbytesin $sumbytesout $sumpacketsin $sumpacketsout |awk '{printf(&%9s | %10d %11d | %11d %12d | %12d %13dn&,$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'

 
脚本3:
#!/bin/bash
# test network width
function usage
{
 echo &Usage: $0?? &
 echo &?????? e.g. $0 eth0 2&
 exit 65
}
if [ $# -lt 2 ];then
usage
fi
typeset in in_old dif_in
typeset out out_old dif_out
typeset timer
typeset eth
eth=$1
timer=$2
in_old=$(cat /proc/net/dev | grep $eth | sed -e &s/(.*):(.*)/2/g& | awk ' { print $1 }' )
out_old=$(cat /proc/net/dev | grep $eth | sed -e &s/(.*):(.*)/2/g& | awk ' { print $9 }' )
while true
do
sleep ${timer}
in=$(cat /proc/net/dev | grep $eth | sed -e &s/(.*):(.*)/2/g& | awk ' { print $1 }' )
out=$(cat /proc/net/dev | grep $eth | sed -e &s/(.*):(.*)/2/g& | awk ' { print $9 }' )
dif_in=$(((in-in_old)/timer))
dif_out=$(((out-out_old)/timer))
echo &IN: ${dif_in} Byte/s OUT: ${dif_out} Byte/s&
in_old=${in}
out_old=${out}
done
exit 0
 

脚本1:
#!/bin/bash
e0_in_old=$(ifconfig eth0 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |head -n1)
e0_out_old=$(ifconfig eth0 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |tail -n1)>e0_total_old=$(($e0_in_old + $e0_out_old))
e1_in_old=$(ifconfig eth1 |grep bytes |awk '{print $2& &$6}' |egrep -o '[0-9]+' |head -n1)
e1_out_old=$(ifconfig eth1 |grep bytes |awk '{pr

关键词标签:linux,网卡

相关阅读 安装红帽子RedHat Linux9.0操作系统教程 Tomcat9.0如何安装_Tomcat9.0环境变量配置方法 多种操作系统NTP客户端配置 Linux操作系统修改IP Linux实现SCSI硬盘热插拔及在线识别 Linux下用CDMA modem拨号上网

文章评论
发表评论

热门文章 安装红帽子RedHat Linux9.0操作系统教程 安装红帽子RedHat Linux9.0操作系统教程 Linux服务器:设计高性能网站架构-LLMP Linux服务器:设计高性能网站架构-LLMP 使用Clonezilla迁移到虚拟Linux环境 使用Clonezilla迁移到虚拟Linux环境 Linux上的MRTG流量监控中心 Linux上的MRTG流量监控中心 Linux 双网卡绑定一个IP原理及实现 Linux 双网卡绑定一个IP原理及实现 linux和windows等系统远程控制ubuntu桌面 linux和windows等系统远程控制ubuntu桌面

相关下载

人气排行 Linux下获取CPUID、硬盘序列号与MAC地址 dmidecode命令查看内存型号 linux tc实现ip流量限制 安装红帽子RedHat Linux9.0操作系统教程 linux下解压rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 关机、重启、注销 命令 查看linux服务器硬盘IO读写负载 linux命令行浏览器的使用方法 Linux NFS服务固定端口及防火墙配置 U盘安装Ubuntu 10.04 Linux清除用户登录记录和命令历史方法