Skip to content

Instantly share code, notes, and snippets.

@vmlinz
Created June 19, 2015 11:19
Show Gist options
  • Save vmlinz/09c51a473b2d477431c4 to your computer and use it in GitHub Desktop.
Save vmlinz/09c51a473b2d477431c4 to your computer and use it in GitHub Desktop.
Create iptables accounting rule using python
# -*- coding: utf-8 -*-
import iptc
def add_port_out_monitor(ip="10.0.2.15", port="9999"):
table = iptc.Table(iptc.Table.FILTER)
chain = iptc.Chain(table, "OUTPUT")
rule = iptc.Rule()
rule.src = ip
rule.protocol = "tcp"
match = iptc.Match(rule, "tcp")
match.sport = port
rule.add_match(match)
target = rule.create_target("ACCEPT")
chain.insert_rule(rule)
def get_flow():
table = iptc.Table(iptc.Table.FILTER)
chain = iptc.Chain(table, 'OUTPUT')
for rule in chain.rules:
for match in rule.matches:
(packets, bytes) = rule.get_counters()
print packets, bytes, match.name, match.sport
@vmlinz
Copy link
Author

vmlinz commented Jun 19, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment