Linux netstat詳解

Dus發表於2015-07-24

做計算機管理員,我們都必要了解一下netstat這個命令,它是一個檢視網路連線狀態的工具,在windows下也預設有這個工具。
Netstat命令詳解 netstat命令怎樣使用 如何關閉TIME_WAIT連線 如何統計web伺服器的訪問量 如果檢視nginx的訪問流量?

下面我們來看看它主要的用法和詳解!
(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,”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 "%st%dn",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

更多的請參考:

http://bbs.linuxtone.org/forum-14-1.html

相關文章