工作中用到的两个安全日志脚本

东坡下载 2012年02月01日 08:40:25

      1 公司服务器每天关于SSH攻击的报警很烦人,于是就在抚琴煮酒大哥实例的基础上改编成以下脚本,略有不同:
      #!/bin/bash
      #Prevent SSH attack

      SLEEPTIME=30
      lastb -n 500| grep -v "^$" | grep -v "btmp" | awk '{print $3}' | sort | uniq -c  | grep -v "公司IP" |sort -nr > attack.log
      while true 
      do
      while read line 
      do 
      IP=`echo $line | awk '{print $2}' `
      TIME=`echo $line | awk '{print $1}' `
      if [ "$TIME" -gt 10 ];then
      grep "$IP" deny.log &> /dev/null
      if [ "$?" -ne "0" ]; then
      echo "sshd: $IP" >> /etc/hosts.deny
      fi
      fi  
      done < attack.log
      /bin/sleep $SLEEPTIME
      done 
      2  线上服务因为开发的问题有些进程会莫名的死掉,需要对这些“弱势群体”不断地进行监控,如果死掉,就立即重启,于是写了以下脚本来实现(以httpd进程为例):

      #/bin/bash
      SLEEPTIME=30
      while true
      do
      id=`ps aux | grep httpd | grep -v "grep" | wc -l`
      if [ $id -lt 1 ];  then
      echo "---`date +"%F %H:%M:%S"`-----httpd restart." >> /u/scripts/httpd_monitor.log
      /etc/init.d/httpd start
      fi
      sleep $SLEEPTIME
      done 

      PS:以上脚本均需要使用nohup放在后台执行,或者使用计划任务也可以!