ChainRule¶
- class ChainRule(*, match: Optional[Match] = None, match_list: Optional[List[Match]] = None, target: Optional[Target] = None, uses_goto: Optional[bool] = False, goto_chain: Optional[Chain] = None)[source]¶
This class represents a rule in an iptables(8) chain. A
ChainRulehas a (possibly empty) list ofMatchobjects and an optionalTargetobject.Multiple
Matchobjects of the same type can be included in a rule. Since multipleMatchobjects imply a logical-AND, including objects of the same type may be useful when using negation. However, there can be at most onePacketMatchobject included.A
ChainRuleobject is iterable, returning the rule’sMatchinstances.- Parameters:
match – optional
Matchobject; if present, it is added to the rule’s list ofMatchobjectsmatch_list – optional list of
Matchobjects; if present, it is appended to the rule’s list ofMatchobjectstarget – a
Targetobject; either this parameter or thegoto_chainparameter may be specifieduses_goto – if
True, rule processing continues at the specified target (which must be aChainTarget) short-circuiting any rules following this one in the chaingoto_chain – an optional
Chainobject that is the target of this rule via agoto(instead ofjump); either this parameter or thetargetparameter may be specified
- get_iptables_line() Optional[str][source]¶
Returns the iptables line if this rule was created from the output of iptables(8), otherwise it returns
None.
- get_chain() Chain[source]¶
Returns the
Chainwhere this rule belongs (returnsNoneif this rule is not in any chain)
- uses_goto() bool[source]¶
Returns
Trueif this rule ‘goes’ to its (chain) target instead of ‘jumping’ to it.
- iter_match_list() Iterator[Match][source]¶
Returns an iterator for the matches of this rule.
This method is deprecated and will be removed at a future version.
- iter_matches(lookfor: Optional[Match] = None) Iterator[Match][source]¶
Returns an iterator for the matches of this rule. If
lookforis notNone, the iterator will returnMatchinstances with criteria that compare equal to those of thelookforMatch; iflookforhas no criteria defined, the iterator will returnMatchinstances of the same type as thelookforMatch.
- has_match(match: Match, is_only_match=True) bool[source]¶
Returns
Trueif the match list of this rule consists only of the specified match (whenis_only_matchisTrue) or if the match list contains the specified match (whenis_only_matchisFalse).An object of
MatchNonecan be used to test for an empty match list.
- has_target(target: Target) bool[source]¶
Returns
Trueif the rule has the specified target. An object ofTargetNonecan be used to test for lack of target.
- targets_chain(chain: Chain) bool[source]¶
Returns
Trueif the target of this rule is the specified chain- Parameters:
chain – a
Chainobject
- get_target_chain() Optional[Chain][source]¶
Returns the
Chainobject that is the target of this rule, orNoneif this rule does not target a chain.
- matches_all_packets() bool[source]¶
Returns
Trueiff this rule matches all packets. This can be because the rule has no matches, or because the only matches are comments.
- to_iptables_args() List[str][source]¶
Returns a list suitable to be used as an argument to the iptables(8) command
Raises an
IptablesErrorif this is an unparsed rule
- jump_to(*, target: Optional[Target] = None, chain: Optional[Chain] = None) ChainRule[source]¶
Add a jump to the specified target. The target is identified either via the
targetargument or via thechainargument.- Raises an
IptablesErrorif: both
targetandchainarguments are notNonethe rule is already part of a
Chain
- Raises an
- go_to(*, chain: Chain) ChainRule[source]¶
Add a goto to the specified chain.
Raises an
IptablesErrorif the rule is already part of aChain
- classmethod create_from_existing(iptables_output_line: str, pft: IptablesPacketFilterTable) ChainRule[source]¶
Create a ChainRule from a line of
iptables -xnvoutput- Parameters:
iptables_output_line – line of
iptables -xnvoutputpft – an
IptablesPacketFilterTableobject