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).
A
Chaininstance is iterable, returning the chain’s rules.The
Chainclass supports the standardlen()function returning the number of rules in the chain.The
Chainclass supports integer-based indexing (slices are not supported). Positive integers are interpreteted as rule numbers, i.e. indexing starts at1. Index0will raise anIndexError. Negative index values are supported with-1identifying the last rule,-2identifying the penultimate rule, etc.- Parameters:
chain_name – real chain name
- has_rules() bool[source]¶
Returns
Trueif the chain contains any rules (note that aChaininstance can also be used directly in a boolean content; if empty, it evaluates toFalse).
- get_reference_count() int[source]¶
Returns the reference count of a (non-builtin) chain; returns 0 for builtin chains
- get_rule_count() int[source]¶
Returns the number of rules in the chain (note that the standard
len()function is also supported)
- iter_rules(*, chain_target=False, uses_goto=False, match_count: Optional[int] = None, match: Optional[Match] = None) Iterator[ChainRule][source]¶
Returns an iterator for the chain rules. The rules returned by the iterator depend on the arguments:
- Parameters:
chain_target – if
True, return rules where the target is a chainuses_goto – if
True, return rules that use gotomatch_count – if not
None, return rules that have that number of matchesmatch – if not
None, return rules that have a matchingMatchin their match list; if thematchhas no criteria set, it will match any :class:Matchinstance of the same class
- 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