Python script to get every devices Hostname *.dynamic-m.com

Solved
GH3
Here to help

Python script to get every devices Hostname *.dynamic-m.com

Hey there,

I have barely no experience with Python and I am looking for a script that would allow me to get every devices Hostname *.dynamic-m.com from every single device in my Meraki network.

Does anybody have something handy to share please?

Thank you

 

1 Accepted Solution
RaphaelL
Kind of a big deal
Kind of a big deal

import json
import requests
import codecs

requests.packages.urllib3.disable_warnings()

base_url_v1 = 'https://api.meraki.com/api/v1'

apikey = "XXXXXXXXXX" #YOUR_API_KEY
orgid = "XXXXXXXXXXX" #YOUR_ORG_ID
headers = {
	'x-cisco-meraki-api-key': format(str(apikey)),
	'Content-Type': 'application/json'
}

def __returnhandler(statuscode, returntext):
	if str(statuscode) == '200':
		return returntext
	else:
		print('HTTP Status Code: {0}\n'.format(statuscode))
def getorgdevices():
	results = []
	geturl = '{0}/organizations/{1}/devices?model=MX'.format(str(base_url_v1), str(orgid))
	dashboard = requests.get(geturl, headers=headers,verify=False)
	if dashboard.status_code == 200:
		raw = dashboard.json()
		for i in raw:  
			results.append(i)
		while 'next' in dashboard.links :
			dashboard = requests.get(dashboard.links['next']['url'],headers=headers,verify=False)
			raw = dashboard.json()
			for i in raw:  
				results.append(i)
	return (results)  
def getManagementInterface(serialnum):
	geturl = '{0}/devices/{1}/managementInterface'.format(str(base_url_v1), str(serialnum))
	dashboard = requests.get(geturl, headers=headers,verify=False)
	result = __returnhandler(dashboard.status_code, dashboard.text)
	return result
	
devices = getorgdevices()
f = codecs.open("DDNS.txt", "a","utf-8")
f.write("DeviceName,activeDdnsHostname,ddnsHostnameWan1,ddnsHostnameWan2\n")
f.close()

for device in devices:
	ManagementInterface = json.loads(getManagementInterface(device['serial']))
	ActiveDDNSHostname = ManagementInterface['ddnsHostnames']['activeDdnsHostname']
	DDNSHostnameWan1 = ManagementInterface['ddnsHostnames']['ddnsHostnameWan1']
	DDNSHostnameWan2 = ManagementInterface['ddnsHostnames']['ddnsHostnameWan2']
	f = codecs.open("DDNS.txt", "a","utf-8")
	message = "{},{},{},{}\n".format(device['name'],ActiveDDNSHostname,DDNSHostnameWan1,DDNSHostnameWan2)
	f.write(message)
	f.close()

 

It is not the prettiest code ! but I was able to get that running in under 5 minutes and I have get going for the night. 

 

I will create a textfile named DDNS.txt which you can then import into excel as a CSV with a ',' delimiter. 

 

I tried it on 60 MX and it was giving me the right info. You will need to supply your API key and your ORG ID. 

 

Have fun !

View solution in original post

10 Replies 10
RaphaelL
Kind of a big deal
Kind of a big deal

That would be easy ! 

 

