Script to get subnets from organization

MarcP
Kind of a big deal

Script to get subnets from organization

Hey all,

has anyone of you got a script which is usefull to get a List of all Subnets within an organization?

 

Found this, but I don´t know anything about it and can´t say if it is helpfull or not. :S

https://www.reddit.com/r/meraki/comments/65xv9f/python_script_to_get_list_of_vlans/

12 REPLIES 12
MarcP
Kind of a big deal

Found this finally... does it seem to be workable? Don´t have a testlab at the moment and don´t want to use the real org...

 

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()
Uberseehandel
Kind of a big deal

I find the listing of VLANs on the dashboard to be "fine as far as it goes".

 

When checking the overall VLAN schema, it would be preferable if VPN VLANs were include from the point of view of completeness. Some of the VLANs have a /28 address as opposed to /24, it would be nice to see if the IP addresses beyond the /28 network range are available for VLAN allocation.

 

The above script produces a much more useful listing..

Robin St.Clair | Principal, Caithness Analytics | @uberseehandel
MarcP
Kind of a big deal


@Uberseehandel wrote:

I find the listing of VLANs on the dashboard to be "fine as far as it goes".

What you mean?

I would like to get a complete list of all VLANs within the org. Best would be xls/csv but .txt is fine so far too.

 

I made it now by using the S2S VPN Tab, and there is every vlan listed, but still would like to have it more professional 😄

Uberseehandel
Kind of a big deal

The listing only shows the "normal" VLANs, as you have observed, it does not contain the VPN VLANs and the LANs, which are required to be able to see at a glance what IP address ranges are available for sub-netting.

 

Have you tried using XlsxWriter to create Excel files using python? I have no idea how good it is.

Robin St.Clair | Principal, Caithness Analytics | @uberseehandel

It'll work, but there's a problem with that script that won't allow it to be universal for everyone. The author is using a specific naming convention on their networks and unless you're following that it'll make a call against non-MX networks.

 

Here, in the main() function it goes:

 

# 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:

It's looking for "MDM" in the name of the network. A better way to do this IMO is to check the network type.

 

If type != appliance or if type != combined

    continue

 

Of course you could hit a combined network without an MX...  But there seems to be enough error checking in the requests return status that it should be OK either way way you go. 

 

It doesn't make any write calls so it shouldn't be able to do any harm. 

 

MarcP
Kind of a big deal

"ignore the MDM network" 

 

So i understand it like it is checking every device and lists it, and only ignores the MDM network (?)

 

I hate this python stuff 😄 Untill someday I´ll learn it, then for sure I´ll love it 😛

SoCalRacer
Kind of a big deal

Meraki Tools for Google Sheets add-on 

 

Might give this a look, I haven't checked all the functions, but it sounds like it would work.

MarcP
Kind of a big deal


@SoCalRacer wrote:

Meraki Tools for Google Sheets add-on 

 

Might give this a look, I haven't checked all the functions, but it sounds like it would work.


Seems like it is not activated anymore. Can´t open it.

SoCalRacer
Kind of a big deal

If you follow that thread, sounds like there have been some issues with the repo recently

Uberseehandel
Kind of a big deal


@MarcP wrote:

"ignore the MDM network" 

 

 

I hate this python stuff 😄 Untill someday I´ll learn it, then for sure I´ll love it 😛


Fortunately, one can do great stuff with Node.js and NodeRed - plenty of stuff on the Cisco DevNet site. Python is the work of the devil, it encourages errors.

Robin St.Clair | Principal, Caithness Analytics | @uberseehandel
jdsilva
Kind of a big deal


@MarcP wrote:

"ignore the MDM network" 

 

So i understand it like it is checking every device and lists it, and only ignores the MDM network (?)

 


Close. It's checking every network, and ignores that network if the name of the network contains "MDM". 

 

MarcP
Kind of a big deal

Thanks to everyone, I´ll get a real testlab now and try the above script.

 

@jdsilva Network name will have no MDM in it Haha 😄

 

Also made a wish for the overview page so they may add a column with "configured VLAN(´s) / Subnets). Think this would be helpfull for many people.

Get notified when there are additional replies to this discussion.
Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.
Labels