Select Page

Today we are going to take a look at retrieving different types of data from Infoblox via the RESTFUL API (a.k.a “Web API”, or WAPI) with the “requests” module and the “infoblox-client” using Python. You might want to check out the last article, “Introduction to Infoblox API (WAPI) using Python“, to get you up and running before you try the examples below.

Using WAPI to with the Python “requests” module to search

We are going to start off looking for all “networks” in Infoblox via WAPI. To do this, we will use the path of “/wapi/v2.10/network”. You can find more information about the Infoblox WAPI at “https://docs.infoblox.com“.

The Infoblox API gives you many ways to search for data. In this article I will cover the following:
– network
– host

We are going to start with looking for a network. Say I want to know if we have the network “10.10.0.0/24”. Let’s create a file named “get_network.py” and paste the code below into it:

WAPI Searching for a Network

import requests
import json
requests.packages.urllib3.disable_warnings()
url = "https://gm/wapi/v2.7/network?network=10.10.0.0/24"
gm_user = 'admin'
gm_pwd = 'infoblox'
response = requests.get(url, verify=False, auth=(gm_user, gm_pwd))
networks = json.loads(response.text)
print(networks)
NOTES:
https://gm - should be your Grid Master DNS Name
gm_user = 'admin' - should be your username
gm_pwd = 'infoblox' - should be your user password

The above code is going to use the URI “/network” with an “=” to “10.10.0.0/24”, which is the network we are looking for in Infoblox. (Just in case you are looking for an IPv6 network, you will need to use /ipv6network instead of /network). We are going to take a look at the output:

[{
    '_ref': 'network/ZG5zLm5ldHdvcmskMTAuMTAuMC4wLzI0LzA:10.10.0.0/24/default',
    'network': '10.10.0.0/24',
    'network_view': 'default'
}]

If you look at the above, you are only getting the default objects. “_ref” is one of the most important keys returned, as you need it if you want to “Update” the the object with Comments, EAs, a DHCP Scope, etc. Since, for now, we want to just display the “network” address that we searched for, let’s update “get_network.py” with the following code:

import requests
import json
requests.packages.urllib3.disable_warnings()
url = "https://gm/wapi/v2.7/network?network=10.10.0.0/24"
gm_user = 'admin'
gm_pwd = 'infoblox'
response = requests.get(url, verify=False, auth=(gm_user, gm_pwd))
networks = json.loads(response.text)
print(networks[0]['network'])

The reason for line 14 above (“networks[0][‘network’]), is that the Infoblox WAPI returns an “array”. So, in order to print it the network value, we have to use “networks[0][‘network’]” for the first object in the “networks” array that JSON returns.

python get_networks.py
10.10.0.0/24

WAPI Searching for a Host

Let’s say you know the hostname for an object, but not the IP address. Now we are going to use ‘record:host‘ for this search, which will be very similar to the API call above. Let’s get started.

Host
A host record defines attributes for a node, such as the name-to-address and address-to-name mapping. This alleviates having to specify an A record and a PTR record separately for the same node. A host can also define aliases and DHCP fixed address nodes. The zone must be created first before adding a host record for the zone.

We are going to search for my ‘Grid Master’, which has the ‘host’ name of ‘gm.lab.local’. Let’s follow the code below:

import requests
import json
requests.packages.urllib3.disable_warnings()
url = "https://gm/wapi/v2.7/record:host?name=gm.lab.local"
gm_user = 'admin'
gm_pwd = 'infoblox'
response = requests.get(url, verify=False, auth=(gm_user, gm_pwd))
networks = json.loads(response.text)
print(response.text)

Now let’s take a look at the output:

[
    {
        "_ref": "record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmxvY2FsLmxhYi5nbQ:gm.lab.local/default", 
        "ipv4addrs": [
            {
                "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQubG9jYWwubGFiLmdtLjE5Mi4xNjguMC4yMDAu:192.168.0.200/gm.lab.local/default", 
                "configure_for_dhcp": false, 
                "host": "gm.lab.local", 
                "ipv4addr": "192.168.0.200"
            }
        ], 
        "name": "gm.lab.local", 
        "view": "default"
    }
]

That’s a lot of stuff to process, so let’s break it down. You get back a list with an array of ‘ipv4addrs’, so we can see the ipv4addr associated with the host name. If we just want to print the hostname and IP address, we have to create a foreach loop. Let’s modify ‘get_host.py’ to do just that:

