ChainRule¶
- class ChainRule(*, match: Optional[Match] = None, match_list: Optional[List[Match]] = None, target: Optional[Target] = None, uses_goto: Optional[bool] = False)[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.- 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
Targetobjectuses_goto – if
True, rule processing continues at the specified target (which must be aChainTarget) short-circuiting any rules following this one in the chain
- classmethod create_from_existing(iptables_output_line: str, pft) ChainRule[source]¶
Create a ChainRule from a line of
iptables -xnvoutput- Parameters:
iptables_output_line – line of
iptables -xnvoutputpft – an
IptablesPacketFilterTableobject
- get_chain()[source]¶
Returns the
Chainwhere this rule belongs (returnsNoneif this rule is not in any chain)
- 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_target_chain()[source]¶
Returns the (non-builtin)
Chainobject that is the target of this rule, orNoneif this rule does not target a chain.
- go_to(*, chain) ChainRule[source]¶
Add a goto to the specified chain.
Raises an
IptablesErrorif the rule is already part of aChainReturns this object.
- Parameters:
chain – a
Chainobject
- has_match(match: Match) bool[source]¶
Returns
Trueif the match list of this rule consists only of the specified match. An object ofMatchNonecan 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.
- jump_to(*, target: Optional[Target] = None, 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
Returns this object.
- Parameters:
target – optional
Targetobjectchain – optional
Chainobject
- Raises an
- targets_chain(chain) bool[source]¶
Returns
Trueif the target of this rule is the specified chain- Parameters:
chain – a
Chainobject
- 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