linux-網路監控命令-netstat進階

來源:互聯網
上載者:User

標籤:sni   監控命令   squid   ace   agent   dump   ++   uniq   open   

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,”\t”,state[key]}’
netstat -n | awk ‘/^tcp/ {++arr[$NF]};END {for(k in arr) print k,”\t”,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\t%d\n",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

linux-網路監控命令-netstat進階

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.