netstat命令1

1.查看哪些IP连接本机

[pso@U10-9 ~]$ netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:2222                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:51790               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0     52 10.143.32.19:2222           10.108.22.231:53541         ESTABLISHED 
tcp        0      0 10.143.32.19:41471          10.143.33.20:62015          ESTABLISHED 
udp        0      0 0.0.0.0:817                 0.0.0.0:*                               
udp        0      0 127.0.0.1:842               0.0.0.0:*                               
udp        0      0 0.0.0.0:43744               0.0.0.0:*                               
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               
udp        0      0 0.0.0.0:631                 0.0.0.0:*                               
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     24303554 /var/titanagent/log.socket
unix  2      [ ACC ]     STREAM     LISTENING     10806  /var/run/cups/cups.sock
unix  2      [ ACC ]     STREAM     LISTENING     11367  /var/run/abrt/abrt.socket
unix  2      [ ACC ]     STREAM     LISTENING     10019  /var/run/vmware/guestServicePipe
unix  2      [ ACC ]     STREAM     LISTENING     7205   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     10911  @/var/run/hald/dbus-SSBfsWglBr
unix  2      [ ACC ]     STREAM     LISTENING     10740  /var/run/dbus/system_bus_socket
unix  2      [ ]         DGRAM                    10936  @/org/freedesktop/hal/udev_event
unix  2      [ ACC ]     STREAM     LISTENING     10873  /var/run/acpid.socket
unix  2      [ ]         DGRAM                    7688   @/org/kernel/udev/udevd
unix  2      [ ACC ]     STREAM     LISTENING     10906  @/var/run/hald/dbus-ifDqi9LYaC
unix  4      [ ]         DGRAM                    8266467 /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     10552  /var/run/rpcbind.sock
unix  2      [ ]         DGRAM                    28589229 
unix  3      [ ]         STREAM     CONNECTED     28589167 
unix  3      [ ]         STREAM     CONNECTED     28589166 
unix  2      [ ]         DGRAM                    28589163 
unix  2      [ ]         DGRAM                    4564508 
unix  2      [ ]         DGRAM                    643193 
unix  2      [ ]         DGRAM                    11369  
unix  3      [ ]         STREAM     CONNECTED     11179  /var/run/acpid.socket
unix  3      [ ]         STREAM     CONNECTED     11178  
unix  3      [ ]         STREAM     CONNECTED     11169  @/var/run/hald/dbus-ifDqi9LYaC
unix  3      [ ]         STREAM     CONNECTED     11168  
unix  3      [ ]         STREAM     CONNECTED     11067  @/var/run/hald/dbus-ifDqi9LYaC
unix  3      [ ]         STREAM     CONNECTED     11052  
unix  3      [ ]         STREAM     CONNECTED     10931  @/var/run/hald/dbus-SSBfsWglBr
unix  3      [ ]         STREAM     CONNECTED     10930  
unix  3      [ ]         STREAM     CONNECTED     10908  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     10907  
unix  2      [ ]         DGRAM                    10875  
unix  3      [ ]         STREAM     CONNECTED     10752  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     10751  
unix  3      [ ]         STREAM     CONNECTED     10745  
unix  3      [ ]         STREAM     CONNECTED     10744  
unix  2      [ ]         DGRAM                    10631  
unix  3      [ ]         DGRAM                    7706   
unix  3      [ ]         DGRAM                    7705   

2.查看TCP连接数

2.1 统计9081端口连接数
[call@moban ~]$ netstat -nat|grep -i "9081"|wc -l
4

2.2 统计httpd协议连接数
[call@moban ~]$ ps -ef|grep httpd|wc -l
1

3.3 统计已连接上的,状态为"established"
[call@moban ~]$ netstat -na|grep ESTABLISHED|wc -l
48

3.4查出哪个IP地址连接最多,将其封了.
netstat -na|grep ESTABLISHED|awk {print $5}|awk -F: {print $1}|sort|uniq -c|sort -r +0n
netstat -na|grep SYN|awk {print $5}|awk -F: {print $1}|sort|uniq -c|sort -r +0n

--------------------------------------------------------------------------------------------------------
1.查看apache当前并发访问数
[call@moban ~]$ netstat -an|grep ESTABLISHED|wc -l
47

2、查看有多少个进程数
[call@moban ~]$ ps aux|grep httpd|wc -l
1

netstat -an会打印系统当前网络链接状态,而grep -i "9081"是用来提取与80端口有关的连接的,wc -l进行连接数统计。
最终返回的数字就是当前所有9081端口的请求总数。
[call@moban ~]$ netstat -nat|grep -i "9081"|wc -l
1

查看Apache的并发请求数及其TCP连接状态:
返回结果示例:
  LAST_ACK 5
  SYN_RECV 30
  ESTABLISHED 1597
  FIN_WAIT1 51
  FIN_WAIT2 504
  TIME_WAIT 1057
  其中的SYN_RECV表示正在等待处理的请求数;ESTABLISHED表示正常数据传输状态;TIME_WAIT表示处理完毕,等待超时结束的请求数。
  
状态:描述
  CLOSED:无连接是活动 的或正在进行
  LISTEN:服务器在等待进入呼叫
  SYN_RECV:一个连接请求已经到达,等待确认
  SYN_SENT:应用已经开始,打开一个连接
  ESTABLISHED:正常数据传输状态
  FIN_WAIT1:应用说它已经完成
  FIN_WAIT2:另一边已同意释放
  ITMED_WAIT:等待所有分组死掉
  CLOSING:两边同时尝试关闭
  TIME_WAIT:另一边已初始化一个释放
  LAST_ACK:等待所有分组死掉

你可能感兴趣的:(Linux)