nadzoring.network_base.traceroute module

Traceroute implementation using system commands.

class nadzoring.network_base.traceroute.TraceHop(hop: int, host: str | None, ip: str | None, rtt_ms: list[float | None] = <factory>)[source]

Bases: object

Represents a single hop in a traceroute.

__init__(hop: int, host: str | None, ip: str | None, rtt_ms: list[float | None] = <factory>) → None[source]
hop: int
host: str | None
ip: str | None
rtt_ms: list[float | None]
nadzoring.network_base.traceroute._is_permission_error(stderr: str) → bool[source]

Return True if stderr indicates a permissions problem.

nadzoring.network_base.traceroute._parse_linux_traceroute(raw: str) → list[TraceHop][source]

Parse the output of the Linux traceroute command.

Parameters:

raw – Raw text output from traceroute.

Returns:

List of TraceHop objects.

nadzoring.network_base.traceroute._parse_windows_tracert(raw: str) → list[TraceHop][source]

Parse the output of the Windows tracert command.

Parameters:

raw – Raw text output from tracert.

Returns:

List of TraceHop objects.

nadzoring.network_base.traceroute._run_linux_traceroute(target: str, *, max_hops: int, per_hop_timeout: float, use_sudo: bool) → list[TraceHop][source]

Execute traceroute on Linux, falling back to tracepath if unavailable.

Parameters:
  • target – Hostname or IP address.

  • max_hops – Maximum number of hops.

  • per_hop_timeout – Per-hop timeout passed to traceroute -w.

  • use_sudo – Prefix the command with sudo.

Returns:

List of TraceHop objects (may be partial on timeout).

nadzoring.network_base.traceroute._run_tracepath(target: str, *, max_hops: int, per_hop_timeout: float) → list[TraceHop][source]

Fallback to tracepath (does not require root on Linux).

Parameters:
  • target – Hostname or IP address.

  • max_hops – Maximum number of hops.

  • per_hop_timeout – Used to calculate the wall-clock budget.

Returns:

List of TraceHop objects (may be partial on timeout).

nadzoring.network_base.traceroute._run_windows_tracert(target: str, *, max_hops: int, per_hop_timeout: float) → list[TraceHop][source]

Execute tracert on Windows.

Parameters:
  • target – Hostname or IP address.

  • max_hops – Maximum number of hops.

  • per_hop_timeout – Used to calculate the wall-clock budget.

Returns:

List of TraceHop objects (may be partial on timeout).

nadzoring.network_base.traceroute._stream_process(cmd: str, *, wall_timeout: float) → tuple[str, str][source]

Run a shell command and collect its output, returning partial output on timeout.

Using Popen instead of check_output lets us collect whatever stdout was written before the wall-clock deadline fires.

Parameters:
  • cmd – Shell command to execute.

  • wall_timeout – Maximum total wall-clock seconds to wait.

Returns:

Tuple of (stdout_text, stderr_text). stdout_text may be partial if the process was still running when the timeout fired.

nadzoring.network_base.traceroute.traceroute(target: str, *, max_hops: int = 30, per_hop_timeout: float = 2.0, use_sudo: bool = False) → list[TraceHop][source]

Perform a traceroute to the specified target host.

Uses ‘traceroute’ (with ‘tracepath’ fallback) on Linux and ‘tracert’ on Windows. Partial results are returned if the overall timeout fires before the trace completes.

On Linux, traceroute requires raw-socket privileges. Either run the process as root, pass use_sudo=True, or grant the capability: sudo setcap cap_net_raw+ep $(which traceroute). tracepath is tried automatically as a root-free fallback.

Parameters:
  • target – Hostname or IP address to trace.

  • max_hops – Maximum number of hops before stopping. Defaults to 30.

  • per_hop_timeout – Per-hop timeout in seconds passed to the underlying tool. The overall wall-clock budget is max_hops * per_hop_timeout * 3 + 5. Defaults to 2.0.

  • use_sudo – Prefix the command with sudo on Linux. Defaults to False.

Returns:

List of TraceHop objects. Unreachable hops have None for host/ip and rtt_ms contains [None].

Examples

>>> hops = traceroute("8.8.8.8", max_hops=5)
>>> hops[0].hop
1