Fifty-three lines against an IPAM, and the three mistakes in them

A small script that answers "what is this address?" from NetBox, written in one commit, with a failure mode that reports a missing record when the real problem is a missing token.

Other

The question it answers

Reading a packet capture or a router config throws up addresses with no context, and the context lives in NetBox. The web UI answers one address at a time, which is fine until you have forty of them (one capture, one afternoon). So misc/netbox_ip_finder.sh asks two questions per address and prints both answers.

ip_url="$BASE_URL/ipam/ip-addresses/?address=$IP"
prefix_url="$BASE_URL/ipam/prefixes/?contains=$IP"

The first is an exact-match lookup on the address object (string equality, so a prefix length has to match too). The second is the one that earns the script its place: ?contains= returns every prefix that covers the address, so an address NetBox has never heard of still comes back with the subnet it belongs to and whatever description that subnet carries. An unknown address inside a known prefix is the common case, not the exception — that is what makes the second query the useful one.

Output goes through jq with defaults for the fields that are usually empty.

echo "$ip_response" | jq -r '
  .results[] |
  "• Address:      \(.address)
   Status:       \(.status.label)
   Assigned to:  \(.assigned_object | if . == null then "—" else "\(.device.name) / \(.name)" end)
   Description:  \(.description // "—")"
'

assigned_object is null for a reserved address and an object for an assigned one, so the conditional is load-bearing, and // "—" keeps an empty description from rendering as the word null.

Mistake one: a token in the file

The token is read from the environment (TOKEN=$NETBOX_API_KEY), with a Vault path in a comment above it so the next reader knows where to get one. Directly under that line, the committed script carries a real token, commented out. It was presumably a working value from the afternoon the script was written, and commenting it out is not redaction (git keeps the line either way). That is the kind of thing a secret scanner catches long after the commit, and the only correct fix is rotation, not deletion.

Mistake two: a 403 looks like an empty result

curl -s with no -f and no status check, then this.

ip_count=$(echo "$ip_response" | jq '.results | length')
if [[ "$ip_count" -gt 0 ]]; then

When the token is wrong, NetBox returns an error body with no results key, jq prints null, and bash evaluates [[ "null" -gt 0 ]] as false — because an unparseable string in an arithmetic context is zero. The script then prints that no record exists for the address — the wrong answer, delivered confidently. An authentication failure and a genuine absence produce identical output, and the only hint is that the prefix query fails the same way.

Mistake three: it never shipped

The other tools in this repository are pushed to the tankers by an Ansible playbook that copies a named list of scripts. netbox_ip_finder.sh was never added to that list, so make install-on-tankers has never carried it. The local make install target does install -m 755 misc/*.sh, which does pick it up — one glob and one explicit list, describing the same directory, disagreeing. Any script added here lands on a laptop and nowhere else unless someone edits the playbook too.

What it is worth

The script is fifty-three lines (two queries, one jq filter) and it still saves a tab and a search every time an unfamiliar address turns up. None of the three faults are in the API handling, which is the part that looks hard going in. They are in the credential, the exit status, and the install list — the parts around the edges, where nobody was looking.