本文共 1505 字,大约阅读时间需要 5 分钟。
#!/bin/bash# Author:Lee# Desc:deny cc# Date:2017-10-26 09:04:49if [[ -z $1 ]];then num=50else num=$1fi#进入脚本工作目录cd $(cd $(dirname $BASH_SOURCE) && pwd)#请求检查、判断及拉黑的主功能函数function check(){ iplist=`netstat -an |grep ^tcp.*:80|egrep -v 'LISTEN|127.0.01'|awk -F"[ ]+[:]" '{peint $6}`|sort|uniq -c|sort -rn|awk -v str=$num '{ if ($1>str){ print $2}}'` if [[ ! -z $iplist ]]; then >./black_ip.txt for black_ip in $iplist do grep -q $black_ip ./white_ip.txt if [[ $? -eq 0 ]];then echo "$black_ip (white_ip)" >>./black_ip.txt else echo $black_ip >>./black_ip.txt iptables -nl |grep $black_ip ||(iptabls -I INPUT -s $black_ip -j DROP $ echo "$black_ip `date +%Y-%m-%H:%M:%S`">>./deny.log & echo 1 >./sendmail) fi done #如果有并发超过阈值的单IP就发送邮件 if [[ `cat ./sendmail` == 1 ]];then sendmsg;fi fi}#邮件发送函数function sendmsg(){ netstat -nutlp |grep "sendmail" > /dev/null 2>&1 || /etc/init.d/sendmail start >/dev/null 2>&1 echo -e "From:发邮件地址@qq.com\nTo:收邮件地址@qq.com\nSubject:Someone Attacking your system!!\nIts Ip is" >./message cat ./black_ip.txt >>./message /usr/sbin/sendmail -f 发邮件地址@qq.com -t 收邮件地址@qq.com -i <./message >./sendmail
}
本文转自 懿懿懿 51CTO博客,原文链接:
转载地址:http://jexto.baihongyu.com/