个人原创地址:http://www.phpthinking.com/archives/406
简介
Netstat命令用于显示各种网络相关信息,如网络连接,路由表,接口状态(InterfaceStatistics),masquerade连接,多播成员(MulticastMemberships)等等。
输出信息含义
执行netstat后,其输出结果为
01
ActiveInternetconnections(w/oservers)
02
03
ProtoRecv-QSend-QLocalAddressForeignAddressState
04
05
tcp02210.34.6.89:telnet210.34.6.96:2873ESTABLISHED
06
07
tcp2960210.34.6.89:1165210.34.6.84:netbios-ssnESTABLISHED
08
09
tcp00localhost.localdom:9001localhost.localdom:1162ESTABLISHED
10
11
tcp00localhost.localdom:1162localhost.localdom:9001ESTABLISHED
12
13
tcp080210.34.6.89:1161210.34.6.10:netbios-ssnCLOSE
14
15
16
17
ActiveUNIXdomainsockets(w/oservers)
18
19
ProtoRefCntFlagsTypeStateI-NodePath
20
21
unix1[]STREAMCONNECTED16178@000000dd
22
23
unix1[]STREAMCONNECTED16176@000000dc
24
25
unix9[]DGRAM5292/dev/log
26
27
unix1[]STREAMCONNECTED16182@000000df
从整体上看,netstat的输出结果可以分为两个部分:
一个是ActiveInternetconnections,称为有源TCP连接,其中”Recv-Q”和”Send-Q”指%0A的是接收队列和发送队列。这些数字一般都应该是0。如果不是则表示软件包正在队列中堆积。这种情况只能在非常少的情况见到。
另一个是ActiveUNIXdomainsockets,称为有源Unix域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。
Proto显示连接使用的协议,RefCnt表示连接到本套接口上的进程号,Types显示套接口的类型,State显示套接口当前的状态,Path表示连接到套接口的其它进程使用的路径名。
常见参数
-a(all)显示所有选项,默认不显示LISTEN相关
-t(tcp)仅显示tcp相关选项
-u(udp)仅显示udp相关选项
-n拒绝显示别名,能显示数字的全部转化成数字。
-l仅列出有在Listen(监听)的服務状态
-p显示建立相关链接的程序名
-r显示路由信息,路由表
-e显示扩展信息,例如uid等
-s按各个协议进行统计
-c每隔一个固定时间,执行该netstat命令。
提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到
实用命令实例
1.列出所有端口(包括监听和未监听的)
列出所有端口netstat-a
01
#netstat-a|more
02
03
ActiveInternetconnections(servers
and
established)
04
05
ProtoRecv-QSend-QLocalAddressForeignAddressState
06
07
tcp00localhost:30037*:*LISTEN
08
09
udp00*:bootpc*:*
10
11
12
13
ActiveUNIXdomainsockets(servers
and
established)
14
15
ProtoRefCntFlagsTypeStateI-NodePath
16
17
unix2[ACC]STREAMLISTENING6135/tmp/.X11-unix/X0
18
19
unix2[ACC]STREAMLISTENING5140/
var
/run/acpid.socket
列出所有tcp端口netstat-at
01
#netstat-at
02
03
ActiveInternetconnections(servers
and
established)
04
05
ProtoRecv-QSend-QLocalAddressForeignAddressState
06
07
tcp00localhost:30037*:*LISTEN
08
09
tcp00localhost:ipp*:*LISTEN
10
11
tcp00*:smtp*:*LISTEN
12
13
tcp600localhost:ipp[::]:*LISTEN
列出所有udp端口netstat-au
01
#netstat-au
02
03
ActiveInternetconnections(servers
and
established)
04
05
ProtoRecv-QSend-QLocalAddressForeignAddressState
06
07
udp00*:bootpc*:*
08
09
udp00*:49119*:*
10
11
udp00*:mdns*:*
2.列出所有处于监听状态的Sockets
只显示监听端口netstat-l
01
#netstat-l
02
03
ActiveInternetconnections(onlyservers)
04
05
ProtoRecv-QSend-QLocalAddressForeignAddressState
06
07
tcp00localhost:ipp*:*LISTEN
08
09
tcp600localhost:ipp[::]:*LISTEN
10
11
udp00*:49119*:*
只列出所有监听tcp端口netstat-lt
[/php]#netstat-lt
ActiveInternetconnections(onlyservers)
ProtoRecv-QSend-QLocalAddressForeignAddressState
tcp00localhost:30037*:*LISTEN
tcp00*:smtp*:*LISTEN
tcp600localhost:ipp[::]:*LISTEN[/php]
只列出所有监听udp端口netstat-lu
1
#netstat-lu
2
3
ActiveInternetconnections(onlyservers)
4
5
ProtoRecv-QSend-QLocalAddressForeignAddressState
6
7
udp00*:49119*:*
8
9
udp00*:mdns*:*
只列出所有监听UNIX端口netstat-lx
01
#netstat-lx
02
03
ActiveUNIXdomainsockets(onlyservers)
04
05
ProtoRefCntFlagsTypeStateI-NodePath
06
07
unix2[ACC]STREAMLISTENING6294
private
/maildrop
08
09
unix2[ACC]STREAMLISTENING6203
public
/cleanup
10
11
unix2[ACC]STREAMLISTENING6302
private
/ifmail
12
13
unix2[ACC]STREAMLISTENING6306
private
/bsmtp
3.显示每个协议的统计信息
显示所有端口的统计信息netstat-s
01
#netstat-s
02
03
Ip:
04
05
11150totalpacketsreceived
06
07
1withinvalidaddresses
08
09
0forwarded
10
11
0incomingpacketsdiscarded
12
13
11149incomingpacketsdelivered
14
15
11635requestssentout
16
17
Icmp:
18
19
0ICMPmessagesreceived
20
21
0inputICMPmessagefailed.
22
23
Tcp:
24
25
582activeconnectionsopenings
26
27
2failedconnectionattempts
28
29
25connectionresetsreceived
30
31
Udp:
32
33
1183packetsreceived
34
35
4packetstounknownportreceived.
36
37
.....
显示TCP或UDP端口的统计信息netstat-st或-su
1
#netstat-st
2
3
#netstat-su
4.在netstat输出中显示PID和进程名称netstat-p
netstat-p可以与其它开关一起使用,就可以添加“PID/进程名称”到netstat输出中,这样debugging的时候可以很方便的发现特定端口运行的程序。
01
#netstat-pt
02
03
ActiveInternetconnections(w/oservers)
04
05
ProtoRecv-QSend-QLocalAddressForeignAddressStatePID/Programname
06
07
tcp10ramesh-laptop.loc:47212192.168.185.75:wwwCLOSE_WAIT2109/firefox
08
09
tcp00ramesh-laptop.loc:52750lax:wwwESTABLISHED2109/firefox
10
11
5.在netstat输出中不显示主机,端口和用户名(host,port
or
user)
当你不想让主机,端口和用户名显示,使用netstat-n。将会使用数字代替那些名称。
同样可以加速输出,因为不用进行比对查询。
1
#netstat-an
如果只是不想让这三个名称中的一个被显示,使用以下命令
1
#netsat-a--numeric-ports
2
3
#netsat-a--numeric-hosts
4
5
#netsat-a--numeric-users
6.持续输出netstat信息
netstat将每隔一秒输出网络信息。
01
#netstat-c
02
03
ActiveInternetconnections(w/oservers)
04
05
ProtoRecv-QSend-QLocalAddressForeignAddressState
06
07
tcp00ramesh-laptop.loc:36130101-101-181-225.ama:wwwESTABLISHED
08
09
tcp11ramesh-laptop.loc:52564101.11.169.230:wwwCLOSING
10
11
tcp00ramesh-laptop.loc:43758server-101-101-43-2:wwwESTABLISHED
12
13
tcp11ramesh-laptop.loc:42367101.101.34.101:wwwCLOSING
14
15
^C
7.显示系统不支持的地址族(AddressFamilies)
netstat–verbose
在输出的末尾,会有如下的信息
1
netstat:nosupport
for
`AFIPX'onthissystem.
2
3
netstat:nosupport
for
`AFAX25'onthissystem.
4
5
netstat:nosupport
for
`AFX25'onthissystem.
6
7
netstat:nosupport
for
`AFNETROM'onthissystem.
8.显示核心路由信息netstat-r
01
#netstat-r
02
03
KernelIProutingtable
04
05
DestinationGatewayGenmaskFlagsMSSWindowirttIface
06
07
192.168.1.0*255.255.255.0U000eth2
08
09
link-local*255.255.0.0U000eth2
10
11
default
192.168.1.10.0.0.0UG000eth2
注意:使用netstat-rn显示数字格式,不查询主机名称。
9.找出程序运行的端口
并不是所有的进程都能找到,没有权限的会不显示,使用root权限查看所有的信息。
01
#netstat-ap|grepssh
02
03
tcp10dev-db:ssh101.174.100.22:39213CLOSE_WAIT-
04
05
tcp10dev-db:ssh101.174.100.22:57643CLOSE_WAIT-
06
07
找出运行在指定端口的进程
08
09
10
11
#netstat-an|grep
':80'
10.显示网络接口列表
01
#netstat-i
02
03
KernelInterfacetable
04
05
IfaceMTUMetRX-OKRX-ERRRX-DRPRX-OVRTX-OKTX-ERRTX-DRPTX-OVRFlg
06
07
eth01500000000000BMU
08
09
eth2150002619600026883600BMRU
10
11
lo16436040004000LRU
显示详细信息,像是ifconfig使用netstat-ie:
01
#netstat-ie
02
03
KernelInterfacetable
04
05
eth0Linkencap:EthernetHWaddr00:10:40:11:11:11
06
07
UPBROADCASTMULTICASTMTU:1500Metric:1
08
09
RXpackets:0errors:0dropped:0overruns:0frame:0
10
11
TXpackets:0errors:0dropped:0overruns:0carrier:0
12
13
collisions:0txqueuelen:1000
14
15
RXbytes:0(0.0B)TXbytes:0(0.0B)
16
17
Memory:f6ae0000-f6b00000
11.IP和TCP分析
查看连接某服务端口最多的的IP地址
01
netstat-nat|grep
"192.168.1.15:22"
|awk
'{print$5}'
|awk-F:
'{print$1}'
|sort|uniq-c|sort-nr|head-20
02
03
18221.136.168.36
04
05
3154.74.45.242
06
07
278.173.31.236
08
09
262.183.207.98
10
11
2192.168.1.14
12
13
2182.48.111.215
14
15
2124.193.219.34
16
17
2119.145.41.2
18
19
2114.255.41.30
20
21
175.102.11.99
TCP各种状态列表
01
netstat-nat|awk
'{print$6}'
02
03
established)
04
05
Foreign
06
07
LISTEN
08
09
TIME_WAIT
10
11
ESTABLISHED
12
13
TIME_WAIT
14
15
SYN_SENT
1
viewplaincopyprint?
2
3
<spanstyle=
"line-height:1.5;font-size:14px"
>先把状态全都取出来,然后使用uniq-c统计,之后再进行排序。</span>
01
netstat-nat|awk
'{print$6}'
|sort|uniq-c
02
03
143ESTABLISHED
04
05
1FIN_WAIT1
06
07
1Foreign
08
09
1LAST_ACK
10
11
36LISTEN
12
13
6SYN_SENT
14
15
113TIME_WAIT
16
17
1established)
1
viewplaincopyprint?
2
3
<spanstyle=
"line-height:1.5;font-size:14px"
>
最后的命令如下:</span>
1
netstat-nat|awk
'{print$6}'
|sort|uniq-c|sort-rn
1
viewplaincopyprint?
2
3
<spanstyle=
"line-height:1.5;font-size:14px"
><strong>分析access.log获得访问前10位的ip地址</strong></span>
4
5
awk
'{print$1}'
access.log|sort|uniq-c|sort-nr|head-10
.
青春气贯长虹,勇敢盖过怯懦,进取压倒苟安。