grep 文本過濾器
sed 流編輯器
awk 報告生成器
格式化以后顯示
awk [option] 'PATTERN {action}' file1 file2
awk -F"|" 'BEGIN{OFS=":"} {print $1,$2,$3}' test.txt
#文本字符串用雙引號
awk -F"|" 'BEGIN{OFS=":"} {print $1,"jksong",$2,$3}' test.txt
#BEGIN 后面不用跟文件名
awk 'BEGIN {print "one\ntwo\nthree"}'
#輸出空白行
awk 'BEGIN {print}'
awk 'BEGIN {print ""}'
awk 變量
1、內置變量之記錄變量
FS: file separator 讀取的文本的分隔符
RS: record separator 換行符
OFS: output file separator
ORS:output record separator
2、內置變量之數據變量
NR the number of input records 處理的多個文件的總共的行數
FNR 當前文件的第多少行
NF 字段數
3、自定義變量
awk -v"name=jksong" 'BEGIN {print name}'
awk 'BEGIN {name="jksong"; print name}'
awk 'BEGIN {name="jksong"; printf "%10s \n",name}'
所有的運算都支持
PATTERN
1、正則
awk -F":" '/^r/ {print $1}' /etc/passwd
2、表達式
$1~/xx/($1 匹配)
$1!~/xx/($1 不匹配)
$1 == "xx"
awk -F":" '$1~/^r/{print $0}' /etc/passwd
awk -F":" '$1=="song"{print $0}' /etc/passwd
awk -F: 'NR>=1 && NR<=2 {print $1,$3,$NF}' /etc/passwd
3、range patt1, patt2 從1開始匹配到2結束匹配
awk -F: '/^bin/,/^adm/{print $1,$3,$NF}' /etc/passwd
4、BEGIN END 僅在awk命令開始前和開始后運行一次
awk -F: 'BEGIN {print "user name sheell "} NR>=1 && NR<=2 {print $1,$3,$NF} END {print "end of report"}' /etc/passwd
5、空模式
對文件的每一行都要處理
#if
awk -F":" '{name = $1=="root" ? "admin" : "user"; print name }' /etc/passwd
awk -F":" '{if($1=="root") print $0}' /etc/passwd
awk -F":" '{if($3>500) {sum+=$3}}END {print sum}' /etc/passwd
#while
awk -F: '{i=1;while(i<=NF){if(length($i)>5) {print $i} i++}}'
df -hP|awk 'NR>1{ gsub("%", "", $5); if($5>2) {print $0} }'
#for
awk 'BEGIN {for(i=0;i<10;i++){print i}}'
#提前結束對本行的處理,進入下一行
next
awk -F: '{if($3%2 == 0) next; print $3," ", $0}' /etc/passwd
#數組
下標從1開始
下標為任意字符串
不存在下標會自動初始化,判斷下標是否存在 index in array
#統計每個shell的使用量
awk -F: '{shell[$NF]++} END {for(a in shell ){print a, " ", shell[a]}}' /etc/passwd
#統計每種狀態的量
netstat -tan|awk 'NR>1 {shell[$NF]++} END {for(a in shell ){print a, " ", shell[a]}}'
?
一些常用工具命令
?
Linux Web服務器網站故障分析常用的命令
系統連接狀態篇:
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篇)按域統計流量
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]}}'
數據庫篇
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
?
?