Last active
February 10, 2026 13:15
-
-
Save maurorappa/1c67956b476d247c94151de962105ea4 to your computer and use it in GitHub Desktop.
print a dot graph from a traceroute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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("}") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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'