Skip to content

Instantly share code, notes, and snippets.

@asafge
Created November 26, 2013 08:36
Show Gist options
  • Save asafge/7655143 to your computer and use it in GitHub Desktop.
Save asafge/7655143 to your computer and use it in GitHub Desktop.
IPTables configuration samples in Python
import iptc
# sudo iptables -t filter -F
if __name__=="__main__":
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
# Block | ANY | ANY
rule_block_all = iptc.Rule()
rule_block_all.in_interface = "eth+"
rule_block_all.src = "0.0.0.0/0.0.0.0"
rule_block_all.target = iptc.Target(rule_block_all, "DROP")
chain.append_rule(rule_block_all)
# Allow | 3389 | ANY
rule_allow_rdp = iptc.Rule()
rule_allow_rdp.in_interface = "eth+"
rule_allow_rdp.src = "4.4.4.4/255.255.255.0"
match_rdp = iptc.Match(rule_allow_rdp, "tcp")
match_rdp.dport = "3389"
rule_allow_rdp.add_match(match_rdp)
rule_allow_rdp.target = iptc.Target(rule_allow_rdp, "ACCEPT")
chain.append_rule(rule_allow_rdp)
# Allow | 80 | 192.168.0.0
rule_allow_ssh = iptc.Rule()
rule_allow_ssh.in_interface = "eth+"
rule_allow_ssh.src = "192.168.0.0/255.255.0.0"
match = rule_allow_ssh.create_match("tcp")
match.dport = "80"
rule_allow_ssh.target = iptc.Target(rule_allow_ssh, "ACCEPT")
chain.append_rule(rule_allow_ssh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment