Created
July 2, 2010 08:40
-
-
Save stattrak-dragonlore/461113 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from cStringIO import StringIO | |
def gen_iptable_rules(rules): | |
""" | |
rules = [ | |
(port, [allow_ip0, allow_ip1...]), | |
... | |
] | |
""" | |
iptables = StringIO() | |
iptables.write("*filter\n\n-A INPUT -i lo -j ACCEPT\n\n") | |
for r in rules: | |
for ip in r[1]: | |
ACTEMP = "-A INPUT -p tcp -m tcp --dport %d -s %s -j ACCEPT\n" | |
iptables.write(ACTEMP % (r[0], ip)) | |
RJTEMP = "-A INPUT -p tcp -m tcp --dport %d -j REJECT\n" | |
iptables.write(RJTEMP % (r[0])) | |
iptables.write('\n') | |
iptables.write("COMMIT\n\n######################################\n") | |
iptables.write("# ref: http://wiki.debian.org/iptables\n") | |
return iptables | |
def _test(): | |
allow_ips = ["192.168.20.1", "192.168.20.2"] | |
rules = [(1986, allow_ips), (1987, allow_ips)] # list of (port, allow_ip_list) | |
print gen_iptable_rules(rules).getvalue() | |
if __name__ == "__main__": | |
_test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment