Linux服務器管理Shell經典命令

轉載:http://86er.sinaapp.com/?p=154

1.站點根目錄下查找是否被放置webshell***根據語句判斷是不是PHP***腳本
# find ./ -name “*.php” |xargs egrep “phpspy|c99sh|milw0rm|eval\(gunerpress|eval\(base64_decode|spider_bc”>/tmp/php.txt
# grep -r –include=*.php  ‘[^a-z]eval($_POST’  > /tmp/eval.txt
#grep -r –include=*.php  ‘file_put_contents(.*$_POST\[.*\]);’ > tmp/file_put_contents.txt
 
2.統計服務器訪問日誌中所有不同ip出現的次數
# cat access.log |awk ‘{print $4}’| sort | uniq -c |sort -rn
或者
#cat access.log  | awk ‘{print $4}’ | sort -n | awk ‘{S[$NF]++} END {for(a in S) {print a”\t” S[a]}}’ | sort +1 -2nr
awk ‘{print $4}’ :  通過管道將第四個字段也就是ip地址篩選出來。
sort -n :將ip地址進行排序
awk ‘{S[$NF]++} END{for(a in S) {print a”\t” S[a]}}:
$NF是awk裏的一個變量,代表最後一個字段的內容,由於這晨只有一個字段, 即:IP地址,所以$NF代表IP地址。
S[$NF]++裏的S代表一個數組,然後統計IP地址出現的次數. 後面是一個for in 循環語句,將這個數組裏的值和鍵打印出來。
sort +1 -2nr:以第二個字段,也就是每個IP的訪問次數進行排序
 
3.分析出現次數最多的ip對網站的具體數據訪問情況
# grep -e IP access.log > filename
# cat filename |awk ‘{print $8}’|sort|uniq -c|sort -rn
 
4.訪問次數最多的文件或頁面,取前20
# cat access.log|awk ‘{print $11}’|sort|uniq -c|sort -nr|head -20
 
5.列出傳輸最大的幾個exe文件(分析下載站的時候常用)
# cat access.log |awk ‘($7~/\.exe/){print $10 ” ” $1 ” ” $4 ” ” $7}’|sort -nr|head -20
 
6.列出輸出大於200000byte(約200kb)的exe文件以及對應文件發生次數
# cat access.log |awk ‘($10 > 200000 && $7~/\.exe/){print $7}’|sort -n|uniq -c|sort -nr|head -100
 
7.如果日誌最後一列記錄的是頁面文件傳輸時間,則有列出到客戶端最耗時的頁面
# cat access.log |awk ‘($7~/\.php/){print $NF ” ” $1 ” ” $4 ” ” $7}’|sort -nr|head -100
 
8.列出最最耗時的頁面(超過60秒的)的以及對應頁面發生次數
# cat access.log |awk ‘($NF > 60 && $7~/\.php/){print $7}’|sort -n|uniq -c|sort -nr|head -100
 
9.列出傳輸時間超過 30 秒的文件
# cat access.log |awk ‘($NF > 30){print $7}’|sort -n|uniq -c|sort -nr|head -20
 
10.統計網站流量(G)
# cat access.log |awk ‘{sum+=$10} END {print sum/1024/1024/1024}’
 
11.統計404的連接
# awk ‘($9 ~/404/)’ access.log | awk ‘{print $9,$7}’ | sort
 
12. 統計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
 
13.查找掛馬內容進行批量清除
# find /webbase/ -type f -exec grep 'www.800816.com.cn' -l {} \;
# sed -i "s/body{.*www.800816.com.cn.*}//g" `grep www.800816.com.cn -rl ./`
 
14.批量轉換GBK爲UTF-8文件編碼
# find default -type d -exec mkdir -p utf/{} \;
# find default -type f -exec iconv -f GBK -t UTF-8 {} -o utf/{} \;
 
15.find查找文件的時候怎麼避開多個文件目錄
# find /usr/sam \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "*.txt" -print
 
16.查看tcp的併發請求數及其TCP連接狀態:
# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
# netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
# 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
 
17.查找請求數前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 -n10
 
18.查看有多少個活動的php-cgi進程
# netstat -anp | grep php-cgi | grep ^tcp | wc -l
 
19.查找較多time_wait連接
# netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
 
20.找查較多的SYN連接
# netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr | more
 
21.根據端口列進程
# netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1
 
22.抓包用來防止80端口被人***時可以分析數據
# tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts
 
23.用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
 
24.查看是哪些蜘蛛在抓取內容。
# /usr/sbin/tcpdump -i eth0 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'bot|crawler|slurp|spider'
 
25.按域統計流量
# 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]}}'
 
26.查看數據庫執行的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'
 
27.將匹配Root一行中no替換成yes
# sed -i '/Root/s/no/yes/' /etc/ssh/sshd_config
 
28.去掉第一列
# awk '{for(i=2;i<=NF;i++) if(i!=NF){printf $i" "}else{print $i} }' list
 
29.按內存從大到小排列
# ps -e -o "%C : %p : %z : %a"|sort -k5 -nr
 
30.按cpu利用率從大到小排列
# ps -e -o "%C : %p : %z : %a"|sort -nr
 
31.怎樣知道某個進程在哪個CPU上運行
# ps -eo pid,args,psr
 
32.清除僵死進程。
# ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9
 
33.查看硬件製造商
# dmidecode -s system-product-name
 
34.查找佔用磁盤IO最多的進程
# wget -c http://linux.web.psi.ch/dist/scientific/5/gfa/all/dstat-0.6.7-1.rf.noarch.rpm
# dstat -M topio -d -M topbio
 
35.檢查I/O使用率(%util)是否超過100%
# iostat -x 1 2
 
36.磁盤空間,檢查是否有分區使用率(Use%)過高(比如超過90%) 如發現某個分區空間接近用盡,可以進入該分區的掛載點,用以下命令找出佔用空間最多的文件或目錄:
# df -h
# du -cks * | sort -rn | head -n 10
 
37.CPU負載檢查前三個輸出值是否超過了系統邏輯CPU的4倍。
# cat /proc/loadavg
 
38.CPU的數量
# cat /proc/cpuinfo |grep -c processor
 
39.檢查網絡流量(rxbyt/s, txbyt/s)是否過高
# sar -n DEV
 
40.每隔1秒顯示一下網絡流量
# watch -n 1 "/sbin/ifconfig eth0 | grep bytes"
 
41.批量覆蓋目錄下的文件不用確定是否執行
# \cp -rf /svn/wwwroot /wwwroot
 
42.調試命令
# strace -p pid
 
43.跟蹤指定進程的PID
# gdb -p pid
 
44.查看當前進程打開了多少個文件句柄
 
lsof -n |awk ‘{print $2}’|sort|uniq -c |sort -nr|more
 
45.查找最近一天被修改的HTML文件
 
find -mtime -1 -type f -name \*.html
 
46.修改網站的權限
 
find -type f -name \*.php -exec chmod 444 {} \;
find ./ -type d -exec chmod 555{} \;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章