PacketMatch¶
Example:
>>> m = PacketMatch()
>>> m.input_interface().equals('eth0')
<linuxnet.iptables.match.PacketMatch object at 0x7ff96e466e10>
>>> m.fragment().not_equals()
<linuxnet.iptables.match.PacketMatch object at 0x7ff96e466e10>
>>> m.source_address().equals(IPv4Network('192.168.1.0/24'))
<linuxnet.iptables.match.PacketMatch object at 0x7ff96e466e10>
>>> m.to_iptables_args()
['-i', 'eth0', '!', '-f', '-s', '192.168.1.0/24']
- class PacketMatch(*, ipv6=False)[source]¶
This class provides matching against the following attributes of a packet:
input interface
output interface
protocol
source address
destination address
fragment bit (IPv4-only)
- Parameters:
ipv6 – optional boolean to indicate IPv6 address matching when
True; the default is IPv4
- static get_match_name() Optional[str][source]¶
Returns the iptables(8) match extension name. In the case of the standard packet match, there is no name.
- get_criteria() Iterable[Criterion][source]¶
Returns the packet match criteria: input-interface, output-interface, protocol, fragmented, source, destination.
- protocol() ProtocolCriterion[source]¶
Match against the protocol
- input_interface() InputInterfaceCriterion[source]¶
Match against the input interface
- output_interface() OutputInterfaceCriterion[source]¶
Match against the output interface
- source_address() SourceAddressCriterion[source]¶
Match against the source address
- dest_address() DestAddressCriterion[source]¶
Match against the destination address
- fragment() FragmentCriterion[source]¶
Match if packet has (or has not) the fragment bit set
InputInterfaceCriterion¶
- class InputInterfaceCriterion(match: Match)[source]¶
Compare with the input interface.
The comparison value is an interface name (a string).
- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- get_iptables_option() str¶
Returns the iptables(8) option
- get_value() Any¶
Returns the criterion value
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set
OutputInterfaceCriterion¶
- class OutputInterfaceCriterion(match: Match)[source]¶
Compare with the output interface.
The comparison value is an interface name (a string).
- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- get_iptables_option() str¶
Returns the iptables(8) option
- get_value() Any¶
Returns the criterion value
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set
ProtocolCriterion¶
- class ProtocolCriterion(match: Match)[source]¶
Compare with the protocol.
The comparison value is a protocol name (a string); it may also be a number in string form if there is no mapping of that number to a protocol name in
/etc/protocols.- equals(proto) Match[source]¶
Compare with the specified protocol.
- Parameters:
proto – the parameter can a string or an integer; if it is an integer, it will be converted to the corresponding protocol name, if possible, otherwise it will be used as-is in string form (i.e. 199 will be converted to “199”)
- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set
SourceAddressCriterion¶
- class SourceAddressCriterion(match: Match, *, ipv6: Optional[bool] = None)[source]¶
Compare with the source address.
The comparison value is an
IPv4Networkor anIPv6Network- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- equals(value: Union[IPv4Network, IPv6Network, IPv4Address, IPv6Address, str]) Match¶
Compare with the specified value, which can be specified as an
IPv4Network, anIPv6Network, anIPv4Address, anIPv6Address, or as a string. Internally the value is always stored as anIPv4Network, or anIPv6Network.
- get_iptables_option() str¶
Returns the iptables(8) option
- get_value() Optional[Union[IPv4Network, IPv6Network]]¶
Returns the criterion value
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set
DestAddressCriterion¶
- class DestAddressCriterion(match: Match, *, ipv6: bool)[source]¶
Compare with the destination address.
The comparison value is an
IPv4Networkor anIPv6Network- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- equals(value: Union[IPv4Network, IPv6Network, IPv4Address, IPv6Address, str]) Match¶
Compare with the specified value, which can be specified as an
IPv4Network, anIPv6Network, anIPv4Address, anIPv6Address, or as a string. Internally the value is always stored as anIPv4Network, or anIPv6Network.
- get_iptables_option() str¶
Returns the iptables(8) option
- get_value() Optional[Union[IPv4Network, IPv6Network]]¶
Returns the criterion value
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set
FragmentCriterion¶
- class FragmentCriterion(match: Match)[source]¶
Check if a packet is a fragment.
- any() Match¶
Match any value.
This method is used when creating a
Criterionin order to search an existing chain for rules that try to match against certain packet properties (e.g. input interface) without being particular about the specific property value (e.g.eth0).
- compare(is_equal: bool, *args, **kwargs) Match¶
Alternative method used for comparisons. It invokes
equals()(ornot_equals()) withargsandkwargsifis_equalisTrue(orFalse).
- get_value() bool¶
Returns the criterion value
- is_positive() bool¶
Returns the ‘polarity’ of the criterion:
Trueforequals()orFalsefornot_equals()Raises
IptablesErrorif the criterion is not set
- is_set() bool¶
Returns
Trueif the criterion has been set