Learn How We Can Help You Keep Teleworkers Protected During the COVID-19 Crisis

API & Integration

Reply
Highlighted

Help w/ using API and DHCP

Techie
Posts: 1
7938     0
I need some assistance. I have been trying ask day to get something going. However, I can't for the life of me get anything going. I've gotten hello worldly work the api but that's about it. What I am trying to do is use a script to get all DHCP networks with option 150 exported to a list. Another example of something else that I am trying to do is locate all networks with a specific IP address for the next server for pxe boot. If possible can someone provide some example search scripts? I've been looking through the how-to on multiple sites but can't get a grip on how the scripts are poor together. I think it's mainly that I don't know where to put what I am searching for in the script.
Highlighted

I have the need for something

Techie
Posts: 8
7938     0
I have the need for something similar (I need a list with option 222). Did you ever figure out how to do this? Any information you have on how to do this would greatly be appreciated. Thank you!
Highlighted

Hi Gents,

Techie
Posts: 4
7938     0

Hi Gents,

Thank you for posting your questions, and my apologies for the delayed response.  I'm reaching out to our team(s) to get a response for you. In the interim, have you found what you need?  Feel free to ping me directly.

 

Eric

 

Highlighted

How to find DHCP ranges with a given option

Adviser
Posts: 133
7938     0

My apologies, when I saw the original question I meant to look into this, but didn't have time then. After looking into this I found one way to generate a list of ranges with a given DHCP option set. It's a three step process:

First you use a WAPI GET operation on the "range" object to ask for a list of all known DHCP ranges. An example Curl command is as follows:

curl --tlsv1 --insecure --user 'admin:infoblox' --header 'Content-Type:application/json' \
    'https://gm.example.com/wapi/v1.0/range'

(Note that' I'm using the long form of the Curl options for clarity.)

Then you have to do a GET for each range (using the returned '_ref' value for the range) to get a list of the DHCP options for that range.

curl --tlsv1 --insecure --user 'admin:infoblox' --header 'Content-Type:application/json' \
    'https://gm.example.com/wapi/v1.0/range/ZG5zLmRoY3BfcmFuZ2UkMTkyLjE2OC4yMDEuMTAwLzE5Mi4xNjguMjAxLjE0O...

Then you have to loop through the returned options for that range and look for the particular option of interest.

Below is a complete working program in Python (with error-checking code removed in the interests of space). You can adapt it by changing the section with the grid master URL, local userid and password, whether your GM uses a self-signed certificate or not, and the DHCP option number you're interested in. The program uses WAPI version 1.0 and so should work on any NIOS version supporting the RESTful API.

# Import the required Python modules.
import requests
import json
import csv

# Set parameters to access the Infoblox API for your own grid master.
url = 'https://gm.example.com/wapi/v1.0/'
id = 'admin'
pw = 'infoblox'
valid_cert = True  # False if self-signed certificate
desired_option = 7  # DHCP option to look for

# Retrieve DHCP ranges (up to first 2000, increase this if needed).
r = requests.get(url + 'range',
                 params={'_max_results': str(2000)},
                 auth=(id, pw),
                 verify=valid_cert)
ranges = r.json()

# Retrieve the options for each range, look for the option we're
# interested in.
discovered_options = []
for range in ranges:
    r = requests.get(url + range['_ref'],
                     params={'_return_fields': 'options'},
                     auth=(id, pw),
                     verify=valid_cert)
    options = r.json()['options']
    for option in options:
        if (option['vendor_class'] == 'DHCP' and
            option['num'] == desired_option):
            option_info = [range['network'],
                           range['start_addr'],
                           range['end_addr'],
                           option['num'],
                           option['value']]
            discovered_options.append(option_info)

# Print information about the ranges containing the option and the
# option's value in each range.
with open('ranges-with-option.csv', 'wb') as out_file:
    out_csv = csv.writer(out_file,
                         delimiter=',',
                         quotechar='"',
                         quoting=csv.QUOTE_MINIMAL)
    out_csv.writerow(['network', 'start_addr', 'end_addr', 'option', 'value'])
    for info in discovered_options:
        out_csv.writerow(info)

This program produces a CSV file 'ranges-with-option.csv' that has the following format:

network,start_addr,end_addr,option,value
192.168.201.0/24,192.168.201.100,192.168.201.148,7,"192.168.201.200,192.168.201.201"​

You can load the CSV file into any spreadsheet.

A few other notes: 1) I haven't looked into the question of options getting set on a range itself, or options getting set by inheritance from the enclosing network or from grid defaults. 2) In retrieving option values I noted that some options are returned with a 'use_option' value of False. I haven't looked into exactly when and why that's the case. 3) If you want to look for a vendor-specific option, change the line with the 'vendor_class' check.

Highlighted

Finding ranges with a specific IP address for PXE next server

Adviser
Posts: 133
7938     0

I forgot to address the other question, about finding DHCP ranges with a specific IP address for the PXE next server. You can follow the same procedure as above, except that for each DHCP range rather than querying for the options you query for the 'nextserver' field. Assuming you have the '_ref' value for  given range an example Curl command is as follows:

curl --tlsv1 --insecure --user 'admin:infoblox' --header 'Content-Type:application/json' \
    'https://gm.example.com/wapi/v1.0/range/ZG5zLmRoY3BfcmFuZ2UkMTkyLjE2OC4yMDEuMTAwLzE5Mi4xNjguMjAxLjE0O...

