標籤:style blog http color strong 檔案 資料 for
即時查看正在執行的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‘
查看http串連
netstat -n | awk ‘/^tcp/ {++state[$NF]} END {for(key in state) print key,"t",state[key]}‘
再來看看awk:
/^tcp/
濾出tcp開頭的記錄,屏蔽udp, socket等無關記錄。
state[]
相當於定義了一個名叫state的數組
NF
表示記錄的欄位數,如上所示的記錄,NF等於6
$NF
表示某個欄位的值,如上所示的記錄,$NF也就是$6,表示第6個欄位的值,也就是TIME_WAIT
state[$NF]
表示數組元素的值,如上所示的記錄,就是state[TIME_WAIT]狀態的串連數
++state[$NF]
表示把某個數加一,如上所示的記錄,就是把state[TIME_WAIT]狀態的串連數加一
END
表示在最後階段要執行的命令
for(key in state)
遍曆數組
print key,"\t",state[key]
列印數組的鍵和值,中間用\t定位字元分割,美化一下。
查看SYN狀態的http串連
netstat -n | awk ‘/^tcp/ {++state[$NF]} END {for(key in state) print key,"t",state[key]}‘
查看TIME_WAIT狀態的http串連
netstat -tna | cut -b 49- |grep TIME_WAIT | sort |more
netstat -an | grep TIME_WAIT | awk ‘{print $5}‘ | awk -F: ‘{print $1}‘ | sort | uniq -c | sort -nr | more
查看ESTABLISHED狀態的http串連
netstat -an | grep ESTABLISHED | awk ‘{print $5}‘ | awk -F: ‘{print $1}‘ | sort | uniq -c | sort -nr | morenetstat -an | grep ":80" | grep ESTABLISHED | sort | more
批量kill進程
ps -efww|grep sqlr-listener|grep -v grep|cut -c 9-15|xargs kill -9
查看活動的php-cgi串連數
ps -efww|grep sqlr-listener|grep -v grep|cut -c 9-15|xargs kill -9
判斷CC攻擊 netstat命令詳解,快速找出有問題的ip
網站排障分析常用的命令】
系統串連狀態篇:
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
7.計算httpd佔用記憶體的平均數
ps aux|grep -v grep|awk ‘/httpd/{sum+=$6;n++};END{print sum/n}’
網站日誌分析篇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