zoukankan      html  css  js  c++  java
  • netstat命令详解

    它主要的用法和详解!
    (netstat -na 命令),本文主要是说Linux下的netstat工具,然后详细说明一下各种网络连接状态。
    netstat -nat |awk ‘{print $6}’|sort|uniq -c|sort -nr
    1.netstat命令详解
    其实我常用的是 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
    
    
    系统连接状态篇:
    
    1.查看TCP连接状态
    netstat -nat |awk ‘{print $6}’|sort|uniq -c|sort -rn
    
    netstat -n | awk ‘/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}’ 或
    netstat -n | awk ‘/^tcp/ {++state[$NF]}; END {for(key in state) print key,”	”,state[key]}’
    netstat -n | awk ‘/^tcp/ {++arr[$NF]};END {for(k in arr) print k,”	”,arr[k]}’
    
    netstat -n |awk ‘/^tcp/ {print $NF}’|sort|uniq -c|sort -rn
    
    netstat -ant | awk ‘{print $NF}’ | grep -v ‘[a-z]‘ | sort | uniq -c
    
    2.查找请求数请20个IP(常用于查找攻来源):
    netstat -anlp|grep 80|grep tcp|awk ‘{print $5}’|awk -F: ‘{print $1}’|sort|uniq -c|sort -nr|head -n20
    
    netstat -ant |awk ‘/:80/{split($5,ip,”:”);++A[ip[1]]}END{for(i in A) print A[i],i}’ |sort -rn|head -n20
    
    3.用tcpdump嗅探80端口的访问看看谁最高
    tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F”.” ‘{print $1″.”$2″.”$3″.”$4}’ | sort | uniq -c | sort -nr |head -20
    
    4.查找较多time_wait连接
    netstat -n|grep TIME_WAIT|awk ‘{print $5}’|sort|uniq -c|sort -rn|head -n20
    
    5.找查较多的SYN连接
    netstat -an | grep SYN | awk ‘{print $5}’ | awk -F: ‘{print $1}’ | sort | uniq -c | sort -nr | more
    
    6.根据端口列进程
    netstat -ntlp | grep 80 | awk ‘{print $7}’ | cut -d/ -f1
    
    网站日志分析篇1(Apache):
    
    1.获得访问前10位的ip地址
    cat access.log|awk ‘{print $1}’|sort|uniq -c|sort -nr|head -10
    cat access.log|awk ‘{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}’
    
    2.访问次数最多的文件或页面,取前20
    cat access.log|awk ‘{print $11}’|sort|uniq -c|sort -nr|head -20
    
    3.列出传输最大的几个exe文件(分析下载站的时候常用)
    cat access.log |awk ‘($7~/.exe/){print $10 ” ” $1 ” ” $4 ” ” $7}’|sort -nr|head -20
    
    4.列出输出大于200000byte(约200kb)的exe文件以及对应文件发生次数
    cat access.log |awk ‘($10 > 200000 && $7~/.exe/){print $7}’|sort -n|uniq -c|sort -nr|head -100
    
    5.如果日志最后一列记录的是页面文件传输时间,则有列出到客户端最耗时的页面
    cat access.log |awk ‘($7~/.php/){print $NF ” ” $1 ” ” $4 ” ” $7}’|sort -nr|head -100
    
    6.列出最最耗时的页面(超过60秒的)的以及对应页面发生次数
    cat access.log |awk ‘($NF > 60 && $7~/.php/){print $7}’|sort -n|uniq -c|sort -nr|head -100
    
    7.列出传输时间超过 30 秒的文件
    cat access.log |awk ‘($NF > 30){print $7}’|sort -n|uniq -c|sort -nr|head -20
    
    8.统计网站流量(G)
    cat access.log |awk ‘{sum+=$10} END {print sum/1024/1024/1024}’
    
    9.统计404的连接
    awk ‘($9 ~/404/)’ access.log | awk ‘{print $9,$7}’ | sort
    
    10. 统计http status.
    cat access.log |awk ‘{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}'
    cat access.log |awk '{print $9}'|sort|uniq -c|sort -rn
    
    10.蜘蛛分析
    查看是哪些蜘蛛在抓取内容。
    /usr/sbin/tcpdump -i eth0 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'bot|crawler|slurp|spider'
    
    网站日分析2(Squid篇)
    
    2.按域统计流量
    zcat squid_access.log.tar.gz| awk '{print $10,$7}' |awk 'BEGIN{FS="[ /]"}{trfc[$4]+=$1}END{for(domain in trfc){printf "%s	%d
    ",domain,trfc[domain]}}'
    
    效率更高的perl版本请到此下载:http://docs.linuxtone.org/soft/tools/tr.pl
    
    数据库篇
    1.查看数据库执行的sql
    /usr/sbin/tcpdump -i eth0 -s 0 -l -w - dst port 3306 | strings | egrep -i 'SELECT|UPDATE|DELETE|INSERT|SET|COMMIT|ROLLBACK|CREATE|DROP|ALTER|CALL'
    
    系统Debug分析篇
    
    1.调试命令
    strace -p pid
    
    2.跟踪指定进程的PID
    gdb -p pid
    
    详细访问linuxtone.org
    
    
    
    source url:http://linuxsjun.blog.163.com/blog/static/35771271201141710521644/
  • 相关阅读:
    <数据结构基础学习>(四)链表 Part 2
    <Android基础> (四) Fragment Part 2
    swagger忽略方法里参数的方法
    ActiveMq
    Tomcat日志归档
    java代理的实现
    包装类型和基础类型是如何比较的
    hashmap
    可达性分析中,可以作为GcRoots的对象
    使用Navicat 连接oracle出现 “ORA-03135: Connection Lost Contact”
  • 原文地址:https://www.cnblogs.com/77qt/p/6144126.html
Copyright © 2011-2022 走看看