linux 查看网络流量来源_linux下查看当前网络流量

cat /proc/net/dev 通过这个文件可以看到网络流量

[@more@]查看主机流量,ok,以KB为单位,一个nohup命令让他跑这么几天, 取出来分析便是。

/proc/net/dev,就是分析状态。

零、嘛是/proc/net/dev:

/proc的那些文件的操作函数很特殊,不需要直接对/proc文件的内容进行写操作来添加内容,就能用充满魔力的show函数来给用户提供信息。听不懂,就当神话来听,知道怎么用这个东东就行。

# cat /proc/net/dev

Inter-| Receive | Transmit

face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed

lo:1219485788609 620698175 0 0 0 0 0 0 1219485788609 620698175 0 0 0 0 0 0

eth0:182025224125 332789797 0 0 0 0 0 1946 226393677809 315770454 0 0 0 0 0 0

eth1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

sit0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

tunl0:18186156911 209305063 0 0 0 0 0 0 0 0 0 0 0 0 0 0

ok,你要用的就是第1,2,9,10列。

一、一般服务器就一个网卡,bash版的够用了:

#! /bin/bash

while true

do

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

send1=`cat /proc/net/dev|grep eth0 | awk '{print$9}'`

sleep 300

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

receive_cnt=`expr $receive2 - $receive1`

receive_cnt=`expr $receive_cnt / 1024`

send2=`cat /proc/net/dev|grep eth0 | awk '{print$9}'`

send_cnt=`expr $send2 - $send1`

send_cnt=`expr $send_cnt / 1024`

echo `date` 'eth0 Receive Bytes:'$receive_cnt >>ok.txt

echo `date` 'eth0 Send Bytes:' $send_cnt >>ok.txt

done

二、如果你多网卡,又想有个万能脚本,ok,老游给你一个perl版本:

#!/usr/bin/perl

$count=0;

my $sec=60;

while(1){

my ($RX_bytes1,$TX_bytes1)=getnet();

sleep($sec);

my ($RX_bytes2,$TX_bytes2)=getnet();

my $net_RX_rate=int(($RX_bytes2-$RX_bytes1)/$sec);

my $net_TX_rate=int(($TX_bytes2-$TX_bytes1)/$sec);

my $net_rate=$net_RX_rate+$net_TX_rate;

my $localt=localtime();

$count++;

printlog("$countt$localtt$net_ratet$net_RX_ratet$net_TX_raten");

}

#-------------------------------------------------------------------------

sub getnet{

system("rm -fr net_use.tmp") if(-e "net_use.tmp");

system("cp /proc/net/dev net_use.tmp");

open(IN,"net_use.tmp")||die "$!";

my @array=;

close IN;

system("rm -fr net_use.tmp");

my ($RX_bytes,$TX_bytes)=(0,0);

foreach my $line(@array){

if($line=~/ethd+:(.+)/){

my $tmp=$1;

my @array2=split(/s+/,$tmp);

$RX_bytes+=$array2[0];

$TX_bytes+=$array2[8];

}

}

return ($RX_bytes,$TX_bytes);

}

#-------------------------------------------------------------------------

sub printlog{

my $str=shift;

open(LOGOUT,">>net_stat.log");

print LOGOUT "$str";

close LOGOUT;

}

ref: http://51runaway.blog.163.com/blog/static/240286882009102665632660/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值