实时查看linux网卡流量脚本

admin2012-06-15linux132

安装过程: iftop 软件列表http://pkgs.repoforge.org/iftop/ 

[root@centos wdlinux]# cat /etc/issue

CentOS release 5.6 (Final) Kernel \r on an \m 

查看版本号 

[root@centos wdlinux]# uname -a

Linux centos 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 EST 2011 x86_64 x86_64 x86_64 GNU/Linux 

查看系统版本

yum install flex byacc libpcap ncurses ncurses-develyum install libpcap libpcap-devel -y 

安装依赖软件包 

 地址:http://rpmfind.net/linux/rpm2html/search.php?query=iftop

系统为centos5版本为64位

wget ftp://rpmfind.net/linux/dag/redhat/el5/en/x86_64/dag/RPMS/iftop-1.0-0.pre3.el5.rf.x86_64.rpm


下载对应系统的对应软件包

rpm -ivh iftop-1.0-0.pre3.el5.rf.x86_64.rpm


安装软件

系统为centos6 版本为64位

wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/iftop-1.0-0.pre3.el6.rf.x86_64.rpm

rpm -ivh iftop-1.0-0.pre3.el6.rf.x86_64.rpm

系统为centos7版本为64位

wget ftp://rpmfind.net/linux/dag/redhat/el7/en/x86_64/dag/RPMS/iftop-1.0-0.pre3.el7.rf.x86_64.rpm

rpm -ivh iftop-1.0-0.pre3.el7.rf.x86_64.rpm

1、本脚本可自定义欲查看接口,精确到小数,并可根据流量大小灵活显示单位。

2、此脚本的采集间隔为1秒。 

3、此脚本不需要额外再安装软件,可在急用情况下应付一下,比如临时想看一下是否有流量通过,大概为多少等。 

4、一些流量查看软件由于计算的精确度不同,所以与此脚本显示的数值不可能一致,此脚本的显示结果与du meter对比过,相差很小。


还有就是传输工具本身显示的传输速度并不准确。 用法为: 

1、chmod +x ./traff.sh 将文件改成可执行脚本。 

2、./traff.sh eth0即可开始监看接口eth0流量,按ctrl+c退出。

