Chain¶
The Chain class provides access to a chain’s rules with methods
to enumerate rules, find rules based on match and/or target,
create new rules, delete existing rules.
Chain instances also provide access to the number of packets/bytes
that have traversed a chain by appropriately aggregating the
per-rule statistics provided by iptables(8).
BuiltinChain is a subclass of Chain that additionally
provides access to the policy-related attributes of builtin chains.
- class Chain(chain_name: str)[source]¶
This class is used to represent an iptables chain. A chain contains a list of rules which can be referenced by number (rule numbers start with 1).
- Parameters:
chain_name – real chain name
- get_reference_count() int[source]¶
Returns the reference count of a (non-builtin) chain; returns 0 for builtin chains
- find_rule_by_target_lcn(logical_chain_name: str) List[ChainRule][source]¶
Return a list of rules with the specified chain as a target
- Parameters:
logical_chain_name – identifies the chain targeted by the rule
- find_rule_by(*, match: Optional[Match] = None, is_only_match=True, target: Optional[Target] = None) List[ChainRule][source]¶
Return a list of
ChainRuleobjects where the rule contains the specifiedmatchobject or has the specifiedtarget(target comparison is by name). If bothmatchandtargetare specified, returned rules must satisfy both criteria. If nomatchortargetis present, an empty list is returned.- Parameters:
match –
Matchobject to compare against; ifmatchisNone, do not perform any match comparisons; ifmatchis aMatchNoneobject, this will match a rule that has no matchesis_only_match – if
Truethe specifiedmatchmust be the only match used in the ruletarget –
Targetobject to compare against; iftargetisNone, do not perform any target comparisons; iftargetis aTargetNoneobject, this will match a rule that has no target
- get_pft() IptablesPacketFilterTable[source]¶
Returns the
IptablesPacketFilterTablewhere this chain belongs
- append_rule(rule: ChainRule) None[source]¶
Append the new rule at the end of the chain
Raises an
IptablesErrorif the rule is already part of a chain
- insert_rule(rule: ChainRule, rulenum=0) None[source]¶
Insert the new rule at the beginning of the chain (by default) or as rule number
rulenum.Raises an
IptablesErrorif the rule is already part of a chain- Parameters:
rule – the
ChainRuleto insertrulenum – rule number (starting with 1) for the inserted rule
- delete_rule(rule: ChainRule) None[source]¶
Delete the specified
rule.Raises an
IptablesErrorif the rule is not part of this chain.
- delete_rulenum(rulenum: int) None[source]¶
Delete the rule with the specified rule number
Raises an
IptablesErrorif the number is invalid- Parameters:
rulenum – rule number (numbering starts from 1)
- delete_rule_by_pred(pred: Callable[[ChainRule], bool]) int[source]¶
Delete all rules for which
predreturnsTrue.- Parameters:
pred – a
Callableobject- Return type:
number of deleted rules
- delete_rule_if(*, match: Optional[Match] = None, target: Optional[Target] = None) int[source]¶
Delete all rules with the specified
matchand/ortarget. If nomatchortargetis present, this is a no-op.- Parameters:
match –
Matchobject to compare against; the comparison will be successful if this is the only match used in the rule; ifmatchisNone, do not perform any match comparisons; ifmatchis aMatchNoneobject, this will match a rule that has no matchestarget –
Targetobject to compare against; iftargetisNone, do not perform any target comparisons; iftargetis aTargetNoneobject, this will match a rule that has no target
- Return type:
number of deleted rules
- delete_rule_by_target_chain(chain: Chain) int[source]¶
Delete all rules that jump/goto the specified chain.
- Parameters:
chain – a
Chainobject- Return type:
number of deleted rules
- classmethod create_from_existing(line_list: List[str], pft: IptablesPacketFilterTable, log_parsing_failures=True) Chain[source]¶
Parse a set of lines from the output of
iptables -xnvinto aChainobject.It raises an
IptablesParsingErrorif there is a parsing error.- Parameters:
line_list – list of iptables(8) output lines
pft – an
IptablesPacketFilterTableobjectlog_parsing_failures – if
True, log any parsing failures
- Return type:
a
Chainobject
- class BuiltinChain(chain_name, policy: Target, packet_count: int, byte_count: int)[source]¶
Bases:
ChainThis class is used to represent an iptables built-in chain.
Instances of this class are not intended to be created by the user; they are created when processing the output of iptables(8)
- Parameters:
chain_name – builtin chain name
policy – chain policy target
packet_count – number of packets that were processed according to the policy
byte_count – number of bytes for packets that were processed according to the policy
- get_policy_packet_count() int[source]¶
Returns the number of packets that were handled as per the chain policy