思路:主要利用 awk ,if結(jié)構(gòu),sort,uniq
#!/bin/bashFilePath="access.log"awk '{print $1}' $FilePath | sort -rn | uniq -c >ip_count.logcat ip_count.log | while read text ####讀取文件內(nèi)容,以行為單位doecho $textcount=`echo $text | awk '{print $1}' `ip=`echo $text | awk '{print $2}'`if [ $count -gt 20 ]thenif iptables -L | grep $ip ###判斷是否已經(jīng)在iptables 中thenecho "ip地址存在iptables中,不添加 " elseecho "添加ip地址到iptables"iptables -A INPUT -s $ip -j DROP && echo $ip >>ip_drop.log/etc/init.d/iptables save &> /dev/null ###使iptables 生效/etc/init.d/iptables restart &> /dev/nullfielseecho "未到達(dá)標(biāo)準(zhǔn),不添加到iptables"fidone最后,我們可以把腳本添加到定時(shí)任務(wù)(crontab)里去,定時(shí)執(zhí)行,這樣就可以實(shí)現(xiàn)動(dòng)態(tài)添加了。
以上所述是小編給大家介紹的利用Shell 腳本解決DDOS攻擊問題,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林站長站網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注