zoukankan      html  css  js  c++  java
  • netstat命令, netstat指令在windows和linux有什么不同



    查看当前tcp监听端口
    [op@TIM html]$ netstat -nltp
    (Not all processes could be identified, non-owned process info
     will not be shown, you would have to be root to see it all.)
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
    tcp        0      0 127.0.0.1:8307              0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 127.0.0.1:5942              0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 0.0.0.0:445                 0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 0.0.0.0:50686               0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 0.0.0.0:902                 0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 0.0.0.0:139                 0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 0.0.0.0:6000                0.0.0.0:*                   LISTEN      -                   
    tcp        0      0 ::1:8307                    :::*                        LISTEN      -                   
    tcp        0      0 :::22                       :::*                        LISTEN      -                   
    tcp        0      0 ::1:631                     :::*                        LISTEN      -                   
    tcp        0      0 ::1:25                      :::*                        LISTEN      -                   
    tcp        0      0 :::33945                    :::*                        LISTEN      -                   
    tcp        0      0 :::8443                     :::*                        LISTEN      -                   
    tcp        0      0 :::443                      :::*                        LISTEN      -                   
    tcp        0      0 :::445                      :::*                        LISTEN      -                   
    tcp        0      0 :::139                      :::*                        LISTEN      -                   
    tcp        0      0 :::111                      :::*                        LISTEN      -                   
    tcp        0      0 :::80                       :::*                        LISTEN      -                   
    tcp        0      0 :::6000                     :::*                        LISTEN      -  


    查看当前tcp监听端口, 需要显示监听的程序名,当不清楚mysql的监听端口时比较好用
    [op@TIM html]$ netstat -tlp
    (Not all processes could be identified, non-owned process info
     will not be shown, you would have to be root to see it all.)
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
    tcp        0      0 localhost:8307              *:*                         LISTEN      -                   
    tcp        0      0 *:ftp                       *:*                         LISTEN      -                   
    tcp        0      0 localhost:5942              *:*                         LISTEN      -                   
    tcp        0      0 *:ssh                       *:*                         LISTEN      -                   
    tcp        0      0 localhost:ipp               *:*                         LISTEN      -                   
    tcp        0      0 localhost:smtp              *:*                         LISTEN      -                   
    tcp        0      0 *:https                     *:*                         LISTEN      -                   
    tcp        0      0 *:microsoft-ds              *:*                         LISTEN      -                   
    tcp        0      0 *:50686                     *:*                         LISTEN      -                   
    tcp        0      0 *:ideafarm-door             *:*                         LISTEN      -                   
    tcp        0      0 *:netbios-ssn               *:*                         LISTEN      -                   
    tcp        0      0 *:sunrpc                    *:*                         LISTEN      -                   
    tcp        0      0 *:x11                       *:*                         LISTEN      -                   
    tcp        0      0 localhost:8307              *:*                         LISTEN      -                   
    tcp        0      0 *:ssh                       *:*                         LISTEN      -                   
    tcp        0      0 localhost:ipp               *:*                         LISTEN      -                   
    tcp        0      0 localhost:smtp              *:*                         LISTEN      -                   
    tcp        0      0 *:33945                     *:*                         LISTEN      -                   
    tcp        0      0 *:pcsync-https              *:*                         LISTEN      -                   
    tcp        0      0 *:https                     *:*                         LISTEN      -                   
    tcp        0      0 *:microsoft-ds              *:*                         LISTEN      -                   
    tcp        0      0 *:netbios-ssn               *:*                         LISTEN      -                   
    tcp        0      0 *:sunrpc                    *:*                         LISTEN      -                   
    tcp        0      0 *:http                      *:*                         LISTEN      -                   
    tcp        0      0 *:x11                       *:*                         LISTEN      -    






    pidof mysqld


    看mysql的监听端口
    [op@TIM html]$ sudo netstat -nulp | grep 17734
    tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17734/mysqld  

    显示UDP端口号的使用情况
    (Not all processes could be identified, non-owned process info
     will not be shown, you would have to be root to see it all.)
    Active Internet connections (servers and established)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
    udp        0      0 *:bootpc                    *:*                                     -                   
    udp        0      0 *:bootpc                    *:*                                     -                   
    udp        0      0 *:37974                     *:*                                     -                   
    udp        0      0 localhost:1001              *:*                                     -                   
    udp        0      0 *:mdns                      *:*                                     -                   
    udp        0      0 *:kerberos-iv               *:*                                     -                   
    udp        0      0 *:sunrpc                    *:*                                     -                   
    udp        0      0 *:ipp                       *:*                                     -                   
    udp        0      0 *:servstat                  *:*                                     -                   
    udp        0      0 localhost:ntp               *:*                                     -                   
    udp        0      0 localhost:ntp               *:*                                     -                   
    udp        0      0 localhost:ntp               *:*                                     -                   
    udp        0      0 localhost:ntp               *:*                                     -                   
    udp        0      0 localhost:ntp               *:*                                     -                   
    udp        0      0 *:ntp                       *:*                                     -                   
    udp        0      0 localhost:netbios-ns        *:*                                     -                   
    udp        0      0 localhost:netbios-ns        *:*                                     -                   
    udp        0      0 localhost:netbios-ns        *:*                                     -                   
    udp        0      0 localhost:netbios-ns        *:*                                     -                   
    udp        0      0 localhost:netbios-ns        *:*                                     -                   
    udp        0      0 localhost:netbios-ns        *:*                                     -                   
    udp        0      0 localhost:netbios-ns        *:*                                     -                   
    udp        0      0 localhost:netbios-ns        *:*                                     -                   
    udp        0      0 *:netbios-ns                *:*                                     -                   
    udp        0      0 localhost:netbios-dgm       *:*                                     -                   
    udp        0      0 localhost:netbios-dgm       *:*                                     -                   
    udp        0      0 localhost:netbios-dgm       *:*                                     -                   
    udp        0      0 localhost:netbios-dgm       *:*                                     -                   
    udp        0      0 localhost:netbios-dgm       *:*                                     -                   
    udp        0      0 localhost:netbios-dgm       *:*                                     -                   
    udp        0      0 localhost:netbios-dgm       *:*                                     -                   
    udp        0      0 localhost:netbios-dgm       *:*                                     -                   
    udp        0      0 *:netbios-dgm               *:*                                     -                   
    udp        0      0 *:783                       *:*                                     -                   
    udp        0      0 *:45346                     *:*                                     -                   
    udp        0      0 *:sunrpc                    *:*                                     -                   
    udp        0      0 *:34032                     *:*                                     -                   
    udp        0      0 *:servstat                  *:*                                     -                   
    udp        0      0 fe80::250:56ff:fec0:1:ntp   *:*                                     -                   
    udp        0      0 fe80::250:56ff:fec0:8:ntp   *:*                                     -                   
    udp        0      0 fe80::d253:49ff:fe81:e26:ntp *:*                                     -                   
    udp        0      0 fe80::6202:92ff:fe62:302:ntp *:*                                     -                   
    udp        0      0 localhost:ntp               *:*                                     -                   
    udp        0      0 *:ntp                       *:*                                     -    


    显示网卡列表
    # netstat -i


    显示网络统计信息
    # netstat -s



    显示监听的套接口
    # netstat -l

    列出所有 tcp 端口
    命令:netstat -at

    统计机器中网络连接各个状态个数
    命令:netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
    [op@TIM html]$ netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
    TIME_WAIT 3
    CLOSE_WAIT 2
    ESTABLISHED 13
    LISTEN 26


    把状态全都取出来后使用uniq -c统计后再进行排序
    命令:netstat -nat |awk '{print $6}'|sort|uniq -c
    [op@TIM html]$ netstat -a | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
    TIME_WAIT 3
    CLOSE_WAIT 2
    ESTABLISHED 13
    LISTEN 26
    [op@TIM html]$ netstat -nat |awk '{print $6}'|sort|uniq -c
          2 CLOSE_WAIT
          1 established)
         14 ESTABLISHED
          1 Foreign
         26 LISTEN
          1 TIME_WAIT



    查看连接某服务端口最多的的IP地址
    命令:netstat -nat | grep "192.168.120.20:16067" |awk '{print $5}'|awk -F: '{print $4}'|sort|uniq -c|sort -nr|head -20


    实例16:找出程序运行的端口
    命令:netstat -ap | grep ssh

    在 netstat 输出中显示 PID 和进程名称
    命令:netstat -pt


    找出运行在指定端口的进程
    命令:netstat -anpt | grep ':16064'



















        查看所有tcp监听端口:netstat -ntlp
        查看所有udp监听端口:netstat -nulp
        查看所有tcp/udp监听端口:netstat -nutlp
        查看所有已经建立的连接:netstat -antp
        查看连接某服务端口最多的的IP地址:netstat -nat | grep ':80' | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | head -20
        网络连接数目:netstat -an | grep -E “^(tcp)” | cut -c 68- | sort | uniq -c | sort -n
        按状态查看连接连接数量:netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}'或netstat -nat|awk '{print $6}'|sort|uniq -c|sort -rn
        按IP查看连接数量:netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n或netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ips[ip], ip | "sort -k1 -nr"}'
        查看80端口的连接,并排序:netstat -an -t | grep ':80' | grep ESTABLISHED | awk '{printf "%s %s ",$5,$6}' | sort
        查看当前的memcache连接:netstat -n | grep :11211
        查看占用端口8080的进程:netstat -tnlp | grep 8080或lsof -i:8080
        查看所有php进程:netstat -anp | grep php | grep ^tcp
        查看所有nginx进程:netstat -anp | grep nginx | grep ^tcp
        查看80端口连接数最多的20个IP:netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
        查找较多time_wait连接:netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
        找查较多的SYN连接:netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more
        输出正在占用UDP和TCP端口的所有程序连带计时器信息:netstat -putona:
        查看指定IP的连接:netstat -nat | grep "192.168.1.222":



    netstat –s
    本选项能够按照各个协议分别显示其统计数据。如果我们的应用程序(如Web浏览器)运行速度比较慢,或者不能显示Web页之类的数据,那么我们就可以用本选项来查看一下所显示的信息。我们需要仔细查看统计数据的各行,找到出错的关键字,进而确定问题所在。

    ·netstat –e
    本选项用于显示关于以太网的统计数据。它列出的项目包括传送的数据报的总字节数、错误数、删除数、数据报的数量和广播的数量。这些统计数据既有发送的数据报数量,也有接收的数据报数量。这个选项可以用来统计一些基本的网络流量)。

    ·netstat –r
    本选项可以显示关于路由表的信息,类似于后面所讲使用route print命令时看到的 信息。除了显示有效路由外,还显示当前有效的连接。

    ·netstat –a
    本选项显示一个所有的有效连接信息列表,包括已建立的连接(ESTABLISHED),也包括监听连接请(LISTENING)的那些连接。

    ·netstat –n
    显示所有已建立的有效连接。









    其实我常用的是 netstat -tnl | grep 443  (查看443端口是否被占用),如果有当前是root用户,我喜欢用netstat -pnl | grep 443 (还可显示出占用本机443端口的进程PID)。
    netstat
    功能说明:显示网络状态。
    语  法:netstat [-acCeFghilMnNoprstuvVwx] [-A<网络类型>][--ip]
    补充说明:利用netstat指令可让你得知整个Linux系统的网络情况。
    参  数:
      -a或--all   显示所有连线中的Socket。
      -A<网络类型>或--<网络类型>   列出该网络类型连线中的相关地址。
      -c或--continuous   持续列出网络状态。
      -C 或--cache   显示路由器配置的快取信息。
      -e或--extend   显示网络其他相关信息。
      -F或 --fib   显示FIB。
      -g或--groups   显示多重广播功能群组组员名单。
      -h或--help   在线帮助。
      -i 或--interfaces   显示网络界面信息表单。
      -l或--listening   显示监控中的服务器的Socket。
      -M 或--masquerade   显示伪装的网络连线。
      -n或--numeric   直接使用IP地址,而不通过域名服务器。
      -N 或--netlink或--symbolic   显示网络硬件外围设备的符号连接名称。
      -o或--timers   显示计时器。
      -p 或--programs   显示正在使用Socket的程序识别码和程序名称。
      -r或--route   显示 Routing Table。
      -s或--statistice   显示网络工作信息统计表。
      -t或--tcp   显示TCP 传输协议的连线状况。
      -u或--udp   显示UDP传输协议的连线状况。
      -v或--verbose   显示指令执行过程。
      -V 或--version   显示版本信息。
      -w或--raw   显示RAW传输协议的连线状况。
      -x或--unix   此参数的效果和指定"-A unix"参数相同。
      --ip或--inet   此参数的效果和指定"-A inet"参数相同。

    2.网络连接状态详解
    共有12中可能的状态,前面11种是按照TCP连接建立的三次握手和TCP连接断开的四次挥手过程来描述的。
    1)、LISTEN:首先服务端需要打开一个socket进行监 听,状态为LISTEN./* The socket is listening for incoming connections. 侦听来自远方TCP端口的连接请求 */

    2)、SYN_SENT:客户端通过应用程序调用connect进行active open.于是客户端tcp发送一个SYN以请求建立一个连接.之后状态置为SYN_SENT./*The socket is actively attempting to establish a connection. 在发送连接请求后等待匹配的连接请求 */

    3)、SYN_RECV:服务端应发出ACK确认客户端的 SYN,同时自己向客户端发送一个SYN. 之后状态置为SYN_RECV/* A connection request has been received from the network. 在收到和发送一个连接请求后等待对连接请求的确认 */

    4)、ESTABLISHED: 代表一个打开的连接,双方可以进行或已经在数据交互了。/* The socket has an established connection. 代表一个打开的连接,数据可以传送给用户 */

    5)、FIN_WAIT1:主动关闭(active close)端应用程序调用close,于是其TCP发出FIN请求主动关闭连接,之后进入FIN_WAIT1状态./* The socket is closed, and the connection is shutting down. 等待远程TCP的连接中断请求,或先前的连接中断请求的确认 */

    6)、CLOSE_WAIT:被动关闭(passive close)端TCP接到FIN后,就发出ACK以回应FIN请求(它的接收也作为文件结束符传递给上层应用程序),并进入CLOSE_WAIT./* The remote end has shut down, waiting for the socket to close. 等待从本地用户发来的连接中断请求 */

    7)、FIN_WAIT2:主动关闭端接到ACK后,就进入了 FIN-WAIT-2 ./* Connection is closed, and the socket is waiting for a shutdown from the remote end. 从远程TCP等待连接中断请求 */

    8)、LAST_ACK:被动关闭端一段时间后,接收到文件结束 符的应用程序将调用CLOSE关闭连接。这导致它的TCP也发送一个 FIN,等待对方的ACK.就进入了LAST-ACK ./* The remote end has shut down, and the socket is closed. Waiting for acknowledgement. 等待原来发向远程TCP的连接中断请求的确认 */

    9)、TIME_WAIT:在主动关闭端接收到FIN后,TCP 就发送ACK包,并进入TIME-WAIT状态。/* The socket is waiting after close to handle packets still in the network.等待足够的时间以确保远程TCP接收到连接中断请求的确认 */

    10)、CLOSING: 比较少见./* Both sockets are shut down but we still don’t have all our data sent. 等待远程TCP对连接中断的确认 */

    11)、CLOSED: 被动关闭端在接受到ACK包后,就进入了closed的状态。连接结束./* The socket is not being used. 没有任何连接状态 */

    12)、UNKNOWN: 未知的Socket状态。/* The state of the socket is unknown. */
    SYN: (同步序列编号,Synchronize Sequence Numbers)该标志仅在三次握手建立TCP连接时有效。表示一个新的TCP连接请求。
    ACK: (确认编号,Acknowledgement Number)是对TCP请求的确认标志,同时提示对端系统已经成功接收所有数据。
    FIN: (结束标志,FINish)用来结束一个TCP回话.但对应端口仍处于开放状态,准备接收后续数据。

    PS:在windows下有个小工具挺好的,TCPView is a Windows program that will show you detailed listings of all TCP and UDP endpoints on your system, including the local and remote addresses and state of TCP connections.见 http://technet.microsoft.com/en-us/sysinternals/bb897437 ; 当然如果要详细分析数据包,可选用sniffer、Wireshark等更强大的工具。

    参考资料:
    http://linux.sheup.com/linux/4/31225.html
    http://hi.baidu.com/mqbest_come_on/blog/item/18526dcef73d791a00e928e5.html
    http://www.daxigua.com/archives/1355

    netstat指令在windows和linux有什么不同

    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
    个人认为win下的netstat 命令实现是linux的子集,两者都已经支持ipv6,但linux下的功能更丰富。
    [root@server ~]# netstat -?
    usage: netstat [-veenNcCF] [<Af>] -r         netstat {-V|--version|-h|--help}
           netstat [-vnNcaeol] [<Socket> ...]
           netstat { [-veenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s } [delay]
     
            -r, --route                display routing table
            -I, --interfaces=<Iface>   display interface table for <Iface>
            -i, --interfaces           display interface table
            -g, --groups               display multicast group memberships
            -s, --statistics           display networking statistics (like SNMP)
            -M, --masquerade           display masqueraded connections
     
            -v, --verbose              be verbose
            -n, --numeric              don't resolve names
            --numeric-hosts            don't resolve host names
            --numeric-ports            don't resolve port names
            --numeric-users            don't resolve user names
            -N, --symbolic             resolve hardware names
            -e, --extend               display other/more information
            -p, --programs             display PID/Program name for sockets
            -c, --continuous           continuous listing
     
            -l, --listening            display listening server sockets
            -a, --all, --listening     display all sockets (default: connected)
            -o, --timers               display timers
            -F, --fib                  display Forwarding Information Base (default)
            -C, --cache                display routing cache instead of FIB
            -T, --notrim               stop trimming long addresses
            -Z, --context              display SELinux security context for sockets
     
      <Iface>: Name of interface to monitor/list.
      <Socket>={-t|--tcp} {-u|--udp} {-S|--sctp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom
      <AF>=Use '-A <af>' or '--<af>'; default: inet
      List of possible address families (which support routing):
        inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) 
        netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) 
        x25 (CCITT X.25) 
    [root@server ~]#

    win下面是这样:

    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
    C:Userswood>netstat /?
     
    显示协议统计和当前 TCP/IP 网络连接。
     
    NETSTAT [-a] [-b] [-e] [-f] [-n] [-o] [-p proto] [-r] [-s] [-x] [-t] [interval]
     
      -a            显示所有连接和侦听端口。
      -b            显示在创建每个连接或侦听端口时涉及的
                    可执行程序。在某些情况下,已知可执行程序承载
                    多个独立的组件,这些情况下,
                    显示创建连接或侦听端口时
                    涉及的组件序列。在此情况下,可执行程序的
                    名称位于底部 [] 中,它调用的组件位于顶部,
                    直至达到 TCP/IP。注意,此选项
                    可能很耗时,并且在你没有足够
                    权限时可能失败。
      -e            显示以太网统计。此选项可以与 -s 选项
                    结合使用。
      -f            显示外部地址的完全限定
                    域名(FQDN)。
      -n            以数字形式显示地址和端口号。
      -o            显示拥有的与每个连接关联的进程 ID。
      -p proto      显示 proto 指定的协议的连接;proto
                    可以是下列任何一个: TCP、UDP、TCPv6 或 UDPv6。如果与 -s
                    选项一起用来显示每个协议的统计,proto 可以是下列任何一个:
                    IP、IPv6、ICMP、ICMPv6、TCP、TCPv6、UDP 或 UDPv6。
      -r            显示路由表。
      -s            显示每个协议的统计。默认情况下,
                    显示 IP、IPv6、ICMP、ICMPv6、TCP、TCPv6、UDP 和 UDPv6 的统计;
                    -p 选项可用于指定默认的子网。
      -t            显示当前连接卸载状态。
      -x            显示 NetworkDirect 连接、侦听器和共享
                    端点。
      -y            显示所有连接的 TCP 连接模板。
                    无法与其他选项结合使用。
      interval      重新显示选定的统计,各个显示间暂停的
                    间隔秒数。按 CTRL+C 停止重新显示
                    统计。如果省略,则 netstat 将打印当前的
                    配置信息一次。

    windows下使用: Netstat –ano|findstr “<端口号>

  • 相关阅读:
    实验四 决策树算法及应用
    实验三 朴素贝叶斯算法及应用
    实验二 K-近邻算法及应用
    实验一 感知器及其应用
    实验3: 面向对象分析与设计
    实验2:结构化分析与设计
    实验1:软件开发文档与工具的安装与使用
    软件工程第三次作业——ATM管理系统
    软件工程第二次作业——举例分析流程图与活动图的区别与联系
    软件工程第一次作业——小学四则运算题目生成程序
  • 原文地址:https://www.cnblogs.com/timssd/p/4559187.html
Copyright © 2011-2022 走看看