zoukankan      html  css  js  c++  java
  • Linux工具之netstat

     
     
    1、简介
     
    Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。
     
     
    从整体上看,netstat的输出结果可以分为两个部分:
    一个是Active Internet connections,称为有源TCP连接,其中"Recv-Q"和"Send-Q"指%0A的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。
     
    另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。
     
    常用:[root@test ~]# netstat -tunlp  
     
     
    2、常见参数
     
    -a (all)显示所有选项,默认不显示LISTEN相关
    -t (tcp)仅显示tcp相关选项
    -u (udp)仅显示udp相关选项
    -n 拒绝显示别名,能显示数字的全部转化成数字。
    -l 仅列出有在 Listen (监听) 的服務状态
     
    -p 显示建立相关链接的程序名
    -r 显示路由信息,路由表
    -e 显示扩展信息,例如uid等
    -s 按各个协议进行统计
    -c 每隔一个固定时间,执行该netstat命令
     
    提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到
     
     
    3、实用命令实例
    1)列出所有端口 (包括监听和未监听的)
     
    列出所有端口 netstat -a
    # netstat -a | more
     Active Internet connections (servers and established)
     Proto Recv-Q Send-Q Local Address           Foreign Address         State
     tcp        0      0 localhost:30037         *:*                     LISTEN
     udp        0      0 *:bootpc                *:*
     
    Active UNIX domain sockets (servers and established)
     Proto RefCnt Flags       Type       State         I-Node   Path
     unix  2      [ ACC ]     STREAM     LISTENING     6135     /tmp/.X11-unix/X0
     unix  2      [ ACC ]     STREAM     LISTENING     5140     /var/run/acpid.socket
     
     
     
    列出所有 tcp 端口 netstat -at
    # netstat -at
     Active Internet connections (servers and established)
     Proto Recv-Q Send-Q Local Address           Foreign Address         State
     tcp        0      0 localhost:30037         *:*                     LISTEN
     tcp        0      0 localhost:ipp           *:*                     LISTEN
     tcp        0      0 *:smtp                  *:*                     LISTEN
     tcp6       0      0 localhost:ipp           [::]:*                  LISTEN
     
     
     
    列出所有 udp 端口 netstat -au
     
    # netstat -au
     Active Internet connections (servers and established)
     Proto Recv-Q Send-Q Local Address           Foreign Address         State
     udp        0      0 *:bootpc                *:*
     udp        0      0 *:49119                 *:*
     udp        0      0 *:mdns                  *:*
     
     
    2)列出所有处于监听状态的 Sockets
    只显示监听端口 netstat    -l
     
    [root@natasha ~]# netstat -l
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State     
    tcp        0          0            *:44993                      *:*                         LISTEN     
    tcp        0          0            *:sunrpc                      *:*                         LISTEN     
    tcp        0          0            *:ssh                           *:*                         LISTEN
     
     
     
    只列出所有监听 tcp 端口 netstat -lt
     
    只列出所有监听 udp 端口 netstat -lu
     
    只列出所有监听 UNIX 端口 netstat -lx
     
     
     
    3)显示每个协议的统计信息
     
    显示所有端口的统计信息 netstat -s
     
    [root@natasha ~]# netstat -s
    Ip:
        197 total packets received
        1 with invalid addresses
        0 forwarded
        0 incoming packets discarded
        188 incoming packets delivered
        157 requests sent out
    Icmp:
        23 ICMP messages received
        0 input ICMP message failed.
        ICMP input histogram:
            destination unreachable: 16
            echo requests: 4
            echo replies: 3
        23 ICMP messages sent
        0 ICMP messages failed
        ICMP output histogram:
            destination unreachable: 16
            echo request: 3
            echo replies: 4
    IcmpMsg:
            InType0: 3
            InType3: 16
            InType8: 4
            OutType0: 4
            OutType3: 16
            OutType8: 3
    Tcp:
        0 active connections openings
        1 passive connection openings
        0 failed connection attempts
        0 connection resets received
        1 connections established
        149 segments received
        118 segments send out
        0 segments retransmited
        0 bad segments received.
        0 resets sent
     
    。。。。。。。。
     
    显示 TCP 或 UDP 端口的统计信息 netstat -st 或 -su
    # netstat -st
    # netstat -su
     
     
    4)在 netstat 输出中显示 PID 和进程名称 netstat -p
     
    netstat -p 可以与其它开关一起使用,就可以添加 “PID/进程名称” 到 netstat 输出中,这样 debugging 的时候可以很方便的发现特定端口运行的程序。
    [root@natasha ~]# netstat -pt
    Active Internet connections (w/o servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
    tcp        0      0 192.168.180.119:ssh         192.168.180.1:4932          ESTABLISHED 3256/sshd
     
     
    5)持续输出 netstat 信息
    netstat 将每隔一秒输出网络信息。
    netstat  -c
    # netstat -c
     Active Internet connections (w/o servers)
     Proto Recv-Q Send-Q Local Address           Foreign Address         State
     tcp        0      0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED
     tcp        1      1 ramesh-laptop.loc:52564 101.11.169.230:www      CLOSING
     tcp        0      0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED
     tcp        1      1 ramesh-laptop.loc:42367 101.101.34.101:www      CLOSING
     ^C
     
     
     
    6)显示核心路由信息 netstat -r
    [root@natasha ~]# netstat -r
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    192.168.180.0   *               255.255.255.0   U         0 0          0 eth0
    link-local      *               255.255.0.0     U         0 0          0 eth0
    default         192.168.180.2   0.0.0.0         UG        0 0          0 eth0
     
     
    7)找出程序运行的端口
    # netstat -ap | grep ssh
     
    # netstat -an | grep ':80'
     
     
     
    8)显示网络接口列表
    # netstat -i
    Kernel Interface table
    Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
    eth0       1500 0         0      0      0 0             0      0      0      0 BMU
    eth2       1500 0     26196      0      0 0         26883      6      0      0 BMRU
    lo        16436 0         4      0      0 0             4      0      0      0 LRU
     
     
     
    9) IP和TCP分析
      查看连接某服务端口最多的的IP地址
     
    [root@natasha ~]# netstat -nat | grep "192.168.1.15:22" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20
    18 221.136.168.36
    3 154.74.45.242
    2 78.173.31.236
    2 62.183.207.98
    2 192.168.1.14
    2 182.48.111.215
    2 124.193.219.34
    2 119.145.41.2
    2 114.255.41.30
    1 75.102.11.99
     
     
    TCP各种状态列表
     
    [root@natasha ~]#netstat -nat |awk '{print $6}'
    established)
    Foreign
    LISTEN
    TIME_WAIT
    ESTABLISHED
    TIME_WAIT
    SYN_SENT
     
     
     
    先把状态全都取出来,然后使用uniq -c统计,之后再进行排序。
     
    [root@natasha ~]# netstat -nat |awk '{print $6}'|sort|uniq -c
    143 ESTABLISHED
    1 FIN_WAIT1
    1 Foreign
    1 LAST_ACK
    36 LISTEN
    6 SYN_SENT
    113 TIME_WAIT
    1 established)
     
     
    最后的命令如下:
     
    netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
     
     
    分析access.log获得访问前10位的ip地址
    awk '{print $1}' access.log |sort|uniq -c|sort -nr|head -10
  • 相关阅读:
    CSS流式布局 仿京东
    Flex 实现仿 携程网 手机端布局
    JS 实现 微信随机发红包思路
    记录gradle 使用笔记
    springBoot入门
    支付宝当面付扫码支付功能详解
    微信验证以及登录流程
    Nginx配置文件详细说明
    crontab定时任务打包备份文件并删除过期文件
    关于List比较好玩的操作
  • 原文地址:https://www.cnblogs.com/liuxing0007/p/11692264.html
Copyright © 2011-2022 走看看