netstat 使用详解

来源:互联网 发布:php源码修改工具 编辑:程序博客网 时间:2024/06/16 02:26
1. netstat 简介

netstat 是一个监控TCP/IP网络的非常有用的工具,用于显示各种网络相关信息,如网络连接,接口状态,路由表, (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。Netstat用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。


2. netstat 参数说明

 netstat -husage: 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                      >>指定-r或–route,显示路由表        -I, --interfaces=<Iface>   display interface table for <Iface>        >>不知道该参数同-i参数有什么区别        -i, --interfaces           display interface table                    >>指定-i或者-interface,显示网络接口信息        -g, --groups               display multicast group memberships        >>指定-g或者-groups,显示多重广播功能群组组员名单        -s, --statistics           display networking statistics (like SNMP)  >>指定-s或者-statistics,显示网络工作信息统计        -M, --masquerade           display masqueraded connections            >>指定-M或者-masquerade,显示伪装的网络连线        -v, --verbose              be verbose                                 >>指定-v或者-verbose,显示详细信息        -n, --numeric              don't resolve names                        >>指定-n或者-numeric,显示使用数字,不解析成相应的名字        --numeric-hosts            don't resolve host names                   >>不解析主机名(用ip显示)        --numeric-ports            don't resolve port names                   >>不解析端口名        --numeric-users            don't resolve user names                   >>不解析用户名        -N, --symbolic             resolve hardware names                     >>指定-N或者-symbolic,显示网络硬件外围设备的符号连接名称        -e, --extend               display other/more information             >>指定-e或者-extend,显示更多的信息        -p, --programs             display PID/Program name for sockets       >>指定-p或者-programs,显示正在使用该Socket的进程id和进程名         -c, --continuous           continuous listing                         >>指定-c或者-continuous,持续显示        -l, --listening            display listening server sockets           >>指定-l或者-listening,显示处于监控状态的服务器的Socket        -a, --all, --listening     display all sockets (default: connected)   >>指定-a或者-all,显示所有socket(默认只显示connected状态的)        -o, --timers               display timers                             >>指定-o或者-times,显示计时器        -F, --fib                  display Forwarding Information Base (default)  >>指定-F或者-fib,显示FIB        -C, --cache                display routing cache instead of FIB       >>指定-C或者-cache,显示路由的cache信息而不是FIB        -T, --notrim               stop trimming long addresses               >>指定-T或者-notrim,显示时不会截断长地址        -Z, --context              display SELinux security context for sockets >>指定-Z或者-context,显示sockets的SELinux security内容  <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) 


3. netstat 输出

从整体上看,netstat的输出结果可以分为两个部分:


一个是Active Internet connections,称为有源TCP连接,Proto显示socket使用的协议(tcp,udp,raw),"Recv-Q"和"Send-Q"指的是接收队列和发送队列(这些数字一般都应该是0,如果不是则表示软件包正在队列中堆积,这种情况是非常少见的),Local Address显示在本地哪个地址和端口上监听,Foreign Address显示接收外部哪些地址哪个端口的请求,State显示socket的状态(通常只有tcp有状态信息),PID/Program name显示socket进程id和进程名


另一个是Active UNIX domain sockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。


##关于Active Internet connections 部分state列解释如下,state列可能12个值如下(通常只有tcp不要state信息):
state列共有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等更强大的工具。
------------------------------------------------------------------------------------------------------------------------------------------


4. netstat使用示例

1)列出所有端口 
netstat -anp


2)列出所有 tcp 端口 
netstat -antp


3)列出所有 udp 端口 
netstat -anup


4)只显示所有监听端口 
netstat -lnp


5)只列出所有监听 tcp 端口 
netstat -ltnp


6)只列出所有监听 udp 端口 
netstat -lunp


7)只列出所有监听 UNIX 端口 
netstat -lxnp


8)找出程序运行的端口
netstat -anp | grep ssh


NOTE:并不是所有的进程都能找到,没有权限的会不显示,使用 root 权限查看所有的信息。


10)找出运行在指定端口的进程
netstat -anp | grep ':3306'


11)持续输出 netstat 信息(每隔一秒输出网络信息)
netstat -cnp


12)显示所有端口的统计信息 
netstat -s

0 0
原创粉丝点击