Skip to content

Instantly share code, notes, and snippets.

@maurorappa
Last active February 10, 2026 13:15
Show Gist options
  • Select an option

  • Save maurorappa/1c67956b476d247c94151de962105ea4 to your computer and use it in GitHub Desktop.

Select an option

Save maurorappa/1c67956b476d247c94151de962105ea4 to your computer and use it in GitHub Desktop.
print a dot graph from a traceroute
import subprocess
import re
target = "example.com"
result = subprocess.run(
["traceroute", target],
capture_output=True,
text=True
)
hops = []
for line in result.stdout.splitlines():
match = re.search(r"\(([\d.]+)\)", line)
if match:
hops.append(match.group(1))
print("digraph network_hops {")
print(" rankdir=LR;")
print(" node [shape=box, style=rounded];")
print(' hop0 [label="Local Host"];')
for i, hop in enumerate(hops, start=1):
print(f' hop{i} [label="{hop}"];')
for i in range(len(hops)):
print(f" hop{i} -> hop{i+1};")
print("}")
@maurorappa
Copy link
Author

using this script you will have a Dot language(https://graphviz.org/doc/info/lang.html) outut , you can print as image file with https://graphviz.org/, like ' dot -Tpng g.dot > g.png'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment