An ability to get a parent prefix from an ip address using API #1424

Closed
opened 2025-12-29 16:32:07 +01:00 by adam · 4 comments
Owner

Originally created by @zelfix on GitHub (Nov 28, 2017).

Issue type

[X] Feature request
[ ] Bug report
[ ] Documentation

Environment

  • Python version: 3.4.5
  • NetBox version: 2.2.6

Description

It is a continuation of NetBox-discussion https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/netbox-discuss/t7J_LL2jMcM/dOvZVqEYAQAJ
Would like to get the closest parent prefix for an IP address using API.

It would be useful for those that use a netbox with external tools. For example, I want to add a list of ip addresses to netbox from a core switch's arp table using API. For each ip address I must set a prefix length. If I could get a parent prefix using API than I could get a prefix length for this IP address.

Originally created by @zelfix on GitHub (Nov 28, 2017). <!-- Before opening a new issue, please search through the existing issues to see if your topic has already been addressed. Note that you may need to remove the "is:open" filter from the search bar to include closed issues. Check the appropriate type for your issue below by placing an x between the brackets. For assistance with installation issues, or for any other issues other than those listed below, please raise your topic for discussion on our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please note that issues which do not fall under any of the below categories will be closed. Due to an excessive backlog of feature requests, we are not currently accepting any proposals which extend NetBox's feature scope. Do not prepend any sort of tag to your issue's title. An administrator will review your issue and assign labels as appropriate. ---> ### Issue type [X] Feature request <!-- An enhancement of existing functionality --> [ ] Bug report <!-- Unexpected or erroneous behavior --> [ ] Documentation <!-- A modification to the documentation --> <!-- Please describe the environment in which you are running NetBox. (Be sure to verify that you are running the latest stable release of NetBox before submitting a bug report.) If you are submitting a bug report and have made any changes to the code base, please first validate that your bug can be recreated while running an official release. --> ### Environment * Python version: 3.4.5 * NetBox version: 2.2.6 <!-- BUG REPORTS must include: * A list of the steps needed for someone else to reproduce the bug * A description of the expected and observed behavior * Any relevant error messages (screenshots may also help) FEATURE REQUESTS must include: * A detailed description of the proposed functionality * A use case for the new feature * A rough description of any necessary changes to the database schema * Any relevant third-party libraries which would be needed --> ### Description It is a continuation of NetBox-discussion https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/netbox-discuss/t7J_LL2jMcM/dOvZVqEYAQAJ Would like to get the closest parent prefix for an IP address using API. It would be useful for those that use a netbox with external tools. For example, I want to add a list of ip addresses to netbox from a core switch's arp table using API. For each ip address I must set a prefix length. If I could get a parent prefix using API than I could get a prefix length for this IP address.
adam added the type: feature label 2025-12-29 16:32:07 +01:00
adam closed this issue 2025-12-29 16:32:07 +01:00
Author
Owner

@Gunni commented on GitHub (Nov 28, 2017):

If you have the ip with mask on CIDR form, just do the following request:

/api/ipam/prefixes/?parent=10.0.0.1%2F24

In my test it returns the parent prefix of 10.0.0.0/24.

Edit: Just noticed that you said you didn't have the prefix length. But you do, if you have ARP then you have a layer 3 interface and that interface has a mask set, right?

@Gunni commented on GitHub (Nov 28, 2017): If you have the ip with mask on CIDR form, just do the following request: /api/ipam/prefixes/?parent=10.0.0.1%2F24 In my test it returns the parent prefix of 10.0.0.0/24. Edit: Just noticed that you said you didn't have the prefix length. But you do, if you have ARP then you have a layer 3 interface and that interface has a mask set, right?
Author
Owner

@zelfix commented on GitHub (Nov 28, 2017):

Yep, If I have the ip with mask, i can calculate the prefix and get it using API
But If I don't know a mask, I have to do something like this:

