Created
November 3, 2019 07:29
-
-
Save afaqk9394/04f3d1d0a7ec4e363f1d64b4b09f094c to your computer and use it in GitHub Desktop.
Gather existing inventory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import requests | |
| api_key = 'e1cf05af36ef9fccf579a556537112fe66a60549' | |
| org_id = '641762946900403561' | |
| def get_net_name(network_id, networks): | |
| return [element for element in networks if network_id == element['id']][0]['name'] | |
| session = requests.session() | |
| headers = {'X-Cisco-Meraki-API-Key': api_key, 'Content-Type': 'application/json'} | |
| name = json.loads(session.get('https://api.meraki.com/api/v0/organizations/' + org_id, headers=headers).text)['name'] | |
| mynetworks = json.loads(session.get('https://api.meraki.com/api/v0/organizations/' + org_id + '/networks', headers=headers).text) | |
| myinventory = json.loads(session.get('https://api.meraki.com/api/v0/organizations/' + org_id + '/inventory', headers=headers).text) | |
| myappliances = [device for device in myinventory if device['model'][:2] in ('MX', 'Z1', 'Z3', 'vM') and device['networkId'] is not None] | |
| mydevices = [device for device in myinventory if device not in myappliances and device['networkId'] is not None] | |
| # Print all appliances within each Network | |
| for appliance in myappliances: | |
| network_name = get_net_name(appliance['networkId'], mynetworks) | |
| print('Network: ' + network_name) | |
| device_name = json.loads(session.get('https://api.meraki.com/api/v0/networks/' + appliance['networkId'] + '/devices/' + appliance['serial'], headers=headers).text)['name'] | |
| print('Appliance Name: ' + device_name) | |
| # Print all devices within each Network | |
| for device in mydevices: | |
| print('Network: ' + network_name) | |
| network_name = get_net_name(device['networkId'], mynetworks) | |
| device_name = json.loads(session.get('https://api.meraki.com/api/v0/networks/' + device['networkId'] + '/devices/' + device['serial'], headers=headers).text)['name'] | |
| print('Device Name: ' + device_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment