且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

linux 查看网卡流量的方法

更新时间:2022-06-16 02:53:24


watch命令的使用

   watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行。在Linux下,watch是周期性的执行下个程序,并全屏显示执行结果。

        默认watch会以2s的间隔重复运行命令,你也可以用 -n 参数指定时间间隔,还有一个实用的参数是-d,这样watch会帮你高亮显示变化的区域,这样更加一目了然了,Ctrl+c 可以退出

你可以拿他来监测你想要的一切命令的结果变化,比如:ls 监测某个文件的大小变化,看你的想象力了!


1.命令格式:

watch [参数] [命令]


2.命令功能:

  可以将命令的输出结果输出到标准输出设备,多用于周期性执行命令/定时执行命令


3.命令参数:

-n或--interval  watch缺省每2秒运行一下程序可以用-n或-interval来指定间隔的时间

-d或--differences  用-d或--differences 选项watch 会高亮显示变化的区域 而-d=cumulative选项会把变动过的地方(不管最近的那次有没有变动)都高亮显示出来

-t 或-no-title  会关闭watch命令在顶部的时间间隔,命令当前时间的输出

         -h, --help 查看帮助文档


4.使用实例:

实例1:

命令:每隔一秒高亮显示网络链接数的变化情况

watch -n 1 -d netstat -ant
说明:

其它操作:
切换终端: Ctrl+x
退出watch:Ctrl+g


实例2:每隔一秒高亮显示http链接数的变化情况

命令:

watch -n 1 -d 'pstree|grep http'

说明:

每隔一秒高亮显示http链接数的变化情况。 后面接的命令若带有管道符,需要加''将命令区域归整。


实例3:实时查看模拟攻击客户机建立起来的连接数

命令:

watch 'netstat -an | grep:21 | \ grep<模拟攻击客户机的IP>| wc -l' 

说明:


实例4:监测当前目录中 scf' 的文件的变化

命令:

watch -d 'ls -l|grep scf' 


实例5:10秒一次输出系统的平均负载

命令:

     watch -n 10 'cat /proc/loadavg'




一、使用wtach + 命令 查看网卡流量

watch ifconfig

watch cat /proc/net/dev

       能看查看各网卡的接收和发送的的数据包/字节情况的统计流


watch -n 1 "/sbin/ifconfig eth0 | grep bytes"



二、nload工具

用途:

          用来即时监看网路状态

安装:

          yum install nload    # epel源


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[xmxiuman@esf ~]$ nload -h
 
