Skip to content

Instantly share code, notes, and snippets.

@ksator
Last active January 22, 2018 18:32
Show Gist options
  • Save ksator/df91a17173dca8e893287620928cfb2f to your computer and use it in GitHub Desktop.
Save ksator/df91a17173dca8e893287620928cfb2f to your computer and use it in GitHub Desktop.
This python scripts make REST calls to Junos space to extract and print the ip addresses of all EX4300-48T
# This python scripts extract and print the ip addresses of all EX4300-48T from junos space
# usage: python junos_space_rest_calls.py
from pprint import pprint as pp
from requests import get
from requests.auth import HTTPBasicAuth
'''
https://www.juniper.net/techpubs/en_US/junos-space-sdk/13.3/apiref/com.juniper.junos_space.sdk.help/JSInventoryManagerSVC/Docs/rest.managed-elements.html
'''
url='https://172.30.109.80'
uri=url + '/api/space/managed-domain/managed-elements'
my_headers= {'Accept': 'application/vnd.net.juniper.space.managed-domain.managed-elements+json;version=1'}
r = get(uri, auth=HTTPBasicAuth('super', 'Juniper123'), verify=False, headers=my_headers)
#r.json()['managed-elements']['managed-element'][-1]
#for item in r.json()['managed-elements']['managed-element']:
# print item[u'ipAddr']
'''
https://www.juniper.net/techpubs/en_US/junos-space-sdk/13.3/apiref/com.juniper.junos_space.sdk.help/JSInventoryManagerSVC/Docs/rest.managed-elements.id.html
'''
my_headers2={'Accept': 'application/vnd.net.juniper.space.managed-domain.managed-element+json;version=1;q=.01'}
for item in r.json()['managed-elements']['managed-element']:
id = int(item[u'@key'])
uri = url + "/api/space/managed-domain/managed-elements/"+str(id)
r2 = get(uri, auth=HTTPBasicAuth('super', 'Juniper123'), verify=False, headers=my_headers2)
if str(r2.json()['managed-element']['device']['platform'])=="EX4300-48T":
print r2.json()['managed-element']['ipAddr']
'''
output is the ip address of EX4300-48T devices in junos space
172.30.108.138
172.30.108.134
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment