Jump to content
  • Blog

Infoblox NETWORK API examples


Cowboy Denny

8 views

 Share

Here are some examples

Create a network

To create networks, use a POST request:

curl -k1 -u admin:testpw -X POST https://192.168.1.2/wapi/v2.11.2/network \ -d network=10.1.0.0/16

The server returns a reference of the created network:

"network/ZG5zLm5ldHdvcmskMTAuMS4wLjAvMTYvMA:10.1.0.0%2F16"

To create another network, send another POST request:

curl -k1 -u admin:testpw -X POST https://192.168.1.2/wapi/v2.11.2/network \ -d network=10.2.0.0/16

Read a network

To verify that both networks have been created, send a GET request:

curl -k1 -u admin:testpw -X GET https://192.168.1.2/wapi/v2.11.2/network

The server returns a list with both networks:

[
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMS4wLjAvMTYvMA:10.1.0.0%2F16",
        "network": "10.1.0.0/16",
        "network_view": "default"
    },
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMi4wLjAvMTYvMA:10.2.0.0%2F16",
        "network": "10.2.0.0/16",
        "network_view": "default"
    }
]

Note that the returned references could be different in your installation. The sample code uses references returned in the above example. Depending on your installation, make sure that you use the references your server returns.

Modify a network

To modify a network, send a PUT request. Send the following to modify its comment:

curl -k1 -u admin:testpw -X PUT \ https://192.168.1.2/wapi/v2.11.2/network/ZG5zLm5ldHdvcmskMTAuMS4wLjAvMTYvMA:\ 10.1.0.0%2F16 -d comment='Sample comment'

The server still returns the network reference. Note that this could be different from before:

"network/ZG5zLm5ldHdvcmskMTAuMS4wLjAvMTYvMA:10.1.0.0%2F16"

Check that the network was modified, since comment is not a field that is returned by default add _return_fields to the GET request:

curl -k1 -u admin:testpw -X GET https://192.168.1.2/wapi/v2.11.2/network \ -d _return_fields=network,network_view,comment

Note that the 10.1.0.0/16 network has been modified:

[
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMS4wLjAvMTYvMA:10.1.0.0%2F16",
        "comment": "Sample comment",
        "network": "10.1.0.0/16",
        "network_view": "default"
    },
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMi4wLjAvMTYvMA:10.2.0.0%2F16",
        "network": "10.2.0.0/16",
        "network_view": "default"
    }
]

Search for a network

To find networks with comments that contain the word sample in a case-insensitive way:

curl -k1 -u admin:testpw -X GET https://192.168.1.2/wapi/v2.11.2/network \ -d comment~:=sample

The server returns the network we just modified:

[
    {
        "_ref": "network/ZG5zLm5ldHdvcmskMTAuMS4wLjAvMTYvMA:10.1.0.0%2F16",
        "comment": "Sample comment",
        "network": "10.1.0.0/16",
        "network_view": "default"
    }
]

If there is no match, the server returns an empty list:

curl -k1 -u admin:testpw -X GET https://192.168.1.2/wapi/v2.11.2/network \ -d comment~:=nomatch

The server returns the following:

[]

Delete a network

To delete a network, send a DELETE request using a reference you have retrieved by searching. For example, to delete the networks we created above, send the following:

curl -k1 -u admin:testpw -X DELETE \ https://192.168.1.2/wapi/v2.11.2/network/ZG5zLm5ldHdvcmskMTAuMS4wLjAvMTYvMA:\ 10.1.0.0%2F16

The server returns the reference of the object it just deleted, if the deletion was successful:

"network/ZG5zLm5ldHdvcmskMTAuMS4wLjAvMTYvMA:10.1.0.0%2F16"

To delete the other network, send the following:

curl -k1 -u admin:testpw -X DELETE \
    https://192.168.1.2/wapi/v2.11.2/network/ZG5zLm5ldHdvcmskMTAuMi4wLjAvMTYvMA:\
    10.2.0.0%2F16

Note that both networks have been removed:

curl -k1 -u admin:testpw -X GET https://192.168.1.2/wapi/v2.11.2/network

The server returns the following:

[]

 

Here are more examples:

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

1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
[{
    '_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:

1
2
3
4
5
6
7
8
9
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.

1
2
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:

1
2
3
4
5
6
7
8
9
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:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
    {
        "_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:

1
2
3
4
5
6
7
8
9
10
11
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: 

1
2
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”:

1
2
3
4
5
6
7
8
9
10
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:

1
2
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):

1
2
3
4
5
6
7
8
9
10
11
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:

1
2
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”:

1
2
3
4
5
6
7
8
9
10
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:

1
2
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:

1
2
3
4
5
6
7
8
9
10
11
12
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.

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

Exactly the same output as the WAPI version

 Share

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...