Traceroute Reference
Complete traceroute command reference for Linux, macOS, and Windows.
TracerouteReference.command
traceroute <host>TracerouteReference.flags
| Flag | Description |
|---|---|
| -n | Do not resolve hostnames (faster) |
| -m <hops> | Set maximum hops (default: 30) |
| -w <sec> | Wait time per probe (default: 5s) |
| -q <n> | Number of probes per hop (default: 3) |
| -I | Use ICMP ECHO instead of UDP |
| -T | Use TCP SYN (requires root) |
| -p <port> | Destination port for UDP/TCP |
| -i <iface> | Use specified network interface |
| -s <src> | Use specified source address |
| -4 / -6 | Force IPv4 or IPv6 |
TracerouteReference.examples
traceroute google.comBasic traceroute to a host
traceroute -n 8.8.8.8Traceroute without DNS resolution
traceroute -m 15 example.comLimit to 15 hops maximum
traceroute -I google.comUse ICMP instead of UDP
sudo traceroute -T -p 80 example.comTCP traceroute on port 80
TracerouteReference.howItWorks
TracerouteReference.howItWorksDesc
Domande Frequenti
Implementazione del Codice
import subprocess
import platform
import re
def traceroute(host: str, max_hops: int = 30) -> list[dict]:
"""Run traceroute and parse output."""
system = platform.system()
if system == "Windows":
cmd = ["tracert", "-h", str(max_hops), host]
else:
cmd = ["traceroute", "-m", str(max_hops), host]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
lines = result.stdout.splitlines()
hops = []
for line in lines[1:]: # skip header
# Parse hop number and RTT values
match = re.match(r"\s*(\d+)\s+(.+)", line)
if match:
hop_num = int(match.group(1))
rest = match.group(2)
times = re.findall(r"([\d.]+)\s*ms", rest)
hostname = re.search(r"([\w.-]+)\s+\(", rest)
hops.append({
"hop": hop_num,
"host": hostname.group(1) if hostname else "*",
"times_ms": [float(t) for t in times],
})
return hops
hops = traceroute("google.com", max_hops=15)
for hop in hops:
avg = sum(hop["times_ms"]) / len(hop["times_ms"]) if hop["times_ms"] else None
avg_str = f"{avg:.1f} ms" if avg else "*"
print(f"{hop['hop']:2d} {hop['host']:<40} {avg_str}")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.