import requests
import json
requests.packages.urllib3.disable_warnings()
url = "https://gm/wapi/v2.7/record:host?name=gm.lab.local"
gm_user = 'admin'
gm_pwd = 'infoblox'
response = requests.get(url, verify=False, auth=(gm_user, gm_pwd))
hosts = json.loads(response.text)
for host in hosts:
    for ip in host['ipv4addrs']:
        print(f"{ip['host']} IP address is {ip['ipv4addr']}")

Here is what the output looks like:

python get_host.py
gm.lab.local IP address is 192.168.0.200

Infoblox Client searching for a “network”

Now we are going to look for the same network as above (10.10.0.0/24) using the Python module “infoblox-client”. We are going to create a new script with the following called “get_network_client.py”:

from infoblox_client import objects
from infoblox_client import connector
import urllib3
urllib3.disable_warnings()
 
opts = {'host': '192.168.0.200', 'username': 'admin', 'password': 'infoblox'}
conn = connector.Connector(opts)
 
networks = conn.get_object('network', {'network': '10.10.0.0/24', 'network_view': 'default'})
print(networks)

Let’s run the above and take a look at the results:

python3 get_network_client.py 
[{'_ref': 'network/ZG5zLm5ldHdvcmskMTAuMTAuMC4wLzI0LzA:10.10.0.0/24/default', 'comment': 'Testing Lab', 'network': '10.10.0.0/24', 'network_view': 'default'}]

Of course, that’s not formatted in a way that’s easy to read, so just like our last blog post, we are going to loop over the information and print out just the network “10.10.0.0/24”
Let’s modify “get_network_client.py” as below, removing the raw “print” statement for the array and adding a “for” loop to print out just the network(s):

from infoblox_client import objects
from infoblox_client import connector
import urllib3
urllib3.disable_warnings()
 
opts = {'host': '192.168.0.200', 'username': 'admin', 'password': 'infoblox'}
conn = connector.Connector(opts)
 
networks = conn.get_object('network', {'network': '10.10.0.0/24', 'network_view': 'default'})
for network in networks:
    print(network['network'])

Let’s take a look at the results:

python3 get_network_client.py 
10.10.0.0/24

As you can see above, we just print out the network, but let’s say we also wanted print the “Network View” as well. To do so, you can simply add “network[‘network_view’]” next to “network[‘network’]” in the print statement within the “for” loop.

Infoblox_client Searching for a Host

For this example, we are going to search for “gm.lab.local” using the infoblox_client module. Just like the WAPI example, let’s create a new file called “get_host_client.py”:

from infoblox_client import objects
from infoblox_client import connector
import urllib3
urllib3.disable_warnings()
 
opts = {'host': '192.168.0.200', 'username': 'admin', 'password': 'infoblox'}
conn = connector.Connector(opts)
 
hosts = conn.get_object('record:host', {'name': 'gm.lab.local'})
print(hosts)

Let’s run the script and look at the output:

python3 get_host_client.py 
[{'_ref': 'record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmxvY2FsLmxhYi5nbQ:gm.lab.local/default', 'ipv4addrs': [{'_ref': 'record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQubG9jYWwubGFiLmdtLjE5Mi4xNjguMC4yMDAu:192.168.0.200/gm.lab.local/default', 'configure_for_dhcp': False, 'host': 'gm.lab.local', 'ipv4addr': '192.168.0.200'}], 'name': 'gm.lab.local', 'view': 'default'}]

Ok, so let’s clean up the output and print just the name and the IP address. We are going to use very similar code to our WAPI example:

from infoblox_client import objects
from infoblox_client import connector
import urllib3
urllib3.disable_warnings()
 
opts = {'host': '192.168.0.200', 'username': 'admin', 'password': 'infoblox'}
conn = connector.Connector(opts)
 
hosts = conn.get_object('record:host', {'name': 'gm.lab.local'})
for host in hosts:
    for ip in host['ipv4addrs']:
        print(f"{ip['host']} IP address is {ip['ipv4addr']}")

WOW! If you think that most of that code looks exactly like the WAPI code, with the “for” loop, that’s because the “infoblox-client” and WAPI calls return the exact same JSON data.

python3 get_host_client.py 
gm.lab.local IP address is 192.168.0.200

Exactly the same output as the WAPI version

Conclusion, infoblox-client or WAPI?

I personally like using WAPI directly, as it’s easier for me to troubleshoot any strange errors that pops up. I do, however, recommend the “infoblox-client” if you are new to Python, or APIs in general.

Let me know if you find this useful with a comment below or have a request for more examples.

Sif Baksh Administrator
Principal Solutions Architect
As Principal Solutions Architect, Sif Baksh is responsible for the design of large-scale Core Services and Security systems. With 25 years of engineering experience in the computer and communications industry, Sif brings a depth of understanding of complex solutions for large and small organizations.
follow me