예제 #1
0
    def get_counters(self, convert_units=True):
        table.refresh()
        counters = []
        for r in self.chain.rules:

            spec = rules.get_rule_spec(r)
            packets, total_bytes = r.get_counters()
            name = spec['name']
            sport = spec['sport']
            dport = spec['dport']
            dst_net = spec['dst_net']
            src_net = spec['src_net']

            if convert_units:
                total_bytes = helper.convert_to_smallest_repr(total_bytes)
                src_sub = src_net.split('/')[1]
                src_net = dst_net.split('/')[0] + '/' + helper.convert_submask_to_cidr(src_sub)
                dst_sub = dst_net.split('/')[1]
                dst_net = dst_net.split('/')[0] + '/' + helper.convert_submask_to_cidr(dst_sub)

            result = {'packets': packets,
                      'bytes': total_bytes,
                      'name': name,
                      'dst_port': dport,
                      'src_port': sport,
                      'dst_net': dst_net,
                      'src_net': src_net}
            counters.append(result)
        return counters
예제 #2
0
    def add_rule(self, new_rule):

        if not self._rule_exists(new_rule.rule):
            print 'Adding new rule with spec:'
            pprint(rules.get_rule_spec(new_rule.rule))
            self.chain.insert_rule(new_rule.rule)