#!/bin/bash while [ "1" ] do eth=$1 RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}') TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}') sleep 1 RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}') TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}') clear echo -e "\t RX `date +%k:%M:%S` TX" RX=$((${RXnext}-${RXpre})) TX=$((${TXnext}-${TXpre})) if [[ $RX -lt 1024 ]];then RX="${RX}B/s" elif [[ $RX -gt 1048576 ]];then RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}') else RX=$(echo $RX | awk '{print $1/1024 "KB/s"}') fi if [[ $TX -lt 1024 ]];then TX="${TX}B/s" elif [[ $TX -gt 1048576 ]];then TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}') else TX=$(echo $TX | awk '{print $1/1024 "KB/s"}') fi echo -e "$eth \t $RX $TX " done #################################################################################

 查看网络平均流量 下面的脚本可以很好的监控你的网络的平均流量,你可以提定时间,首先制定法 #!/bin/bash echo -n "which nic?" read eth echo "the nic is "$eth echo -n "how much seconds:" read sec echo "duration is "$sec" seconds, wait please..." 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" seconds total :"$sum"bytes" aver=$(($sum/$sec)) echo "avrage :"$aver"bytes/sec" 

 ########################################################################## 

 Linux流量监控的iftop工具 iftop是什么? iftop是类似于top的实时流量监控工具。 官方网站:http://www.ex-parrot.com/~pdw/iftop/ 安装iftop 安装方法1、编译安装 如果采用编译安装可以到iftop官网下载最新的源码包。 安装前需要已经安装好基本的编译所需的环境,比如make、gcc、autoconf等。安装iftop还需要安装libpcap和libcurses。 CentOS上安装所需依赖包: yum install flex byacc libpcap ncurses ncurses-devel libpcap-devel Debian上安装所需依赖包: apt-get install flex byacc libpcap0.8 libncurses5 下载iftop wget http://www.ex-parrot.com/pdw/iftop/download/iftop-0.17.tar.gz tar zxvf iftop-0.17.tar.gz cd iftop-0.17 ./configure make && make install 安装方法2:(懒人办法,最简单) 直接省略上面的步骤 CentOS系统: yum install flex byacc libpcap ncurses ncurses-devel wget ftp://fr2.rpmfind.net/linux/dag/redhat/el5/en/i386/dag/RPMS/iftop-0.17-1.el5.rf.i386.rpm rpm -ivh iftop-0.17-1.el5.rf.i386.rpm Debian系统 运行:apt-get install iftop 运行iftop 直接运行: iftop 效果如下图: 相关参数及说明 1、iftop界面相关说明 界面上面显示的是类似刻度尺的刻度范围,为显示流量图形的长条作标尺用的。 中间的<= =>这两个左右箭头,表示的是流量的方向。 TX:发送流量 RX:接收流量 TOTAL:总流量 Cumm:运行iftop到目前时间的总流量 peak:流量峰值 rates:分别表示过去 2s 10s 40s 的平均流量 2、iftop相关参数 常用的参数 -i设定监测的网卡,如:# iftop -i eth1 -B 以bytes为单位显示流量(默认是bits),如:# iftop -B -n使host信息默认直接都显示IP,如:# iftop -n -N使端口信息默认直接都显示端口号,如: # iftop -N -F显示特定网段的进出流量,如# iftop -F 10.10.1.0/24或# iftop -F 10.10.1.0/255.255.255.0 -h(display this message),帮助,显示参数信息 -p使用这个参数后,中间的列表显示的本地主机信息,出现了本机以外的IP信息; -b使流量图形条默认就显示; -f这个暂时还不太会用,过滤计算包用的; -P使host信息及端口信息默认就都显示; -m设置界面最上边的刻度的最大值,刻度分五个大段显示,例:# iftop -m 100M 进入iftop画面后的一些操作命令(注意大小写) 按h切换是否显示帮助; 按n切换显示本机的IP或主机名; 按s切换是否显示本机的host信息; 按d切换是否显示远端目标主机的host信息; 按t切换显示格式为2行/1行/只显示发送流量/只显示接收流量; 按N切换显示端口号或端口服务名称; 按S切换是否显示本机的端口信息; 按D切换是否显示远端目标主机的端口信息; 按p切换是否显示端口信息; 按P切换暂停/继续显示; 按b切换是否显示平均流量图形条; 按B切换计算2秒或10秒或40秒内的平均流量; 按T切换是否显示每个连接的总流量; 按l打开屏幕过滤功能,输入要过滤的字符,比如ip,按回车后,屏幕就只显示这个IP相关的流量信息; 按L切换显示画面上边的刻度;刻度不同,流量图形条会有变化; 按j或按k可以向上或向下滚动屏幕显示的连接记录; 按1或2或3可以根据右侧显示的三列流量数据进行排序; 按<根据左边的本机名或IP排序; 按>根据远端目标主机的主机名或IP排序; 按o切换是否固定只显示当前的连接; 按f可以编辑过滤代码,这是翻译过来的说法,我还没用过这个! 按!可以使用shell命令,这个没用过!没搞明白啥命令在这好用呢! 按q退出监控。 常见问题 1、make: yacc: Command not found make: *** [grammar.c] Error 127 解决方法:apt-get install byacc / yum install byacc 2、configure: error: Curses! Foiled again! (Can't find a curses library supporting mvchgat.) Consider installing ncurses. 解决方法:apt-get install libncurses5-dev / yum install ncurses-devel ####################################################################################### 

 Linux流量监控的nload工具 官网及下载 http://www.roland-riegel.de/nload/index.html http://www.roland-riegel.de/nload/nload-0.7.2.tar.gz http://dl.wdlinux.cn:5180/soft/nload-0.7.2.tar.gz 安装 wget http://www.roland-riegel.de/nload/nload-0.7.2.tar.gz tar zxvf nload-0.7.2.tar.gz cd nload-0.7.2 ./configure;make;make install 使用 直接用nload回车即可,也可以指定网卡,如nload eth1 还可以指定是以K或M来显示流量,如nload -u M显示的流量是以MB为单位的 看看还有哪些参数或选项 [root@wdlinux ~]# nload -h nload 默认分为上下两块:上半部分是:Incoming也就是进入网卡的流量,下半部分是:Outgoing,也就是从这块网卡出去的流量,每 部分都有当前流量(Curr),平均流量(Avg),最小流量(Min),最大流量(Max),总和流量(Ttl)这几个部分,看起来还是蛮直观的。 另外,你也可以自己定义流量数值显示的单位 #nload --help 就可以看到具体的相关参数了。