Network Change & Configuration Management

Reply

Trying to use Python and the REST API to export config files based on GroupMembership and struggling

Authority
Posts: 20
5476     1

This is my first pseudo complex Python script using the NetMRI API. Below is my script I have been running in circles trying to get the output of the running_config_text based on the group membership. I am not even to the point of file output, just trying to get the running_config_text to print so I know that it worked. I am getting the DeviceID and DeviceName back but dying on the configs = c.api section. Here is the Python error.

 

[user1@linux ~]$python -i scripts/python/devices.py

DeviceID       DeviceName
  1 ROUTER-01
devices/running_config_text/?id=1
Traceback (most recent call last):
  File "scripts/python/devices.py", line 53, in <module>
    print configs.keys()
AttributeError: 'NoneType' object has no attribute 'keys'

 

If I remark out the print configs.keys() I get this error:

 

[user1@linux ~]$python -i scripts/python/devices.py

DeviceID       DeviceName
  1 IDC-E-I-A-ASR1002X
devices/running_config_text/?id=1
Traceback (most recent call last):
  File "scripts/python/devices.py", line 52, in <module>
    for r in configs['running_config_text']:
TypeError: 'NoneType' object has no attribute '__getitem__'

 

 

###############################################################################

#!/usr/bin/python

# -*- coding: utf-8 -*-

 

 

import os

import urllib3

import infoblox_netmri

import sys

 

#

# We are using a cert that this box does not know about

# This disable trust warnings

#

urllib3.disable_warnings()

 

 

host = os.environ['NETMRI_HOST']

user = os.environ['NETMRI_USER']

password = os.environ['NETMRI_PASSWORD']

use_ssl = os.environ['NETMRI_USE_SSL']

api_version = os.environ['NETMRI_API_VERSION']

ssl_verify = os.environ.get('NETMRI_SSL_VERIFY')

 

c = infoblox_netmri.InfobloxNetMRI(

    host=host,

    username=user,

    password=password,

    api_version=api_version,

    use_ssl=use_ssl,

    ssl_verify=ssl_verify)

 

devices = c.api_request('devices/index/?GroupID=220&select=DeviceID%2CDeviceName', {'limit': 100})

 

print devices.keys()

 

FORMAT='%3s %16s'

print FORMAT % ('DeviceID', 'DeviceName')

for d in devices['devices']:

   print FORMAT % (d['DeviceID'], d['DeviceName'])

   device_argstr = "devices/running_config_text/?id={}".format(d['DeviceID'])

   print (device_argstr) 

   configs = c.api_request('{}'.format(device_argstr), {'limit': 1})

   print devices.keys()  

   for r in configs['running_config_text']:

       print (r['running_config_text'])

Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg

Adviser
Posts: 357
5476     1
I am not sure why that wouldn’t work, but for performance purposes, you are better off including methods=running_config_text in your /devices/index call. This will call that method on each device and include it in the returned results. It’s a lot faster because it’s done on the server side.

John

Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg

Authority
Posts: 20
5476     1

Thanks for the methods trick it worked like a champ and your are correct it is also very fast.

Highlighted

Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg

Adviser
Posts: 424
5476     1

Do you mind posting the script so others can learn Smiley Happy

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh

Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg

Authority
Posts: 20
5477     1

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#
#

import os
import urllib3
import infoblox_netmri
import sys
#
# We are using a cert that this box does not know about
# This disable trust warnings
#

urllib3.disable_warnings()

host = os.environ['NETMRI_HOST']
user = os.environ['NETMRI_USER']
password = os.environ['NETMRI_PASSWORD']
use_ssl = os.environ['NETMRI_USE_SSL']
api_version = os.environ['NETMRI_API_VERSION']
ssl_verify = os.environ.get('NETMRI_SSL_VERIFY')

c = infoblox_netmri.InfobloxNetMRI(
    host=host,
    username=user,
    password=password,
    api_version=api_version,
    use_ssl=use_ssl,
    ssl_verify=ssl_verify)

devices = c.api_request('devices/index/?GroupID=1&select=DeviceName&methods=running_config_text', {'timeout': 10, 'limit': 3000})

#FORMAT='%16s %80s'
#print FORMAT % ('DeviceName', ' running_config_text')
#
for d in devices['devices']:
 #  print FORMAT % (d['DeviceName'], d['running_config_text'])
   save_path = '/my/custom/path'
   tgt_file = '{}.cfg'.format(d['DeviceName'])
   full_path_file = os.path.join(save_path, tgt_file)
   with open(full_path_file, 'w+') as f1:
     f1.writelines(d['running_config_text'])

Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg

spenumaka Community Manager
Community Manager
Posts: 60
5477     1

We drafted our Infoblox expert, Sif Baksh to talk about using Python for NetMRI in our upcoming webinar - A Python Primer for NetMRI.

 

It is on July 19, 2018, at 10 AM PDT. 

 

Register today at https://infoblox.zoom.us/webinar/register/5615306444782/WN_vn_8OsCwSVKwBXRI6-4LSw

 

Srinivas

--------------------------------------
Check out our new Tech docs website for latest documentation on Infoblox products.

Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg

securitysmith
Techie
Posts: 2
5477     1

is there a recording of this, by chance?

Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg

securitysmith
Techie
Posts: 2
5477     1
Showing results for 
Search instead for 
Do you mean 

Recommended for You