Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1296274
  • 博文数量: 436
  • 博客积分: 7854
  • 博客等级: 少将
  • 技术积分: 3225
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-18 16:30
文章分类

全部博文(436)

文章存档

2013年(2)

2012年(56)

2011年(70)

2010年(308)

分类:

2010-05-30 18:11:29

1.脚本方式
 

语法:[--help] [-i] [-d]

#!/bin/bash

VERSION="1.0.0.1"
eth=""
sec=0

help()
{
  printf "Version: $VERSION \n\
  Usage: $0 [-i interface] [-d time]\n\
  \t-i|--interface : The needs of the inquiries Interface Network Card (such as eth0, eth1...)\n\
  \t-d|--time : Delay time(seconds)\n"
;
}


echo "$*"
for dummy
do
 case $1 in
  --help) help; exit 1;;
  -i|--interface) shift; eth=$1;;
  -d|--time) shift; sec=$1;;
 esac
 shift
done


#echo -n "Please enter the needs of the inquiries Interface Network Card (such as eth0, eth1...): "
#read eth
echo "Your input data is:"$eth
#echo -n "Input delay time(seconds):"
#read sec
echo "You are calculated within "$sec" seconds of the average flow, Please wait... "
infirst=$(awk '/'$eth'/{print $1 }' /proc/net/dev |sed 's/'$eth'://')
outfirst=$(awk '/'$eth'/{print $10 }' /proc/net/dev)
sumfirst=$(($infirst+$outfirst))
sleep $sec"s"
inend=$(awk '/'$eth'/{print $1 }' /proc/net/dev |sed 's/'$eth'://')
outend=$(awk '/'$eth'/{print $10 }' /proc/net/dev)
sumend=$(($inend+$outend))
sum=$(($sumend-$sumfirst))
echo $sec"(s) total flow is: "$sum" bytes"
aver=$(($sum/$sec))
echo "The average flow is: "$aver" bytes/sec"

另外2中截取网卡数据方法

cat /proc/net/dev|grep eth0 | awk '{print$1}'|sed -s 's/eth0://g'

cat /proc/net/dev | grep eth0 | sed  's=^.*:=='  | awk '{ print $1 }'

http://kb.cnblogs.com/a/884087/
2.c语言实现

 

仅是通过/proc/net/dev读取其中的数据.(转载)
#include
#include
#include

void skipline(FILE *f)
{
  int ch;
  do {
    ch = getc(f);
  } while ( ch != 'n' && ch != EOF );
}

int main(int argc, char *argv[])
{
  FILE *pnd;
  char buffer[BUFSIZ];
  char *interface;
  struct ifinfo {
    char name[8];
    unsigned int r_bytes, r_pkt, r_err, r_drop, r_fifo, r_frame;
    unsigned int r_compr, r_mcast;
    unsigned int x_bytes, x_pkt, x_err, x_drop, x_fifo, x_coll;
    unsigned int x_carrier, x_compr;
  } ifc;
  unsigned long long bin, bout, lbin, lbout;
  int first;

  if ( argc != 2 ) {
    fprintf(stderr, "Usage: %s interfacen", argv[0]);
    exit(1);
  }

  interface = argv[1];

  first = 1;
  lbin = 0; lbout = 0;

  while ( 1 ) {
    pnd = fopen("/proc/net/dev", "r");
    if ( !pnd ) {
      fprintf(stderr, "%s: /proc/net/dev: %s", argv[0], strerror(errno));
      exit(1);
    }

    /* Skip header */
    skipline(pnd);
    skipline(pnd);

    /* Get interface info */
    do {
      if ( fscanf(pnd, " %6[^:]:%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
                  &ifc.name,
                  &ifc.r_bytes, &ifc.r_pkt, &ifc.r_err, &ifc.r_drop,
                  &ifc.r_fifo, &ifc.r_frame, &ifc.r_compr, &ifc.r_mcast,
                  &ifc.x_bytes, &ifc.x_pkt, &ifc.x_err, &ifc.x_drop,
                  &ifc.x_fifo, &ifc.x_coll, &ifc.x_carrier, &ifc.x_compr)
           != 16 ) {
        exit(200);
      }
      skipline(pnd);
    } while ( strcmp(ifc.name, interface) );

    bin = ifc.r_bytes + (lbin & ~0xffffffffULL);
    bout = ifc.x_bytes + (lbout & ~0xffffffffULL);

    if ( bin < lbin )
      bin += (1ULL << 32);
    if ( bout < lbout )
      bout += (1ULL << 32);

    if ( !first ) {
      printf("%d %Lu %Lun", time(NULL), (bout-lbout)*8, (bin-lbin)*8);
      fflush(stdout);
    } else {
      first = 0;
    }

    lbin = bin; lbout = bout;

    fclose(pnd);

    sleep(1);
  }
}


http://blog.csdn.net/alin0725/archive/2008/03/17/2192889.aspx
阅读(727) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~