Linux Shell經(jīng)典案例二

六、寫一個腳本解決DOS***生產(chǎn)案例

提示:根據(jù)web日志或者或者網(wǎng)絡(luò)連接數(shù),監(jiān)控當某個IP并發(fā)連接數(shù)或者短時內(nèi)PV達到100,即調(diào)用防火墻命令封掉對應(yīng)的IP,監(jiān)控頻率每隔3分鐘。防火墻命令為:iptables -I INPUT -s 10.0.1.10 -j DROP。(請用至少兩種方法實現(xiàn)!)
方法1:

[root@web01 scripts]# vim david06_1.sh     #根據(jù)web日志分析
#!/bin/sh
while ture
do
  cat /application/nginx/logs/access.log |awk '{print $1}'|sort|uniq -c >/application/nginx/logs/a.log
  exec</application/nginx/logs/a.log
 
  while read line
  do
    pv=`echo $line |awk '{print $1}'`
    ip=`echo $line |awk '{print $2}'`
    if [ $pv -gt 50 ] && [ `iptables -L -n|grep "$ip"|wc -l` -eq 0 ]
     then
       iptables -A INPUT -s $ip -j DROP
     fi
  done
  sleep 3
done

方法2:

[root@web01 scripts]# vim david06_2.sh     #根據(jù)網(wǎng)絡(luò)連接數(shù)
#!/bin/sh

while true
do
   /bin/netstat -an|grep EST|awk -F "[ :]+" '{print $6}'|sort|uniq -c >/application/nginx/logs/b.log

  exec</application/nginx/logs/b.log

  while read line
  do
        pv=`echo $line |awk '{print $1}'`
        ip=`echo $line |awk '{print $2}'`
        if [ $pv -gt 50 ] && [ `iptables -L -n|grep "$ip"|wc -l` -eq 0 ]
           then
           iptables -A INPUT -s $ip -j DROP
        fi
  done
  sleep 3
done

七、已知下面的字符串是通過RANDOM隨機數(shù)變量md5sum|cut-c 1-8截取后的結(jié)果,請破解這些字符串對應(yīng)的md5sum前的RANDOM對應(yīng)數(shù)字?

21029299 00205d1c a3da1677 1f6d12dd faedd439

[root@web01 scripts]# cat david07.sh 
#!/bin/sh
array=(21029299 00205d1c a3da1677 1f6d12dd faedd439)
for n in {0..33000}
do
  MD5=`echo $n | md5sum | cut -c 1-8`
  for m in ${array[*]}
  do
    if [ "$MD5" == "$m" ]
    then 
      echo "$m 對應(yīng)的MD5前的RANDOM數(shù)為 $n。"
    fi
  done
done
[root@web01 scripts]# sh david07.sh  
00205d1c 對應(yīng)的MD5前的RANDOM數(shù)為 1346。
1f6d12dd 對應(yīng)的MD5前的RANDOM數(shù)為 7041。
a3da1677 對應(yīng)的MD5前的RANDOM數(shù)為 25345。
21029299 對應(yīng)的MD5前的RANDOM數(shù)為 25667。
faedd439 對應(yīng)的MD5前的RANDOM數(shù)為 27889。

八、批量檢查多個網(wǎng)站地址是否正常 ,

要求:shell數(shù)組方法實現(xiàn),檢測策略盡量模擬用戶訪問思路。
http://www.baidu.org
http://www.taobao.com
http://www.51cto.com
http://10.0.0.13
方法1:

[root@web01 scripts]# cat david08_1.sh 
#!/bin/sh
array=(
  http://www.baidu.org
  http://www.taobao.com
  http://www.51cto.com
  http://10.0.0.13
)
for n in ${array[*]}
do
  CURL=`curl -I $n 2>/dev/null | egrep "200|301|302"|wc -l`
  if [ $CURL -eq 1 ]
  then
    echo "$n is OK"
  else
    echo "$n is NO OK"
  fi
done
[root@web01 scripts]# sh david08_1.sh 
http://www.baidu.org is OK
http://www.taobao.com is OK
http://www.51cto.com is OK
http://10.0.0.13 is NO OK

方法2:

[root@web01 scripts]# cat david08_2.sh 
#!/bin/sh
[ -f /etc/init.d/functions ] && source /etc/init.d/functions 
array=(
  http://www.baidu.org
  http://www.taobao.com
  http://www.51cto.com
  http://10.0.0.13
)

wait(){
  echo -n "wait 3s"
  for((i=0;i<3;i++))
  do
     echo -n "."
     sleep 1
  done
  echo
}

check_url(){
  wget -T 5 -t 2 --spider $1 &>/dev/null
  RETVAL=$?
  if [ $RETVAL -eq 0 ];then
     action "check $1" /bin/true
  else
     action "check $1"  /bin/false
  fi
  return $RETVAL 
}
main(){
   wait
   for((i=0;i<${#array[@]};i++))
   do
      check_url ${array[i]}
   done  
}
main $*
[root@web01 scripts]# sh david08_2.sh  
wait 3s...
check http://www.baidu.org                                    [  OK  ]
check http://www.taobao.com                                [  OK  ]
check http://www.51cto.com                                  [  OK  ]
check http://10.0.0.13                                            [FAILED]

衍生題:(工作中常用單個檢查)

[root@web01 scripts]# cat david08 
#!/bin/sh
[ -f /etc/init.d/functions ] && source /etc/init.d/functions 

wait(){
  echo -n "wait 3s"
  for((i=0;i<3;i++))
  do
     echo -n "."
     sleep 1
  done
  echo
}

check_url(){
  wget -T 5 -t 2 --spider $1 &>/dev/null
  RETVAL=$?
  if [ $RETVAL -eq 0 ];then
     action "check $1" /bin/true
  else
     action "check $1"  /bin/false
  fi
  return $RETVAL 
}
main(){
   wait
   check_url $1
}
main $*
[root@web01 scripts]# sh curl3.sh http://www.david.com
wait 3s...
check http://www.david.com                                 [FAILED]
[root@web01 scripts]# sh curl3.sh http://www.hao123.com
wait 3s...
check http://www.hao123.com                                [  OK  ]

九、用shell處理以下內(nèi)容:

The months of learning in Old Boy education are the few months that I think the time efficient is the most.I had also studied at other training institutions before, but I was hard to understand what the tutor said and hard to follow. It was just too much to learn with no outline.
1、按單詞出現(xiàn)頻率降序排序!

[root@web01 scripts]# sed 's# #\n#g' <test.txt |sort |uniq -c|sort -nr
[root@web01 scripts]# sed 's#[,\.]##g' <test.txt|sed 's# #\n#g'|sort|uniq -c
[root@web01 scripts]# tr " ," "\n" <test.txt |awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -nr(較復雜)

2、按字母出現(xiàn)頻率降序排序!

[root@web01 scripts]# tr '\n' ' ' < test.txt |sed 's# ##g'|grep -o "\w"|sort|uniq -c|sort -nr
[root@web01 scripts]# sed 's# ##g' test.txt |grep -o "\w"|sort|uniq -c|sort -nr
[root@web01 scripts]#sed 's#[ ,.]##g' test.txt|grep -o '.' |sort|uniq -c|sort -nr

十、

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容