nadzoring.network_base.router_ip module

Default gateway (router) IP address resolution for Linux and Windows.

On some Linux distributions the net-tools package must be installed because the route command is not available by default:

sudo apt install net-tools
nadzoring.network_base.router_ip._get_linux_router_ip(*, ipv6: bool) str | None[source]

Retrieve the default gateway address on Linux via route -n.

nadzoring.network_base.router_ip._get_windows_router_ip(*, ipv6: bool) str | None[source]

Retrieve the default gateway address on Windows via route PRINT.

nadzoring.network_base.router_ip._is_valid_ipv4(value: str) bool[source]

Return True if value is a syntactically valid IPv4 address.

nadzoring.network_base.router_ip._is_valid_ipv6(value: str) bool[source]

Return True if value is a syntactically valid IPv6 address.

nadzoring.network_base.router_ip.check_ipv4(hostname: str) str[source]

Return a resolved IPv4 address for hostname, or the input unchanged.

If hostname is already a valid IPv4 address it is returned as-is. Otherwise a DNS lookup is attempted via get_ip_from_host().

Parameters:

hostname – Hostname or IPv4 address string.

Returns:

IPv4 address string, or hostname unchanged when resolution fails.

nadzoring.network_base.router_ip.check_ipv6(hostname: str) str[source]

Return a resolved IPv6 address for hostname, or the input unchanged.

If hostname is already a valid IPv6 address it is returned as-is. Otherwise a DNS lookup is attempted via get_ip_from_host().

Parameters:

hostname – Hostname or IPv6 address string.

Returns:

IPv6 address string, or hostname unchanged when resolution fails.

nadzoring.network_base.router_ip.get_ip_from_host(hostname: str) str[source]

Resolve a hostname to an IP address, returning the input on failure.

Parameters:

hostname – Hostname or IP address string to resolve.

Returns:

Resolved IP address string, or hostname unchanged if resolution fails.

nadzoring.network_base.router_ip.router_ip(*, ipv6: bool = False) str | None[source]

Return the default router (gateway) IP address for the current system.

Supports Linux (via route -n) and Windows (via route PRINT). The raw gateway value is validated and, if necessary, resolved from a hostname to an IP address.

Parameters:

ipv6 – When True, treat the gateway value as an IPv6 address. Defaults to False (IPv4).

Returns:

Gateway IP address string, or None when the gateway cannot be determined or the operating system is not supported.