Hello everyone,
My apologies, I am not a Developer, however I have been placed with a task with creating an automatic way to obtain values found thru the Meraki API.
The main goal is to obtain the "Subnet" for every NetworkId that belongs to VLAN 1, then create a .csv file so I can import it into a monitoring tool called DCRUM.
So I thought I would use PostMan to achieve this. I am able to set the Environmental Variables for the first step.
https://api.meraki.com/api/v0/organizations/{{organizationID}}/networks/
Below is the Test Script I am using, it successfully populates the Environmental Variables (NetworkName, networkNAMEs, NetworkID, and NetworkIDs.
var jsonData = JSON.parse(responseBody);
var schema = {
"id": {
"name" :{
}
}
};
var networkNAMEs = [];
var networkIDs = [];
jsonData.forEach(function(network) {
var testTitle = network.name + " , " + network.id + " conforms to schema";
tests[testTitle] = tv4.validate(network, schema);
networkNAMEs.push(network.name);
networkIDs.push(network.id);
});
postman.setEnvironmentVariable("NetworkName", networkNAMEs.shift());
postman.setEnvironmentVariable("networkNAMEs", JSON.stringify(networkNAMEs));
postman.setEnvironmentVariable("NetworkId", networkIDs.shift());
postman.setEnvironmentVariable("NetworkIDs", JSON.stringify(networkIDs));
The second Request I need to run the following URL in a loop, each time updating the NetworkId variable within the URL with the next NetworkId in the NetworkIDs EnvironmentVariables.
https://api.meraki.com/api/v0/networks/{{NetworkId}}/vlans/1
However, my Test Results all fail with the below error:
FAILStatus code is 200 | AssertionError: expected response to have status code 200 but got 404
The Test Script I am using is:
var networkIDs = [];
networkIDs = JSON.parse(postman.getEnvironmentVariable("NetworkIDs"));
console.log(networkIDs);
for (var i = 0; i < networkIDs.length; i++) {
postman.setEnvironmentVariable("NetworkId", networkIDs[i]);
postman.setNextRequest();
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
}
What I really need to know is, am I going about this the right way? Should I be using a different tool?
If not, the outcome I need is to be able have the Test Results contain the Networkname and Subnet for each iteration and save the results in a .csv, or any file that I can convert later to a .csv.
I hope this is enough details for everyone, please just let me know if I'm just a little too slow to tackle something like this. : )
Thanks for all the help,
Tom
404 usually means the URL is wrong or something is not right with the API key. Perhaps double check the URL you are presenting.
Check out this post using Python.
https://www.reddit.com/r/meraki/comments/65xv9f/python_script_to_get_list_of_vlans/
That is awesome, thanks for the help, much appreciated. I am in a glass case of emotion.
That for sure put me on a better track, that is exactly what I was looking for. I'm using PyCharm now, and of course I still don't know what I'm doing, but slowly learning. : ). I've defined the variables and it looks like I get passed the get_net_ids step, but the script fails at the get_vlan_info step with the error:
Incorrect VLAN Query string
I think it's either not creating the json_list in the get_net_ids def or its not passing the id to the get_vlans_info def. Below is the actual script I am using. (minus the defined variables) (Ugh - it pasted ugly, here's a link as well) https://github.com/portseven/merpy/blob/master/merpy_get_vlans.py
import requests
import json
# Define some variables we need
API_KEY = 'SOMEKEY'
MERAKI_URL = 'https://dashboard.meraki.com/api/v0/'
ORG_KEY = 'SOMEORG'
headers = {'X-Cisco-Meraki-API-Key':API_KEY}
# Function to grab the list of the Networks for the Organisation
def get_net_id():
net_list = requests.get(MERAKI_URL + 'organizations/' + ORG_KEY + '/networks', headers=headers)
if net_list.status_code != 200:
print('Incorrect Network Query String');
exit(1);
else:
json_list = json.loads(net_list.text)
return json_list
# Function to grab the VLAN's in the Network, based on Network ID (passed)
def get_vlan_info(passed_id):
vlan_list = requests.get(MERAKI_URL + 'networks/' + passed_id + '/vlans', headers=headers)
if vlan_list.status_code != 200:
print('Incorrect VLAN Query String');
exit(1);
else:
json_vlist = json.loads(vlan_list.text)
return json_vlist
def main():
# Get list of networks and ID's
net_data = get_net_id()
for i in net_data:
net_name = i["name"]
net_id = i["id"]
# Ignore the MDM Network
if net_name == 'MDM':
continue
else:
# Use the ID to get a list of VLAN's per Network
vlan_data = get_vlan_info(net_id)
for v in vlan_data:
# Extract just the VLAN Name, and Subnet Range
sub_net = v["subnet"]
vlan_name = v["name"]
# Print Results in a CSV Friendly Format
print(net_name + ', ' + vlan_name + ', ' + sub_net)
main()
Do you have VLANing turned on or off on your MX's?
Hello Philip,
Thanks, I do have VLANing turned on, on our MXs.
Thanks,
Tom
You might be on to something, I know that all of the Networks don't belong to VLANS, the first ID that appears in the list when running the URL "https://api.meraki.com/api/v0/organizations/{{organizationID}}/networks/" in Postman does not belong to a VLAN. I know all of the Networks that contain the word "STORE" do belong to VLANS. Do you happen to know how to perform a filter on "name" so it would only return those Networks in the following section?
net_list = requests.get(MERAKI_URL + 'organizations/' + ORG_KEY + '/networks', headers=headers)
if net_list.status_code != 200:
print('Incorrect Network Query String');
exit(1);
else:
json_list = json.loads(net_list.text)
return json_list
Or before the VLAN def runs?
# Function to grab the VLAN's in the Network, based on Network ID (passed)
def get_vlan_info(passed_id):
vlan_list = requests.get(MERAKI_URL + 'networks/' + passed_id + '/vlans', headers=headers)
if vlan_list.status_code != 200:
print('Incorrect VLAN Query String');
exit(1);
Thanks again, I truly truly am very thankful,
Tom
I don't know the answer.
But I suspect for MX's not configured with VLANs enabled you might need to use "Return a single device":
/networks/[networkId]/devices/[serial]
https://dashboard.meraki.com/api_docs#return-a-single-device
So I would probably try and retrieve the VLANs first, and if that fails try the above call. Perhaps you might even get away with just the above API function.
Based on what your code appears to do, after the VLANs are returned in vlan_list, all that is required is a filter function select_vlan (vlan_list, criterion) that returns selected_vlans, being those that contain the word STORE.
Some examples here https://stackoverflow.com/questions/5319922/python-check-if-word-is-in-a-string
If you want to keep it simple just use the in operator, but you might get false positives.
TommyD-
I may be able to help you with a python script. Should only be a little scripting to fit your needs.