def get_prefix_length_for_ip(ip_address):
    for prefix_length in range(31, 0, -1):
        resp = requests.get(apiBaseUrl + '/ipam/prefixes/?parent=' + ip_address + '/' + str(prefix_length),
                            headers=headers).json()
        if resp['count'] > 0:
            return str(prefix_length)
    return None

This code gives a big load to the Netbox database that's why the function is executing slowly.

@zelfix commented on GitHub (Nov 28, 2017): Yep, If I have the ip with mask, i can calculate the prefix and get it using API But If I don't know a mask, I have to do something like this: ``` def get_prefix_length_for_ip(ip_address): for prefix_length in range(31, 0, -1): resp = requests.get(apiBaseUrl + '/ipam/prefixes/?parent=' + ip_address + '/' + str(prefix_length), headers=headers).json() if resp['count'] > 0: return str(prefix_length) return None ``` This code gives a big load to the Netbox database that's why the function is executing slowly.
Author
Owner

@Gunni commented on GitHub (Nov 28, 2017):

Just noticed that you said you didn't have the prefix length. But you do if you have ARP then you have a layer 3 interface and that interface has a mask set, right?

Example from an IOS XR router:

# show int Bundle-Ether1.XXX
Bundle-Ether1.XXX is up, line protocol is up
...
  Internet address is XX.XX.XXX.29/28
...
# show ipv4 vrf all int brief | i Bundle-Ether1.XXX
Bundle-Ether1.XXX              XX.XX.XXX.29    Up              Up       example-vrfname
# show arp vrf example-vrfname Bundle-Ether1.XXX location 0/0/CPU0
Address         Age        Hardware Addr   State      Type  Interface
XX.XX.XXX.17    00:42:23   XXXX.XXXX.XXXX  Dynamic    ARPA  Bundle-Ether1.XXX
XX.XX.XXX.29    -          XXXX.XXXX.XXXX  Interface  ARPA  Bundle-Ether1.XXX

Your code takes that and:

foreach address in ...
	requests.post(..... XX.XX.XXX.17/28
@Gunni commented on GitHub (Nov 28, 2017): > Just noticed that you said you didn't have the prefix length. But you do if you have ARP then you have a layer 3 interface and that interface has a mask set, right? Example from an IOS XR router: # show int Bundle-Ether1.XXX Bundle-Ether1.XXX is up, line protocol is up ... Internet address is XX.XX.XXX.29/28 ... # show ipv4 vrf all int brief | i Bundle-Ether1.XXX Bundle-Ether1.XXX XX.XX.XXX.29 Up Up example-vrfname # show arp vrf example-vrfname Bundle-Ether1.XXX location 0/0/CPU0 Address Age Hardware Addr State Type Interface XX.XX.XXX.17 00:42:23 XXXX.XXXX.XXXX Dynamic ARPA Bundle-Ether1.XXX XX.XX.XXX.29 - XXXX.XXXX.XXXX Interface ARPA Bundle-Ether1.XXX Your code takes that and: foreach address in ... requests.post(..... XX.XX.XXX.17/28
Author
Owner

@brettfire commented on GitHub (Jul 17, 2019):

If you have the ip with mask on CIDR form, just do the following request:

/api/ipam/prefixes/?parent=10.0.0.1%2F24

In my test it returns the parent prefix of 10.0.0.0/24.

Edit: Just noticed that you said you didn't have the prefix length. But you do, if you have ARP then you have a layer 3 interface and that interface has a mask set, right?

It appears that this is incorrect.
For those looking for this feature, the query param is actually "?contains=".

@brettfire commented on GitHub (Jul 17, 2019): > If you have the ip with mask on CIDR form, just do the following request: > > ``` > /api/ipam/prefixes/?parent=10.0.0.1%2F24 > ``` > > In my test it returns the parent prefix of 10.0.0.0/24. > > Edit: Just noticed that you said you didn't have the prefix length. But you do, if you have ARP then you have a layer 3 interface and that interface has a mask set, right? It appears that this is incorrect. For those looking for this feature, the query param is actually "?contains=".
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#1424