Created
June 11, 2015 06:52
-
-
Save jta/7d7e51e6c4cf0b355232 to your computer and use it in GitHub Desktop.
Reconfigure mangle postrouting for both v4 and v6.
This file contains 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
import iptc | |
import time | |
class Iptables(object): | |
tablecls = iptc.Table | |
rulecls = iptc.Rule | |
@classmethod | |
def get_ttl_rule(cls): | |
rule = cls.rulecls() | |
rule.create_match("comment").comment = "change ttl" | |
rule.create_target("TTL").ttl_set = str(2) | |
rule.create_match("devgroup").dst_group = "!0" | |
return rule | |
@classmethod | |
def get_dscp_rule(cls): | |
rule = cls.rulecls() | |
rule.create_match("comment").comment = "add dscp mark" | |
rule.create_target("DSCP").set_dscp = str(0x20) | |
rule.create_match("devgroup").dst_group = "!0" | |
return rule | |
@classmethod | |
def reconfigure(cls): | |
table = cls.tablecls(cls.tablecls.MANGLE) | |
table.autocommit = False | |
table.refresh() | |
rules = [cls.get_ttl_rule(), cls.get_dscp_rule()] | |
chain = iptc.Chain(table, 'POSTROUTING') | |
if chain.rules != rules: | |
if chain.rules: | |
chain.flush() | |
for rule in rules: | |
chain.append_rule(rule) | |
table.commit() | |
class Ip6tables(Iptables): | |
tablecls = iptc.Table6 | |
rulecls = iptc.Rule6 | |
@classmethod | |
def get_ttl_rule(cls): | |
rule = cls.rulecls() | |
rule.create_match("comment").comment = "change ttl" | |
rule.create_target("HL").hl_set = str(2) | |
rule.create_match("devgroup").dst_group = "!0" | |
return rule | |
if __name__ == "__main__": | |
while True: | |
print "Configuring.." | |
Iptables.reconfigure() | |
Ip6tables.reconfigure() | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment