API call to lookup IP?

550 views
Skip to first unread message

Jason Emery

unread,
Oct 11, 2021, 1:39:15 PM10/11/21
to NetBox
I have a use case where I'd like to query netbox API with a single IP address and figure out if that IP is defined in netbox or if the IP is within any existing prefix or IP range.

I suspect this isn't possible, but maybe would be a simple plugin? Any other suggestions?

Thanks,
Jason

Brian Candler

unread,
Oct 11, 2021, 2:36:25 PM10/11/21
to NetBox
Do you mean the REST API?  I believe currently what you want requires three calls:

- one to see if the IP address exists: /api/ipam/ip-addresses/?address=1.2.3.4

- another call to find the prefix which contains the IP address: /api/ipam/ip-addresses/?contains=1.2.3.4
This will give potentially multiple prefixes, i.e. the prefix and its ancestors, so you'll need to pick the one with the longest prefix length.

- another call to see if it's within a range: /api/ipam/ip-ranges/?contains=1.2.3.4

(also specify the vrf, if that's important).

I guess a plugin could do this.

Jason Emery

unread,
Oct 11, 2021, 7:00:46 PM10/11/21
to NetBox
Thanks - ok yeah this makes sense - that is what I was looking for - thank you!

Can the REST API be used for a more general search and return multiple objects? Like could I search in general for "1.2.3.4" and it would return any addresses, prefixes, or ranges that match?

Thanks again,
Jason



Brian Candler

unread,
Oct 12, 2021, 5:18:48 AM10/12/21
to NetBox
In the web UI there's the global search q=1.2.3.4, but I don't think that's supported in the API across multiple object types.

You can search across a single type: /api/ipam/ip-addresses/?q=1.2.3.4

But this does substring searches and may turn up things you don't expect (the above would also match 1.2.3.45, and things with 1.2.3.4 in their description).  There may also be some odd edge cases: e.g. #6016

Reply all
Reply to author
Forward
0 new messages