Loop through all your devices that are MXs : /organizations/{organizationId}/devices ( ref : https://developer.cisco.com/meraki/api-v1/#!get-organization-devices 

     Then Retrieve the management interfaces for each device ( includes DDNS infos ) : /devices/{serial}/managementInterface   (ref : https://developer.cisco.com/meraki/api-v1/#!get-device-management-interface ) 

 

I can write the script for you if you want ! 

GH3
Here to help

Hey Raphael!

If you could write the script it would be so appreciated and I'm sure it will help a lot of people, I've been struggling to find time for months to learn how to develop on Python but it's not my core thing I do not develop.

Thanks a lot in advance!

alemabrahao
Kind of a big deal
Kind of a big deal

It can help you:

 

https://rowelldionicio.com/listing-meraki-network-devices-using-the-api-devnet/

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.
GH3
Here to help

Thanks a lot, I will definitely look into that, I glanced at it real quick and I am certain it will help me learn!!

RaphaelL
Kind of a big deal
Kind of a big deal

import json
import requests
import codecs

requests.packages.urllib3.disable_warnings()

base_url_v1 = 'https://api.meraki.com/api/v1'

apikey = "XXXXXXXXXX" #YOUR_API_KEY
orgid = "XXXXXXXXXXX" #YOUR_ORG_ID
headers = {
	'x-cisco-meraki-api-key': format(str(apikey)),
	'Content-Type': 'application/json'
}

def __returnhandler(statuscode, returntext):
	if str(statuscode) == '200':
		return returntext
	else:
		print('HTTP Status Code: {0}\n'.format(statuscode))
def getorgdevices():
	results = []
	geturl = '{0}/organizations/{1}/devices?model=MX'.format(str(base_url_v1), str(orgid))
	dashboard = requests.get(geturl, headers=headers,verify=False)
	if dashboard.status_code == 200:
		raw = dashboard.json()
		for i in raw:  
			results.append(i)
		while 'next' in dashboard.links :
			dashboard = requests.get(dashboard.links['next']['url'],headers=headers,verify=False)
			raw = dashboard.json()
			for i in raw:  
				results.append(i)
	return (results)  
def getManagementInterface(serialnum):
	geturl = '{0}/devices/{1}/managementInterface'.format(str(base_url_v1), str(serialnum))
	dashboard = requests.get(geturl, headers=headers,verify=False)
	result = __returnhandler(dashboard.status_code, dashboard.text)
	return result
	
devices = getorgdevices()
f = codecs.open("DDNS.txt", "a","utf-8")
f.write("DeviceName,activeDdnsHostname,ddnsHostnameWan1,ddnsHostnameWan2\n")
f.close()

for device in devices:
	ManagementInterface = json.loads(getManagementInterface(device['serial']))
	ActiveDDNSHostname = ManagementInterface['ddnsHostnames']['activeDdnsHostname']
	DDNSHostnameWan1 = ManagementInterface['ddnsHostnames']['ddnsHostnameWan1']
	DDNSHostnameWan2 = ManagementInterface['ddnsHostnames']['ddnsHostnameWan2']
	f = codecs.open("DDNS.txt", "a","utf-8")
	message = "{},{},{},{}\n".format(device['name'],ActiveDDNSHostname,DDNSHostnameWan1,DDNSHostnameWan2)
	f.write(message)
	f.close()

 

It is not the prettiest code ! but I was able to get that running in under 5 minutes and I have get going for the night. 

 

I will create a textfile named DDNS.txt which you can then import into excel as a CSV with a ',' delimiter. 

 

I tried it on 60 MX and it was giving me the right info. You will need to supply your API key and your ORG ID. 

 

Have fun !

GH3
Here to help

Thank you so much Raphael!!

I will test it soon and will let you know if it worked!!

GH3
Here to help

I tried the script yesterday, it works like a charm!! I had a hard time finding the ORG ID but it was so simple, it was at the very bottom of the Meraki Dashboard page lol.

 

Thanks again for your tremendous help!

G.

RaphaelL
Kind of a big deal
Kind of a big deal

My pleasure !  


Yes it can be found at the bottom of your dashboard or via the API endpoint : getOrganizations ( https://developer.cisco.com/meraki/api-latest/#!get-organizations )  

 

🙂 

mvalpreda
Getting noticed

I tried using this and out of my 115 devices, it only output 5 of them to the ddns.txt file. Run it again, and I get a different set of values.

ZaidAmla
New here

Were you able to find a solution to this?

 

Get notified when there are additional replies to this discussion.