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" /etc/hosts.deny &> /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放在後臺執行,或者使用計劃任務也可以!