- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
Trying to use Python and the REST API to export config files based on GroupMembership and struggling
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
10-11-2017 09:21 PM
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
10-12-2017 06:42 AM
John
Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
10-12-2017 07:48 AM
Thanks for the methods trick it worked like a champ and your are correct it is also very fast.
Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
10-12-2017 07:49 AM
Do you mind posting the script so others can learn
Twitter: https://twitter.com/sifbaksh
https://sifbaksh.com
Re: Trying to use Python and the REST API to export config files based on GroupMembership and strugg
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
10-12-2017 07:53 AM
#!/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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
07-03-2018 12:05 PM
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
06-12-2019 12:33 PM
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
06-12-2019 12:35 PM
..as I answer my own question...
https://community.infoblox.com/t5/Tech-Videos/A-Python-Primer-for-NetMRI-Webinar-Archive/ba-p/13956