Skip to content

Version 0.1.0 is a first version, for trial. See what is not proven →

bogolang
FR

← Examples

reach_hosts

Pings four hosts at once with ping_all, traces the path to one and scans a small network.

Pings four hosts at the same time with ping_all and prints one table: packets back, loss, average time, jitter, and one word per host (ok, LOSS, SLOW or DOWN). Then it traces the path to one host and scans a small network for the addresses that answer.

Run it on the saved answers in saved, with no network:

bogo run --replay saved reach_hosts.bogo
bogo test reach_hosts.bogo

To use it on your own network:

  • Put your own addresses in hosts, far and lan at the top of the class.
  • Set slow to the time you call slow on your links.
  • Run it without --replay: then it really sends pings and probes.
  • bogo run --save-replay saved reach_hosts.bogo keeps the real answers for a later replay.
reach_hosts.bogo
edition 2027
"""Is everything up? Ping a list of hosts at once, trace the path to one, scan a small network."""


class ReachHosts:
    """A morning reach check: a table of ping results, one trace and one scan."""

    hosts: list[IP] = [192.0.2.1, 192.0.2.10, 198.51.100.7, 203.0.113.20]
    far: IP = 203.0.113.20
    lan: Net = 192.0.2.0/29
    slow: Duration = 50ms

    @staticmethod
    def verdict(r: PingResult, slow: Duration) -> str:
        """One word for a ping result: DOWN, LOSS, SLOW or ok."""
        if not r.ok:
            return "DOWN"
        if r.received < r.sent:
            return "LOSS"
        if r.avg is not None and r.avg > slow:
            return "SLOW"
        return "ok"

    @staticmethod
    def main(args: list[str]) -> None:
        results: list[PingResult] = ping_all(ReachHosts.hosts, count=3, timeout=1s)
        rows: list[dict[str, object]] = []
        for r in results:
            rows.append(
                {
                    "host": r.host,
                    "received": f"{r.received}/{r.sent}",
                    "loss": r.loss,
                    "avg": r.avg,
                    "jitter": r.jitter,
                    "state": ReachHosts.verdict(r, ReachHosts.slow),
                }
            )
        table(rows)

        print(f"\nPath to {ReachHosts.far}:")
        for hop in trace(ReachHosts.far, max_hops=10, probes=2):
            print(f"  {hop}")

        up: list[IP] = scan(ReachHosts.lan)
        print(
            f"\n{len(up)} addresses answer in {ReachHosts.lan}: {', '.join(str(ip) for ip in up)}"
        )


class TestReachHosts:
    def test_no_answer_is_down(self) -> None:
        r: PingResult = PingResult(3, 0, 100pct, None, None, None)
        assert ReachHosts.verdict(r, 50ms) == "DOWN"

    def test_one_lost_packet_is_loss(self) -> None:
        r: PingResult = PingResult(3, 2, 33.3pct, 1ms, 2ms, 3ms)
        assert ReachHosts.verdict(r, 50ms) == "LOSS"

    def test_slow_and_fast(self) -> None:
        slow: PingResult = PingResult(3, 3, 0pct, 70ms, 80ms, 90ms)
        fast: PingResult = PingResult(3, 3, 0pct, 1ms, 2ms, 3ms)
        assert ReachHosts.verdict(slow, 50ms) == "SLOW"
        assert ReachHosts.verdict(fast, 50ms) == "ok"