iptables module¶
Ip6tablesChains
¶
The ip6tables chains and their policies in one table.
Commands:
ip6tables-save -t <table> 2> /dev/null || sudo -n ip6tables-save -t <table> 2> /dev/null || true
Returns:
dict[str, str]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_ip6tables_chains.py.
Ip6tablesPersistence
¶
Whether the live ip6tables rules match a saved snapshot file.
Commands:
sh -c 'live=$( (ip6tables-save 2>/dev/null || sudo -n ip6tables-save 2>/dev/null) | sed '"'"'/^#/d'"'"'); saved=$(sed '"'"'/^#/d'"'"' <path> 2>/dev/null || true); [ -n "$live" ] && [ "$live" = "$saved" ] && echo synced || echo drifted'
Returns:
bool
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_ip6tables_persistence.py.
Ip6tablesRules
¶
The ip6tables rules of one table, as parsed rule definitions.
Commands:
ip6tables-save -t <table> 2> /dev/null || sudo -n ip6tables-save -t <table> 2> /dev/null || true
Returns:
list[IptablesRule]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_ip6tables_rules.py.
Ip6tablesRulesOrdered
¶
The ip6tables rules with line numbers and packet/byte counters.
Commands:
ip6tables -t <table> -L <chain> -n -v -x --line-numbers 2> /dev/null || sudo -n ip6tables -t <table> -L <chain> -n -v -x --line-numbers 2> /dev/null || true
Returns:
list[IptablesOrderedRule]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_ip6tables_rules_ordered.py.
Ip6tablesTables
¶
The ip6tables tables currently loaded in the kernel.
Commands:
cat /proc/net/ip6_tables_names 2>/dev/null || true
Returns:
list[str]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_ip6tables_tables.py.
Ip6tablesVersion
¶
The installed ip6tables release and its legacy-vs-nft backend.
Commands:
ip6tables --version
Returns:
IptablesVersionInfo | None
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_ip6tables_version.py.
IpSet
¶
Attributes:
| Name | Type | Description |
|---|---|---|
type |
The set's type ( |
|
revision |
The kernel set-type revision number. |
|
header |
The |
|
size_in_memory |
The set's in-memory size in bytes. |
|
references |
How many iptables rules reference the set. |
|
members |
The set's entries, one string per member ( |
IpSets
¶
The ipset containers, their types and their members.
Commands:
ipset list 2> /dev/null || sudo -n ipset list 2> /dev/null || true
Returns:
dict[str, IpSet]
Raises:
HostConnectionError
Examples:
Examples can be found in examples/modules/iptables/iptables_ip_sets.py.
IptablesChains
¶
The iptables chains and their policies in one table.
Commands:
iptables-save -t <table> 2> /dev/null || sudo -n iptables-save -t <table> 2> /dev/null || true
Returns:
dict[str, str]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_iptables_chains.py.
IptablesModules
¶
The loaded xt_* iptables match/target kernel modules.
Commands:
lsmod | grep '^xt_' || true
Returns:
list[str]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_iptables_modules.py.
IptablesOrderedRule
¶
Attributes:
| Name | Type | Description |
|---|---|---|
chain |
The chain the rule lives in (from the |
|
number |
The 1-based rule number within its chain (the
|
|
packets |
The exact packets counter ( |
|
bytes |
The exact bytes counter ( |
|
target |
The rule's target column ( |
|
protocol |
The protocol column as printed ( |
|
opt |
The options column, usually |
|
in_interface |
The incoming interface column ( |
|
out_interface |
The outgoing interface column ( |
|
source |
The source address/CIDR column ( |
|
destination |
The destination address/CIDR column. |
|
options |
The trailing match/target detail text ( |
IptablesPersistence
¶
Whether the live iptables rules match a saved snapshot file.
Commands:
sh -c 'live=$( (iptables-save 2>/dev/null || sudo -n iptables-save 2>/dev/null) | sed '"'"'/^#/d'"'"'); saved=$(sed '"'"'/^#/d'"'"' <path> 2>/dev/null || true); [ -n "$live" ] && [ "$live" = "$saved" ] && echo synced || echo drifted'
Returns:
bool
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_iptables_persistence.py.
IptablesRule
¶
Attributes:
| Name | Type | Description |
|---|---|---|
chain |
The chain the rule lives in (the |
|
jump |
The rule's target (the |
|
protocol |
The protocol match ( |
|
source |
The source address/CIDR match, when set. |
|
destination |
The destination address/CIDR match, when set. |
|
in_interface |
The incoming interface match, when set. |
|
out_interface |
The outgoing interface match, when set. |
|
not_protocol |
The negated protocol match ( |
|
not_source |
The negated source match ( |
|
not_destination |
The negated destination match ( |
|
not_in_interface |
The negated incoming interface match, when set. |
|
not_out_interface |
The negated outgoing interface match, when set. |
|
log_prefix |
The LOG target's |
|
to_destination |
The DNAT target's |
|
to_source |
The SNAT target's |
|
to_ports |
The REDIRECT target's |
|
extras |
Every unmapped flag as sorted |
IptablesRules
¶
The iptables rules of one table, as parsed rule definitions.
Commands:
iptables-save -t <table> 2> /dev/null || sudo -n iptables-save -t <table> 2> /dev/null || true
Returns:
list[IptablesRule]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_iptables_rules.py.
IptablesRulesOrdered
¶
The iptables rules with line numbers and packet/byte counters.
Commands:
iptables -t <table> -L <chain> -n -v -x --line-numbers 2> /dev/null || sudo -n iptables -t <table> -L <chain> -n -v -x --line-numbers 2> /dev/null || true
Returns:
list[IptablesOrderedRule]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_iptables_rules_ordered.py.
IptablesTables
¶
The iptables tables currently loaded in the kernel.
Commands:
cat /proc/net/ip_tables_names 2>/dev/null || true
Returns:
list[str]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_iptables_tables.py.
IptablesVersion
¶
The installed iptables release and its legacy-vs-nft backend.
Commands:
iptables --version
Returns:
IptablesVersionInfo | None
Raises:
CommandNotFoundError, HostConnectionError
Examples:
Examples can be found in
examples/modules/iptables/iptables_iptables_version.py.
IptablesVersionInfo
¶
Attributes:
| Name | Type | Description |
|---|---|---|
version |
The iptables release string, as printed after the |
|
backend |
The packet-filtering backend the binary drives:
|
chain
async
¶
chain(
ctx: OpContext,
chain: str,
present: bool = True,
table: str = "filter",
policy: str | None = None,
version: int = 4,
**kwargs: Any,
) -> AsyncGenerator[Command]
Add, remove or update an iptables chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
chain
|
str
|
The name of the chain to manage. |
required |
present
|
bool
|
Whether the chain should exist. Defaults to True. |
True
|
table
|
str
|
The iptables table the chain belongs to. Defaults to "filter". |
'filter'
|
policy
|
str | None
|
The policy the chain should have
(built-in chains only -- the kernel rejects |
None
|
version
|
int
|
Whether to target iptables (4) or ip6tables (6). Defaults to 4. |
4
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated iptables chain commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/iptables/iptables_chain.py.
flush
async
¶
flush(
ctx: OpContext,
chain: str | None = None,
table: str = "filter",
version: int = 4,
**kwargs: Any,
) -> AsyncGenerator[Command]
Flush the rules of one chain, or of every chain in a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
chain
|
str | None
|
The chain to flush; None flushes every chain of the table. Defaults to None. |
None
|
table
|
str
|
The iptables table to flush. Defaults to "filter". |
'filter'
|
version
|
int
|
Whether to target iptables (4) or ip6tables (6). Defaults to 4. |
4
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated iptables flush commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/iptables/iptables_flush.py.
ipset
async
¶
ipset(
ctx: OpContext,
set_name: str,
set_type: str = "hash:ip",
entries: tuple[str, ...] = (),
present: bool = True,
**kwargs: Any,
) -> AsyncGenerator[Command]
Manage an ipset container and its members.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
set_name
|
str
|
The name of the set to manage. |
required |
set_type
|
str
|
The set type used when creating it
( |
'hash:ip'
|
entries
|
tuple[str, ...]
|
The member entries to add
( |
()
|
present
|
bool
|
Whether the set (and the listed entries) should exist. Defaults to True. |
True
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated ipset commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/iptables/iptables_ipset.py.
policy
async
¶
policy(
ctx: OpContext,
chain: str,
policy: str,
table: str = "filter",
version: int = 4,
**kwargs: Any,
) -> AsyncGenerator[Command]
Set the default policy of a built-in iptables chain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
chain
|
str
|
The built-in chain whose policy to set ( |
required |
policy
|
str
|
The policy target: |
required |
table
|
str
|
The iptables table the chain belongs to. Defaults to "filter". |
'filter'
|
version
|
int
|
Whether to target iptables (4) or ip6tables (6). Defaults to 4. |
4
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated iptables policy commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/iptables/iptables_policy.py.
restore
async
¶
restore(
ctx: OpContext,
path: str,
version: int = 4,
noflush: bool = False,
**kwargs: Any,
) -> AsyncGenerator[Command]
Atomically load iptables rules from a snapshot file on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
path
|
str
|
The host-side iptables-restore format file to load
(upload it with |
required |
version
|
int
|
Whether to restore iptables (4) or ip6tables (6) rules. Defaults to 4. |
4
|
noflush
|
bool
|
Whether to append the file's rules
without flushing the existing ones ( |
False
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated iptables-restore commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/iptables/iptables_restore.py.
rule
async
¶
rule(
ctx: OpContext,
chain: str,
jump: str,
present: bool = True,
table: str = "filter",
append: bool = True,
version: int = 4,
position: int | None = None,
protocol: str | None = None,
not_protocol: str | None = None,
source: str | None = None,
not_source: str | None = None,
destination: str | None = None,
not_destination: str | None = None,
in_interface: str | None = None,
not_in_interface: str | None = None,
out_interface: str | None = None,
not_out_interface: str | None = None,
to_destination: str | None = None,
to_source: str | None = None,
to_ports: int | str | None = None,
log_prefix: str | None = None,
log_level: int | str | None = None,
destination_port: int | None = None,
source_port: int | None = None,
destination_ports: tuple[int | str, ...] = (),
source_ports: tuple[int | str, ...] = (),
ctstate: tuple[str, ...] = (),
limit: str | None = None,
limit_burst: int | None = None,
match_set: tuple[str, str] | None = None,
comment: str | None = None,
extras: str = "",
**kwargs: Any,
) -> AsyncGenerator[Command]
Add or remove an iptables rule, with typed match extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
chain
|
str
|
The chain this rule should live in. |
required |
jump
|
str
|
The target of the rule ( |
required |
present
|
bool
|
Whether the rule should exist. Defaults to True. |
True
|
table
|
str
|
The iptables table this rule belongs to. Defaults to "filter". |
'filter'
|
append
|
bool
|
Whether to append (True) or insert at the top (False) when adding the rule. Defaults to True. |
True
|
version
|
int
|
Whether to target iptables (4) or ip6tables (6). Defaults to 4. |
4
|
position
|
int | None
|
The 1-based line number to
insert a new rule at (overrides |
None
|
protocol
|
str | None
|
Filter by protocol ( |
None
|
not_protocol
|
str | None
|
Negated protocol filter. Defaults to None. |
None
|
source
|
str | None
|
Filter by source address/CIDR. Defaults to None. |
None
|
not_source
|
str | None
|
Negated source filter. Defaults to None. |
None
|
destination
|
str | None
|
Filter by destination address/CIDR. Defaults to None. |
None
|
not_destination
|
str | None
|
Negated destination filter. Defaults to None. |
None
|
in_interface
|
str | None
|
Filter by incoming interface. Defaults to None. |
None
|
not_in_interface
|
str | None
|
Negated incoming interface filter. Defaults to None. |
None
|
out_interface
|
str | None
|
Filter by outgoing interface. Defaults to None. |
None
|
not_out_interface
|
str | None
|
Negated outgoing interface filter. Defaults to None. |
None
|
to_destination
|
str | None
|
Where to route when
|
None
|
to_source
|
str | None
|
Where to route when
|
None
|
to_ports
|
int | str | None
|
Where to route when
|
None
|
log_prefix
|
str | None
|
Log prefix when
|
None
|
log_level
|
int | str | None
|
Syslog level when
|
None
|
destination_port
|
int | None
|
Destination port
shortcut (requires |
None
|
source_port
|
int | None
|
Source port shortcut
(requires |
None
|
destination_ports
|
tuple[int | str, ...]
|
Multiport
destination match, e.g. |
()
|
source_ports
|
tuple[int | str, ...]
|
Multiport
source match (requires |
()
|
ctstate
|
tuple[str, ...]
|
Conntrack state match, e.g.
|
()
|
limit
|
str | None
|
Rate limit match, e.g.
|
None
|
limit_burst
|
int | None
|
Burst allowance for
|
None
|
match_set
|
tuple[str, str] | None
|
An ipset match as
|
None
|
comment
|
str | None
|
Rule comment (the |
None
|
extras
|
str
|
Raw iptables extension arguments for
anything the typed parameters do not cover (e.g.
|
''
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated iptables rule commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/iptables/iptables_rule.py.
save
async
¶
save(
ctx: OpContext,
path: str | None = None,
version: int = 4,
**kwargs: Any,
) -> AsyncGenerator[Command]
Persist the live iptables rules to a snapshot file on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
path
|
str | None
|
The host-side file to write;
None picks |
None
|
version
|
int
|
Whether to save iptables (4) or ip6tables (6) rules. Defaults to 4. |
4
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated iptables-save commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/iptables/iptables_save.py.
zero
async
¶
zero(
ctx: OpContext,
chain: str | None = None,
table: str = "filter",
version: int = 4,
**kwargs: Any,
) -> AsyncGenerator[Command]
Zero the packet and byte counters of one chain, or of a whole table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
chain
|
str | None
|
The chain whose counters to zero; None zeroes every chain of the table. Defaults to None. |
None
|
table
|
str
|
The iptables table to zero. Defaults to "filter". |
'filter'
|
version
|
int
|
Whether to target iptables (4) or ip6tables (6). Defaults to 4. |
4
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
The iptables zero command, always (the operation is |
AsyncGenerator[Command]
|
stateless). |
Raises:
ValueError
Examples:
Examples can be found in examples/modules/iptables/iptables_zero.py.