Linux Shell经典案例二

六、写一个脚本解决DOS***生产案例

提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables -I INPUT -s 10.0.1.10 -j DROP。(请用至少两种方法实现!)
方法1:

[root@web01 scripts]# vim david06_1.sh     #根据web日志分析
#!/bin/sh
while ture
do
  cat /application/nginx/logs/access.log |awk '{print $1}'|sort|uniq -c >/application/nginx/logs/a.log
  exec

方法2:

[root@web01 scripts]# vim david06_2.sh     #根据网络连接数
#!/bin/sh

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

  exec

七、已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果,请破解这些字符串对应的md5sum前的RANDOM对应数字?

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 对应的MD5前的RANDOM数为 $n。"
    fi
  done
done
[root@web01 scripts]# sh david07.sh  
00205d1c 对应的MD5前的RANDOM数为 1346。
1f6d12dd 对应的MD5前的RANDOM数为 7041。
a3da1677 对应的MD5前的RANDOM数为 25345。
21029299 对应的MD5前的RANDOM数为 25667。
faedd439 对应的MD5前的RANDOM数为 27889。

八、批量检查多个网站地址是否正常 ,

要求:shell数组方法实现,检测策略尽量模拟用户访问思路。
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处理以下内容:

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、按单词出现频率降序排序!

[root@web01 scripts]# sed 's# #\n#g' 

2、按字母出现频率降序排序!

[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

十、

你可能感兴趣的:(Linux Shell经典案例二)