NetBox to hosts
Performing a traceroute is a very common task for network operations.
In this post I share a simple solution I implemented to make it easier to read the command output.
Table of Contents
Problem
I have noticed many requests to the NetOps team for clarification of traceroute output. The Service Desk team does not understand the traceroute between sites on the client’s network.
Solution
Being a big fan of NetBox I onboarded the client’s network some time ago, most of the network prefixes and IP addresses are already in the database.
Leveraging the database of IPs and names, I created a simple script to collect all IP entries in Netbox that have a non-empty DNS field.
The result is used to compile the hosts file of the local machine so you can see the names and IPs in the traceroute.
Script
The script is a mix of Python and bash. First, a Python script reads all the IP addresses in NetBox that have a non-empty DNS value and writes the output to hosts.netbox.
Then the Python script is executed by a bash script that takes care of updating the local hosts file.
Bash
Let’s start exporting NetBox IP, API and the name of the VRF:
export NBURL=https://localhost/
export NBAPI=01234567890123456789
export NBVRF=NAME-OF-VRF
The bash script:
- lines 5 to 11 create a backup of the current hosts file, if not already exists
- line 14 executes the Python script to collect the IP/name pairs and saves them in hosts.netbox
- lines 17 to 28 merge the original *hosts.old file with hosts.netbox into hosts
|
|
Python
Notice on line 17 the use of the REST api filter dns_name__empty=False .
The script getNetBoxIPnames.py requires pynetbox module.
|
|
Run it
Export the necessary data:
export NBURL=https://localhost/
export NBAPI=01234567890123456789
export NBVRF=MY-WONDERFUL-VRF
Run the script with sudo, it needs permission to write the hosts file:
./getNBnames.sh
Now run your favorite traceroute tool (mine is mtr ) and enjoy the output (obfuscated).
mtr -b 9.9.9.9
Host
1. _gateway (10.x.x.129)
2. (waiting for reply)
3. it-dc1-aci-l3out (10.199.199.1)
4. it-dc1-wsw01-vlan199 (10.x.x.60)
5. it-dc1-fw-int-1-vdom-vpn-nat (10.x.x.251)
6. it-dc1-fw--vdom-frontend-p2p-to-vdom-vpn-nat (192.x.x.130)
7. it-dc1-sw-fe-2 (192.x.x.3)
8. ce-isp1-internet-rtr-2 (212.x.x.29)
9. pe-isp1-internet (.x.x.129)
10. rtr1.mix-it.net (217.x.x.51)
11. rtr2 (109.x.x.196)
12. dns9.quad9.net (9.9.9.9)
Final notes
I recommend using explicit, long DNS names that include the host name and interface, sometimes with a short description to help ServiceDesk/NetOps teams.
If the DNS field is already used and must remain within defined standards, NetBox supports the use of custom fields .
DNS limits (according to RFC 1035 ):
- labels: 63 octets or less
- names: 255 octets or less
There is plenty of room for self-explaining entries.
Where’s the code?
The code is available in GitHub . Enjoy!
Links
Feedback
I’m always looking to improve and would love your feedback. If you spot any errors, have suggestions, or just want to share your thoughts, reach out to me directly on X.
Your input helps me make this blog better for everyone!
Support Ifconfig.it
If you enjoy the technical content I share and find it valuable, consider supporting the blog.