工作中用到的兩個腳本

 

1 公司服務器每天關於SSH***的報警很煩人,於是就在撫琴煮酒大哥實例的基礎上改編成以下腳本,略有不同:

  1. #!/bin/bash 
  2. #Prevent SSH attack 

  3. SLEEPTIME=30 
  4.   lastb -n 500| grep -v "^$" | grep -v "btmp" | awk '{print $3}' | sort | uniq -c  | grep -v "公司IP" |sort -nr > attack.log 
  5. while true  
  6.  do 
  7.   while read line  
  8.     do  
  9.  IP=`echo $line | awk '{print $2}' ` 
  10.  TIME=`echo $line | awk '{print $1}' ` 
  11.  if [ "$TIME" -gt 10 ];then 
  12.    grep "$IP" /etc/hosts.deny &> /dev/null    
  13. if [ "$?" -ne "0" ]; then 
  14.    echo "sshd: $IP" >> /etc/hosts.deny 
  15.   fi 
  16. fi   
  17.   done < attack.log 
  18. /bin/sleep $SLEEPTIME 
  19. done  

 

2  線上服務因爲開發的問題有些進程會莫名的死掉,需要對這些“弱勢羣體”不斷地進行監控,如果死掉,就立即重啓,於是寫了以下腳本來實現(以httpd進程爲例):
  1. #/bin/bash 
  2. SLEEPTIME=30 
  3. while true 
  4.   do 
  5.   id=`ps aux | grep httpd | grep -v "grep" | wc -l` 
  6.    if [ $id -lt 1 ];  then 
  7.       echo "---`date +"%F %H:%M:%S"`-----httpd restart." >> /u/scripts/httpd_monitor.log 
  8.       /etc/init.d/httpd start 
  9.    fi 
  10.  
  11.   sleep $SLEEPTIME 
  12.  
  13. done  

 PS:以上腳本均需要使用nohup放在後臺執行,或者使用計劃任務也可以!

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