How to get unused IP addresses via the API?

3,032 views
Skip to first unread message

Christian Mäder

unread,
May 30, 2017, 4:47:41 AM5/30/17
to NetBox
I'd like Netbox to tell me which IP, given a certain prefix, is the next available IP.

Can I do this via the API?
Would it also be possible to fetch a certain number of available IPs?

~Chris

Jeremy Stretch

unread,
May 30, 2017, 9:59:55 AM5/30/17
to Christian Mäder, NetBox
> Can I do this via the API?

The API only returns existing objects, but you can use something like the netaddr library to determine free IP addresses from a prefix.


> Would it also be possible to fetch a certain number of available IPs?

You can use the limit and offset query parameters to control how many objects are returned by any API request.

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/etPan.592d31ab.2469fa9c.158a0%40nine.ch.
For more options, visit https://groups.google.com/d/optout.

Jeremy Stretch

unread,
May 30, 2017, 10:06:53 AM5/30/17
to Christian Mäder, NetBox
Are you trying to get a list of available IPs, or check whether a particular IP is available?

On Tue, May 30, 2017 at 10:03 AM, Christian Mäder <c...@nine.ch> wrote:
So when I determine an IP from a prefix I have to check with Netbox that it's not already taken, don't I?

On 30 May 2017 at 16:00:07, 'Jeremy Stretch' via NetBox (netbox-discuss@googlegroups.com) wrote:

> Can I do this via the API?

The API only returns existing objects, but you can use something like the netaddr library to determine free IP addresses from a prefix.

> Would it also be possible to fetch a certain number of available IPs?

You can use the limit and offset query parameters to control how many objects are returned by any API request.
On Tue, May 30, 2017 at 4:47 AM, Christian Mäder <c...@nine.ch> wrote:
--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "NetBox" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/netbox-discuss/1qj4x3H0iiw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to netbox-discuss+unsubscribe@googlegroups.com.

To post to this group, send email to netbox-discuss@googlegroups.com.

Christian Mäder

unread,
May 30, 2017, 10:13:04 AM5/30/17
to Jeremy Stretch, NetBox
By available I meant 'unused', i.e. an IP that isn't already marked as active, reserved, deprecated or dhcp.

The aim is to get one or more currently un-used IPs that can be assigned to a new server.

On 30 May 2017 at 16:07:02, 'Jeremy Stretch' via NetBox (netbox-...@googlegroups.com) wrote:

Are you trying to get a list of available IPs, or check whether a particular IP is available?
On Tue, May 30, 2017 at 10:03 AM, Christian Mäder <c...@nine.ch> wrote:
So when I determine an IP from a prefix I have to check with Netbox that it's not already taken, don't I?

On 30 May 2017 at 16:00:07, 'Jeremy Stretch' via NetBox (netbox-discuss@googlegroups.com) wrote:

> Can I do this via the API?

The API only returns existing objects, but you can use something like the netaddr library to determine free IP addresses from a prefix.

> Would it also be possible to fetch a certain number of available IPs?

You can use the limit and offset query parameters to control how many objects are returned by any API request.
On Tue, May 30, 2017 at 4:47 AM, Christian Mäder <c...@nine.ch> wrote:
--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/etPan.592d31ab.2469fa9c.158a0%40nine.ch.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "NetBox" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/netbox-discuss/1qj4x3H0iiw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/CADvjYzoTSfUsS2A3aOJgpSs_cwhN2qsPT%2BDWAtqNOxq%3D%2BsUwUQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "NetBox" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/netbox-discuss/1qj4x3H0iiw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to netbox-discus...@googlegroups.com.

To post to this group, send email to netbox-...@googlegroups.com.

Josh VanDeraa

unread,
May 30, 2017, 4:58:27 PM5/30/17
to Christian Mäder, Jeremy Stretch, NetBox
I've built the request within my python script. That essentially does a for loop in range and does requests against Netbox. A colleague on the infrastructure side duplicated this in Power Shell, and the script takes about 7 seconds to get through to the next available IP in a /24. This is also doing a check to see if the response is 0 I believe? It was some trial and error to get the results we were looking to get right.


On Tue, May 30, 2017 at 9:13 AM, Christian Mäder <c...@nine.ch> wrote:
By available I meant 'unused', i.e. an IP that isn't already marked as active, reserved, deprecated or dhcp.

The aim is to get one or more currently un-used IPs that can be assigned to a new server.
--
You received this message because you are subscribed to a topic in the Google Groups "NetBox" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/netbox-discuss/1qj4x3H0iiw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-discuss@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-discuss@googlegroups.com.

Ken Jenney

unread,
Sep 28, 2017, 11:06:06 AM9/28/17
to NetBox
Josh,

Is that script on gist or github, i.e. for public consumption?

On Tuesday, May 30, 2017 at 4:58:27 PM UTC-4, Josh VanDeraa wrote:
I've built the request within my python script. That essentially does a for loop in range and does requests against Netbox. A colleague on the infrastructure side duplicated this in Power Shell, and the script takes about 7 seconds to get through to the next available IP in a /24. This is also doing a check to see if the response is 0 I believe? It was some trial and error to get the results we were looking to get right.

On Tue, May 30, 2017 at 9:13 AM, Christian Mäder <c...@nine.ch> wrote:
By available I meant 'unused', i.e. an IP that isn't already marked as active, reserved, deprecated or dhcp.

The aim is to get one or more currently un-used IPs that can be assigned to a new server.

On 30 May 2017 at 16:07:02, 'Jeremy Stretch' via NetBox (netbox-...@googlegroups.com) wrote:

Are you trying to get a list of available IPs, or check whether a particular IP is available?
On Tue, May 30, 2017 at 10:03 AM, Christian Mäder <c...@nine.ch> wrote:
So when I determine an IP from a prefix I have to check with Netbox that it's not already taken, don't I?

On 30 May 2017 at 16:00:07, 'Jeremy Stretch' via NetBox (netbox-...@googlegroups.com) wrote:

> Can I do this via the API?

The API only returns existing objects, but you can use something like the netaddr library to determine free IP addresses from a prefix.

> Would it also be possible to fetch a certain number of available IPs?

You can use the limit and offset query parameters to control how many objects are returned by any API request.
On Tue, May 30, 2017 at 4:47 AM, Christian Mäder <c...@nine.ch> wrote:
I'd like Netbox to tell me which IP, given a certain prefix, is the next available IP.

Can I do this via the API?
Would it also be possible to fetch a certain number of available IPs?

~Chris
--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.

To post to this group, send email to netbox-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/etPan.592d31ab.2469fa9c.158a0%40nine.ch.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "NetBox" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/netbox-discuss/1qj4x3H0iiw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to netbox-discus...@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "NetBox" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/netbox-discuss/1qj4x3H0iiw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to netbox-discus...@googlegroups.com.

To post to this group, send email to netbox-...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.

To post to this group, send email to netbox-...@googlegroups.com.

Jeremy Stretch

unread,
Sep 28, 2017, 11:12:57 AM9/28/17
to Ken Jenney, NetBox
FYI there's an API endpoint now that returns the first n available IPs in a prefix:

GET /api/ipam/prefixes/<id>/available-ips/?limit=100

POSTing to this endpoint will automatically create the first available IP.

Jeremy

To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to netbox-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/279c4874-557a-49ce-8780-0b220c805533%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages