加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

网上扒的查看网卡流量的shell

发布时间:2020-12-15 07:06:34 所属栏目:安全 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ?#!/bin/bash# osdba 2008.10.22 monitor the interface's network traffic.# Zeuslion 2009.08.29.if [ $# -ne 3 ];then echo Useage : $0 interface

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

?#!/bin/bash
# osdba 2008.10.22 monitor the interface's network traffic.
#   Zeuslion 2009.08.29.
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"
while [ "1" ]
do

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 "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
      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",$5)}'
done

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读