Created
June 19, 2015 11:19
-
-
Save vmlinz/09c51a473b2d477431c4 to your computer and use it in GitHub Desktop.
Create iptables accounting rule using python
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
# -*- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: http://blog.yunlong.me/2015/03/09/port-flow-control/