netstat 命令

Print network connections, routing tables, interface statistics,
masquerade connections, and multicast memberships

netstat用来显示网络相关的信息,如网络连接,路由表,接口统计,伪装链接和广播成员

常用的几个选项

–numeric , -n Show numerical addresses instead of trying to determine
symbolic host,port or user names.

不解析名称,显示数字就行

-p, –program Show the PID and name of the program to which each socket belongs.

显示端口(?)所属的程序的pid和名字

-l, –listening Show only listening sockets. (These are omitted by default.)

只显示正在监听的端口

{-t|–tcp} {-u|–udp}

分别表示显示TCP/UDP传输协议的连接

一些在我看来很高级的用法:

查看连接某服务端口最多的的IP地址:

netstat -ntu | grep :80 | awk ‘{print $5}’ | cut -d: -f1 | awk
‘{++ip[$1]} END {for(i in ip) print ip[i],”\t”,i}’ | sort -nr

TCP各种状态列表:

netstat -nt | grep -e 127.0.0.1 -e 0.0.0.0 -e ::: -v | awk ‘/^tcp/
{++state[$NF]} END {for(i in state) print i,”\t”,state[i]}’

查看phpcgi进程数,如果接近预设值,说明不够用,需要增加:

netstat -anpo | grep “php-cgi” | wc -l