Skip to content
KALANBLON
Show contents

Chapter 6 of 7 · 25 min

Project: troubleshoot a failure

By the end of this chapter, you'll be able to

  • Apply a layer-by-layer troubleshooting method
  • Check an IP configuration: address, mask, gateway
  • Chain the DNS, TCP and HTTP checks
  • Produce a clear diagnosis and a suggested fix

The method

We check the steps in the order a browser goes through them. As soon as a step fails, the following ones cannot succeed: there is no point looking further.

We conclude on the first failing step, with a concrete lead.

Step 5 of 5
Text description of the animation
  1. Does the machine have a valid address, and is its gateway in its network?
  2. Does the site's name translate into an IP address?
  3. Does the server accept a connection on the encrypted web port?
  4. What does the server answer? A 5xx points to a problem on its side.
  5. We conclude on the first failing step, with a concrete lead.

The program

The “network” is simulated by a dictionary: the names known to DNS, each server's open ports and each site's HTTP response. Each scenario describes a computer's configuration and the site it tries to open.

import ipaddress

internet = {
    "dns": {"shop.example": "203.0.113.10", "school.example": "203.0.113.30"},
    "open_ports": {"203.0.113.10": {80, 443}, "203.0.113.30": {80}},
    "http": {"203.0.113.10": 200, "203.0.113.30": 503},
}

def diagnose(computer, site):
    interface = ipaddress.ip_interface(computer["ip"])
    gateway = ipaddress.ip_address(computer["gateway"])
    if gateway not in interface.network:
        return "IP", f"the gateway {gateway} is outside the network {interface.network}: check the mask"
    address = internet["dns"].get(site)
    if address is None:
        return "DNS", f"“{site}” matches no address: check the spelling of the name"
    if 443 not in internet["open_ports"].get(address, set()):
        return "TCP", f"{address} does not answer on port 443: the HTTPS service is stopped or filtered"
    code = internet["http"][address]
    if code >= 500:
        return "HTTP", f"the server answers {code}: the failure is on its side, not on your network"
    if code >= 400:
        return "HTTP", f"the server answers {code}: check the page address"
    return "OK", f"everything works ({code})"

scenarios = [
    ({"ip": "192.168.1.24/24", "gateway": "192.168.1.1"}, "shop.example"),
    ({"ip": "192.168.1.24/24", "gateway": "192.168.0.1"}, "shop.example"),
    ({"ip": "192.168.1.24/24", "gateway": "192.168.1.1"}, "shp.example"),
    ({"ip": "10.0.0.8/8", "gateway": "10.0.0.1"}, "school.example"),
]
for computer, site in scenarios:
    step, message = diagnose(computer, site)
    print(f"[{step:<4}] {site}: {message}")
[OK  ] shop.example: everything works (200)
[IP  ] shop.example: the gateway 192.168.0.1 is outside the network 192.168.1.0/24: check the mask
[DNS ] shp.example: “shp.example” matches no address: check the spelling of the name
[TCP ] school.example: 203.0.113.30 does not answer on port 443: the HTTPS service is stopped or filtered
  • ipaddress.ip_interface reads the address and the mask in one go, and .network gives the network;
  • internet["open_ports"].get(address, set()) returns an empty set for an unknown server, which avoids an error;
  • each failing check returns its diagnosis immediately: the following steps are not evaluated.

The fourth scenario is instructive: the user's network works perfectly, but the site school.example does not listen on port 443 (only 80, unencrypted HTTP). The TCP check stops there, before even seeing that the server also returns a 503 error.

Mini-lab: make up a failurePython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: no address at all

When a machine receives no address from the home router (the DHCP service does not answer), it gives itself an address in 169.254.x.x, called link-local, which does not allow going on the Internet. Add a very first check to diagnose: if the address is of this kind, the diagnosis is "DHCP", with advice. Test it with "169.254.12.7/16".

Hint 1
ipaddress.ip_interface(…).ip.is_link_local is True for 169.254.x.x addresses.
Hint 2
Put this test before the gateway one: it is the lowest layer.
Show the solution and common mistakes
import ipaddress

internet = {"dns": {"shop.example": "203.0.113.10"}, "open_ports": {"203.0.113.10": {443}}, "http": {"203.0.113.10": 200}}

def diagnose(computer, site):
    interface = ipaddress.ip_interface(computer["ip"])
    if interface.ip.is_link_local:
        return "DHCP", "no address received from the router: restart it or check the Wi-Fi or the cable"
    gateway = ipaddress.ip_address(computer["gateway"])
    if gateway not in interface.network:
        return "IP", f"the gateway {gateway} is outside the network {interface.network}"
    address = internet["dns"].get(site)
    if address is None:
        return "DNS", f"“{site}” matches no address"
    if 443 not in internet["open_ports"].get(address, set()):
        return "TCP", f"{address} does not answer on port 443"
    return "OK", f"everything works ({internet['http'][address]})"

print(diagnose({"ip": "169.254.12.7/16", "gateway": "192.168.1.1"}, "shop.example"))
print(diagnose({"ip": "192.168.1.24/24", "gateway": "192.168.1.1"}, "shop.example"))
('DHCP', 'no address received from the router: restart it or check the Wi-Fi or the cable')
('OK', 'everything works (200)')

Without this test, the tool would have concluded there was a gateway error: true on the surface, but the real cause is further down. The 169.254 address is a valuable clue: the machine never received its configuration.

Common mistakes:

  • Adding the test after the gateway one: it would never be reached, since the gateway 192.168.1.1 is not in the network 169.254.0.0/16.
  • Testing computer["ip"].startswith("169.254"): it works here, but it is fragile. The ipaddress methods know the exact ranges, IPv6 included.

Summary

  1. 1A network is troubleshot layer by layer, in order: IP configuration, DNS, TCP, HTTP.
  2. 2The first failing step points to the cause; the following ones cannot succeed.
  3. 3A gateway outside the local network prevents any access to the Internet.
  4. 4An error message tells where the problem shows up, not necessarily where it is.
  5. 5A 169.254.x.x address signals that no configuration was received (DHCP).

Your progress is saved on this device. Create a free account to keep your progress on all your devices.