Skip to content

Instantly share code, notes, and snippets.

@ls0f
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ls0f/59f445ea3b768ce6a217 to your computer and use it in GitHub Desktop.
Save ls0f/59f445ea3b768ce6a217 to your computer and use it in GitHub Desktop.
block ip
#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