Last active
August 29, 2015 14:20
-
-
Save ls0f/59f445ea3b768ce6a217 to your computer and use it in GitHub Desktop.
block ip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
import os | |
## block cc攻击 的ip | |
CMD = "netstat -ntup | grep -v SYN |awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr" | |
MAX_CONN = 50 | |
def block_ip(): | |
lines = os.popen(CMD).readlines() | |
for line in lines: | |
line = line.strip() | |
if not line: | |
continue | |
conn_num, ip = line.split() | |
if int(conn_num) > MAX_CONN: | |
if ip.startswith('127.0.0'): | |
print '内网ip:%s,conn:%s' % (ip, conn_num) | |
elif ip.startswith('10.'): | |
print '内网ip:%s,conn:%s' % (ip, conn_num) | |
else: | |
print 'ip:%s,conn:%s' % (ip, conn_num) | |
cmd = "iptables -A INPUT -s %s -j DROP" % ip | |
print cmd | |
os.system(cmd) | |
else: | |
return | |
if __name__ == "__main__": | |
block_ip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment