04-14-2022 01:56 PM
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
Solved! Go to Solution.
04-14-2022 03:58 PM
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 !
04-14-2022 03:15 PM
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 !
04-14-2022 03:23 PM
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!
04-14-2022 03:53 PM
It can help you:
https://rowelldionicio.com/listing-meraki-network-devices-using-the-api-devnet/
04-18-2022 07:14 AM
Thanks a lot, I will definitely look into that, I glanced at it real quick and I am certain it will help me learn!!
04-14-2022 03:58 PM
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 !
04-18-2022 07:16 AM
Thank you so much Raphael!!
I will test it soon and will let you know if it worked!!
05-04-2022 05:36 AM
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.
05-04-2022 05:37 AM
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 )
🙂
07-01-2022 04:56 PM
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.
07-20-2022 10:06 AM
Were you able to find a solution to this?
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide