Python API
For more information about API calls, see the descriptive table of Python API calls here: Tools icon > Network > API Documentation > API List.
Note
When using API calls, replace BASE
in any table entry with the base_uri
value returned by the /api/base_uri
call.
The Python API functions as a wrapper around the API Core. This wrapper binds the XML output to Python objects.
Tip
Infoblox recommends familiarity with the API Core before working with the Python API.
Installing the Python API
The following are required to use the Python API:
- Unix, Linux, or Windows operating system
- Python 2.7or 3.9 for your operating system
Python modules:
- requests => 2.5.2
- setuptools => 17.1
- PyYAML => 5.4.1
To install the Python API, go to NetMRI Tools icon > Network > API Documentation > Python Distribution and download the package in a whl or tar.gz format.
For more instructions about installing the Python packages, visit https://packaging.python.org/tutorials/installing-packages/.
Authentication
When your script connects to NetMRI, it must pass the URL, user name, and password to the client object. The URL and credentials can be passed into the constructor when connecting to NetMRI:
from infoblox_netmri.client import InfobloxNetMRI
defaults = {
"host": "1.2.3.4",
"username": "user",
"password": "password",
}
client = InfobloxNetMRI(
defaults.get("host"),
defaults.get("username"),
defaults.get("password"),
)
Or you can create a .netmri.yml file with authentication information in your home directory.
For example:
# .netmri.yml
---
username: admin
password: secret
Example:
# client = InfobloxNetMRI(netmri_host, username, password)
# username and password are taken from .netmri.yml if exists
from infoblox_netmri.client import InfobloxNetMRI
client = InfobloxNetMRI("1.2.3.4")
The .netmri.yml file should be appropriately protected if you include a password.
Using the Python Objects
All interactions with the NetMRI device are through a client class called InfobloxNetMRI. You must create an instance of this class before interactions are performed.
To open a connection with NetMRI:
defaults = {
"host": "1.2.3.4",
"username": "user",
"password": "password",
}
client = InfobloxNetMRI(
defaults.get("host"),
defaults.get("username"),
defaults.get("password"),
)
There is a broker class for each data type, such as device, interface, and VLAN. This class contains all the routines that query, summarize, or otherwise act on sets of data objects.
ConfigRevision = client.get_broker(“ConfigRevision”)
For instance, the ConfigRevision is a broker that can be used to query for a specific list of configuration files. Files can be searched across all active saved and running configuration files.
Example 1
If you want to list all routing devices on your network, you can search by device type. Querying for devices with types “Router” and “Switch-Router” will list all these devices. To do this, first create a device broker using the InfobloxNetMRI:
broker = client.get_broker(“Device”)
Next, query for devices with device type: Router or Switch-Router:
devices = broker.index(DeviceType=['Router', 'Switch-Router'])
This will make one or more HTTP requests to retrieve all devices from the NetMRI that matches the query. When all records are retrieved, the device’s array contains the complete list of device objects. You can access the fields of the device object using accessor methods. This loop, for example, prints a list of all the devices returned, including devices' name, IP address and device type for each device:
for device in devices:
print("{} {} {}".format(device.DeviceName, device.DeviceIPDotted, device.DeviceType))
Additional documentation
HTML version of this documentation is available on the following page: NetMRI Tools icon > Network > API Documentation > Python API.
Example scripts
Several example scripts are included in the NetMRI Tools icon > Network > API Documentation > Python Distribution.