nload version 0.7.2
Copyright (C) 2001 - 2008 by Roland Riegel <feedback@roland-riegel.de>
nload comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
welcome to redistribute it under certain conditions. For more details see the
GNU General Public License Version 2 (http://www.gnu.org/copyleft/gpl.html).
 
Command line syntax:
nload [options] [devices]
nload --help|-h
 
Options:
-a period       Sets the length in seconds of the time window for average
                calculation.
                Default is 300.
-i max_scaling  Specifies the 100% mark in kBit/s of the graph indicating the
                incoming bandwidth usage. Ignored if max_scaling is 0 or the
                switch -m is given.
                Default is 10240.
-m              Show multiple devices at a time; no traffic graphs.
-o max_scaling  Same as -i but for the graph indicating the outgoing bandwidth
                usage.
                Default is 10240.
-t interval     Determines the refresh interval of the display in milliseconds.
                Default is 500.
-u h|b|k|m|g    Sets the type of unit used for the display of traffic numbers.
   H|B|K|M|G    h: auto, b: Bit/s, k: kBit/s, m: MBit/s etc.
                H: auto, B: Byte/s, K: kByte/s, M: MByte/s etc.
                Default is h.
-U h|b|k|m|g    Same as -u, but for a total amount of data (without "/s").
   H|B|K|M|G    Default is H.
devices         Network devices to use.
                Default is to use all auto-detected devices.
--help
-h              Print this help.
 
example: nload -t 200 -i 1024 -o 128 -U M
 
The options above can also be changed at run time by pressing the 'F2' key.

-a:这个好像是全部数据的刷新时间周期,单位是秒,默认是300. 
-i:进入网卡的流量图的显示比例最大值设置,默认10240 kBit/s. 
-m:显示所有网卡的统计数据,不显示流量图。 
-o:出去网卡的流量图的显示比例最大值设置,默认10240 kBit/s. 
-t:显示数据的刷新时间间隔,单位是毫秒,默认500。 
-u:设置右边Curr、Avg、Min、Max的数据单位,默认是自动变的.注意大小写单位不同! 
h|b|k|m|g    h: auto, b: Bit/s, k: kBit/s, m: MBit/s etc. 
H|B|K|M|G    H: auto, B: Byte/s, K: kByte/s, M: MByte/s etc. 
-U:设置右边Ttl的数据单位,默认是自动变的.注意大小写单位不同(与-u相同)! 
Devices:自定义监控的网卡,默认eth0,使用左右键切换。 



nload默认查看的是eth0网卡,可以使用左右方向键选择检测的网卡

也可以这样监测eth1网卡的流量:
nload eth1


nload 默认分为上下两块:

     上半部分是:Incoming也就是进入网卡的流量,

     下半部分是:Outgoing,也就是从这块网卡出去的流量

       每部分都有当前流量(Curr),平均流量(Avg),最小流量(Min),最大流量(Max),总和流量(Ttl)这几个部分,看起来还是蛮直观的。

另外,你也可以自己定义流量数值显示的单位


q或crtl + c 退出



三、ifstat

介绍

      ifstat工具是个网络接口监测工具,能比较简单看实时网络流量

安装:

1
2
3
4
wget 
 
./configure
make && make install

    

实例

默认使用

[root@Node6 ~]# ifstat
       eth0                eth1       
 KB/s in  KB/s out   KB/s in  KB/s out
    0.06      0.18      0.00      0.00
    0.06      0.13      0.00      0.00
    0.06      0.13      0.00      0.00
    0.06      0.13      0.00      0.00
    0.06      0.13      0.00      0.00
    0.06      0.13      0.00      0.00

默认ifstat不监控回环接口,显示的流量单位是KB。

 

监控所有网络接口

# ifstat -a
        lo                 eth0                eth1       
 KB/s in  KB/s out   KB/s in  KB/s out   KB/s in  KB/s out    
    0.00      0.00      0.28      0.58      0.06      0.06
    0.00      0.00      1.41      1.13      0.00      0.00
    0.61      0.61      0.26      0.23      0.00      0.00

 ifstat 能比较简单看网络流量概况。


参数: 

-a   监测能检测到的所有网络接口的状态信息 
-z   隐藏流量是无的接口,例如那些接口虽然启动了但是未用的 
-i    指定要监测的接口,后面跟网络接口名 

时间: 

-t    显示时间

1
2
3
4
5
[root@Node6 ~]# ifstat -t 
  Time           eth0                eth1       
HH:MM:SS   KB/s in  KB/s out   KB/s in  KB/s out
20:22:39      0.15      0.25      0.06      0.00
20:22:40      0.06      0.15      0.06      0.00

-T    报告所有监测接口的全部带宽 

1
2
3
4
5
6
7
8
9
10
[root@Node6 ~]# ifstat -T
       eth0                eth1               Total       
 KB/s in  KB/s out   KB/s in  KB/s out   KB/s in  KB/s out
    0.06      0.21      0.00      0.00      0.06      0.21
    0.06      0.15      0.06      0.00      0.12      0.15
    0.06      0.15      0.18      0.00      0.23      0.15
    0.06      0.15      0.18      0.00      0.23      0.15
    0.06      0.15      0.18      0.00      0.23      0.15
    0.06      0.15      0.12      0.00      0.18      0.15
    0.06      0.15      0.06      0.00      0.12      0.15

-w    用指定的列宽,而不是为了适应接口名称的长度而去自动放大列宽 
-W   如果内容比终端窗口的宽度还要宽就自动换行 
-b    用kbits/s显示带宽而不是kbytes/s



四、iftop

介绍

      iftop是一款实时流量监控工具,监控TCP/IP连接等,缺点就是无报表功能。必须以root身份才能运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[xmxiuman@esf ~]$ iftop -h
iftop: display bandwidth usage on an interface by host
 
Synopsis: iftop -h | [-npbBP] [-i interface] [-f filter code] [-N net/mask]
 
   -h                  display this message
   -n                  don't do hostname lookups
   -N                  don't convert port numbers to services
   -p                  run in promiscuous mode (show traffic between other
                       hosts on the same network segment)
   -b                  don't display a bar graph of traffic
   -B                  Display bandwidth in bytes
   -i interface        listen on named interface
   -f filter code      use filter code to select packets to count
                       (default: none, but only IP packets are counted)
   -F net/mask         show traffic flows in/out of network
   -P                  show ports as well as hosts
   -m limit            sets the upper limit for the bandwidth scale
   -c config file      specifies an alternative configuration file
 
iftop, version 0.17
copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors

-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

监控eth1

iftop -i eth1

直接显示IP, 不进行DNS反解析

iftop -

直接显示连接端口号, 不显示服务名称:

iftop -N

显示某个网段进出封包流量

iftop -F 192.168.1.0/24 or 192.168.1.0/255.255.255.0

 

基于实例讲解输出含义

执行iftop -N -n -i eth1后界面为

1
2
3
                 19.1Mb            38.1Mb              57.2Mb               76.3Mb             95.4Mb+-----------------+-----------------+--------------------+--------------------+---------------------192.168.1.11                                  => 192.168.1.66                  5.3Mb  3.22Mb  3.20Mb                                              <=                               219kb  45.7kb  49.3kb192.168.1.11                                   => 192.168.1.29                 144kb  30.8kb  29.6kb                                              <=                               11.3Mb  2.38Mb  2.74Mb192.168.1.11                                  => 12.2.11.71                    0b   6.40kb  6.66kb                                              <=                               0b      0b      0b192.168.1.11                                  => 192.168.1.8                   2.63kb  1.43kb   932b                                              <=                               1.31kb  1.05kb   893b192.168.1.11                                   => 192.168.2.78                 2.53kb  1.54kb  2.15kb                                               <=                              160b    160b    187b192.168.1.11                                   => 111.126.195.69               0b    166b     69b                                               <=                              0b      0b      0b------------------------------------------------------------------------------------------------------TX:             cum:   9.70MB   peak:   15.6Mb                          rates:   15.4Mb  3.26Mb  3.23Mb
RX:                    8.38MB           14.9Mb                                   11.5Mb  2.42Mb  2.79Mb
TOTAL:                 18.1MB           30.5Mb                                   27.0Mb  5.69Mb  6.03Mb


iftop界面含义如下

第一行:

中间部分:
,即记录了哪些ip正在和本机的网络连接

中间部分右边:
       
        =>代表发送数据,<= 代表接收数据

底部三行:
     

底部三行第二列:cumm (累计)
      

底部三行第三列:

底部三行第四列:


       通过iftop的界面很容易找到哪个ip在霸占网络流量,这个是ifstat做不到的。不过iftop的流量显示单位是Mb,这个b是bit,是位,不是字节,而ifstat的KB,这个B就是字节了,byte是bit的8倍。初学者容易被误导。

 

进入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退出监控。


五、iptraf、dstat

      dstat详见:http://xiexiaojun.blog.51cto.com/2305291/1883447



 yum install iptraf 


对监控网络来说,这个更适合也更强大,但在总的流量显示上,没nload直观和方便


使用
直接运行 iptraf
后有一个如下的菜单提示,然后进入相关的选项查看

 IP流量监视(IP traffic monitor)
网络接口的一般信息统计(General Interface Statistics)
网络接口的细节信息统计(Detailed Interface Statistics)
统计分析(Statistical Breakdowns)
局域网工作站统计(LAN Station Statistics)
过滤器(Filters...)
配置(Configure...)
退出(Exit)

也可以直接加参数或选项直接进入
可以查看还有哪些参数和选项












本文转自xiexiaojun51CTO博客,原文链接:http://blog.51cto.com/xiexiaojun/1919776 ,如需转载请自行联系原作者