Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zelfix opened this issue Nov 28, 2017 · 4 comments
Closed

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

zelfix opened this issue Nov 28, 2017 · 4 comments
Labels
type: feature Introduction of new functionality to the application

Comments

@zelfix
Copy link

zelfix commented 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.

@Gunni
Copy link

Gunni commented 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?

@zelfix
Copy link
Author

zelfix commented 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.

@Gunni
Copy link

Gunni commented 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

@jeremystretch jeremystretch added the type: feature Introduction of new functionality to the application label Nov 30, 2017
@icanhazpython
Copy link

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=".

@lock lock bot locked as resolved and limited conversation to collaborators Jan 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

4 participants