Corresponding Python code would be as follows:

discovered_nextservers = []
for range in ranges:
    r = requests.get(url + range['_ref'],
                     params={'_return_fields': 'nextserver'},
                     auth=(id, pw),
                     verify=valid_cert)
    if 'nextserver' in r.json():
        nextserver = r.json()['nextserver']
        nextserver_info = [range['network'],
                           range['start_addr'],
                           range['end_addr'],
                           nextserver]
        discovered_nextservers.append(nextserver_info)

You could generate a CSV file of all ranges with a PXE next server value set (as in my prior example), or you could do a check for the particular next server value you're interested in and just print those out, for example:

    if 'nextserver' in r.json():
        nextserver = r.json()['nextserver']
        if nextserver == '192.168.1.100':
            nextserver_info = [range['network'],
                               range['start_addr'],
                               range['end_addr'],
                               nextserver]
            discovered_nextservers.append(nextserver_info)

Note that since the PXE next server value may not be present in all ranges you need to explicitly check whether the query returned a value in the JSON response or not.

Highlighted

Re: Finding ranges with a specific IP address for PXE next server

Techie
Posts: 3
7938     0

With some of the updates to the API, is there any examples that could be posted of using Python to:

DHCP Static reservation of a IP address, with a MAC Address, then setting specific DHCP option codes to the IP address (for our purposes we want to set option codes 66/67)

 

 

Highlighted

Re: Finding ranges with a specific IP address for PXE next server

Techie
Posts: 3
7938     0

Here is a post that is helpful:

https://community.infoblox.com/t5/API-Integration/Create-Fixed-Address-Web-Api/td-p/714

 

This details out how to assign a MAC Address (fixed DHCP reservation) with a IP addy, so this is good, 

now the question is what about DHCP specific OPTION Codes, and setting these on the host. with the option codes, there is an BOX that needs to be set, or enabled, that forces the option codes to be pushed to the client regardless if they request them or not. So hopefully there is a way to set that also

Highlighted

Re: Finding ranges with a specific IP address for PXE next server

Adviser
Posts: 40
7938     0

I do not have the python code handy, but below are examples for setting options on IP addresses using curl:

 

From the WAPI guide under the fixed address object (host also has this
property):

options

An array of DHCP options that lists the DHCP options associated with the
object.
Type
A/An DHCP option  struct array.
Create
The default value is:
[ { "name": "dhcp-lease-time","num": 51,"use_option": false,"value":
"43200","vendor_class": "DHCP"}]

Search
The field is not searchable.
Notes
options is associated with the field use_options  (see use flag ).

The use options flag controls if the options are sent to the client:

use_option

Use flag for: options
Type
Bool.
Create
The default value is False.
Search
The field is not searchable.

Note that not every option requires setting the use_option flag.  E.g.,
options 66 and 67 do not use this flag.

To set these fields on a fixed address, you'd set them when you create or
update the object.  E.g.

user$ curl -k1 -u ibuser:infoblox -H "Content-Type:
appliction/json" -X POST
https://10.10.100.51/wapi/v1.4.1/fixedaddress?_return_fields=ipv4addr -d
'{"ipv4addr":"func:nextavailableip:10.30.40.0/24","mac":"ac:ff:ff:ff:ff:ff"
,"options":[{"name": "tftp-server-name","num": 66,"value":
"someserver.somedomain.com","vendor_class": "DHCP"},{"name":
"bootfile-name","num": 67,"value": "testfile","vendor_class": "DHCP"}]}'
{
    "_ref":
"fixedaddress/ZG5zLmZpeGVkX2FkZHJlc3MkMTAuMzAuNDAuMi4wLi4:10.30.40.2/defaul
t",
    "ipv4addr": "10.30.40.2"
}

 

To check to see the options have been created:


user$ curl -k1 -u ibuser:infoblox -H "Content-Type:
appliction/json" -X GET
'https://10.10.100.51/wapi/v1.4.1/fixedaddress?_return_fields%2b=options&ip
v4addr=10.30.40.2'
[
    {
        "_ref":
"fixedaddress/ZG5zLmZpeGVkX2FkZHJlc3MkMTAuMzAuNDAuMi4wLi4:10.30.40.2/defaul
t",
        "ipv4addr": "10.30.40.2",
        "network_view": "default",
        "options": [
            {
                "name": "bootfile-name",
                "num": 67,
                "value": "testfile",
                "vendor_class": "DHCP"
            },
            {
                "name": "tftp-server-name",
                "num": 66,
                "value": "someserver.somedomain.com",
                "vendor_class": "DHCP"
            },
            {
                "name": "dhcp-lease-time",
                "num": 51,
                "use_option": false,
                "value": "43200",
                "vendor_class": "DHCP"
            }
        ]
    }
]

 

The reason that lease is showing up even though I didn't create it/set it
when I created the fixed address is that this is set by default when you
create Fixed Addresses.

To change the options on an existing IP address, you would do a PUT
instead of a POST and use the object ID.

Note that in Infoblox options can be inherited, so if the options will be
the same for the entire range or network/subnet, then it is better to set
the options at the range or network level.

If you are not sure what the option name or values are, best way is to set
this through the UI on an test fixed address or host record, then query the options via the API using the _return_fields%2b=options flag.

Showing results for 
Search instead for 
Do you mean 

Recommended for You