ufw module¶
UfwApplications
¶
The application profiles installed on the host.
Commands:
ufw app list 2> /dev/null || sudo -n ufw app list 2> /dev/null || true
Returns:
list[str]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/ufw/ufw_ufw_applications.py.
UfwRule
¶
Attributes:
| Name | Type | Description |
|---|---|---|
number |
The 1-based rule number (the |
|
action |
The verdict word ( |
|
direction |
The direction word ( |
|
log |
Whether the rule logs (the trailing |
|
v6 |
Whether the row is the IPv6 family member (the |
|
to_addr |
The destination address/CIDR ( |
|
port |
The destination port token ( |
|
protocol |
The protocol suffix ( |
|
app |
The application profile name ( |
|
from_addr |
The source address/CIDR ( |
|
interface |
The bound interface (the |
|
comment |
The rule comment (the trailing |
UfwRules
¶
The ufw ruleset as numbered, structured rule entries.
Commands:
ufw status numbered 2> /dev/null || sudo -n ufw status numbered 2> /dev/null || true
Returns:
list[UfwRule]
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/ufw/ufw_ufw_rules.py.
UfwStatus
¶
Whether ufw is active, its logging level and its default policies.
Commands:
ufw status verbose 2> /dev/null || sudo -n ufw status verbose 2> /dev/null || true
Returns:
UfwStatusInfo
Raises:
HostConnectionError
Examples:
Examples can be found in
examples/modules/ufw/ufw_ufw_status.py.
UfwStatusInfo
¶
Attributes:
| Name | Type | Description |
|---|---|---|
active |
Whether |
|
logging |
The logging level word ( |
|
default_incoming |
The default incoming policy ( |
|
default_outgoing |
The default outgoing policy; |
|
default_routed |
The default routed (forwarded) policy
( |
default_policy
async
¶
default_policy(
ctx: OpContext,
direction: str = "incoming",
policy: str = "deny",
**kwargs: Any,
) -> AsyncGenerator[Command]
Set the default policy for incoming, outgoing or routed traffic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
direction
|
str
|
Which default policy to manage:
|
'incoming'
|
policy
|
str
|
The fallback verdict: |
'deny'
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated ufw default-policy commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/ufw/ufw_default_policy.py.
logging
async
¶
Set the ufw logging level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
level
|
str
|
The logging level: |
'low'
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated ufw logging commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/ufw/ufw_logging.py.
reset
async
¶
Reset ufw to installation defaults, deleting every rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
The reset command, always (the operation is stateless). |
Raises:
None
Examples:
Examples can be found in examples/modules/ufw/ufw_reset.py.
rule
async
¶
rule(
ctx: OpContext,
port: int | str | tuple[int | str, ...] | None = None,
protocol: str | None = None,
action: str = "allow",
present: bool = True,
direction: str = "in",
route: bool = False,
from_ip: str | None = None,
to_ip: str | None = None,
app: str | None = None,
interface: str | None = None,
log: bool = False,
comment: str | None = None,
insert: int | None = None,
**kwargs: Any,
) -> AsyncGenerator[Command]
Add or remove a ufw rule (ports, profiles, addresses, routes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
port
|
int | str | tuple[int | str, ...] | None
|
The
destination port: a single port ( |
None
|
protocol
|
str | None
|
The protocol, |
None
|
action
|
str
|
The verdict: |
'allow'
|
present
|
bool
|
Whether the rule should exist. Defaults to True. |
True
|
direction
|
str
|
Traffic direction, |
'in'
|
route
|
bool
|
Whether this is a routed (forwarded)
traffic rule ( |
False
|
from_ip
|
str | None
|
Source address/CIDR restriction; None means any source. Defaults to None. |
None
|
to_ip
|
str | None
|
Destination address/CIDR restriction; None means any destination. Defaults to None. |
None
|
app
|
str | None
|
An application profile name (e.g.
|
None
|
interface
|
str | None
|
Bind the rule to an interface
(the incoming interface for |
None
|
log
|
bool
|
Whether matched packets are logged. Defaults to False. |
False
|
comment
|
str | None
|
Rule comment -- the reliable
auditing marker, shown by |
None
|
insert
|
int | None
|
The 1-based position to insert a new rule at; an existing rule is never repositioned, and deletion ignores it. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated ufw rule commands. |
Raises:
HostConnectionError, ValueError
Examples:
Examples can be found in examples/modules/ufw/ufw_rule.py.
state
async
¶
Ensure the ufw firewall is enabled or disabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
OpContext
|
Execution context for the operation. |
required |
enabled
|
bool
|
Whether the firewall should be active (and start on boot). Defaults to True. |
True
|
**kwargs
|
Any
|
Additional keyword arguments. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
Command |
AsyncGenerator[Command]
|
Generated ufw state commands. |
Raises:
HostConnectionError
Examples:
Examples can be found in examples/modules/ufw/ufw_state.py.