- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It can help you:
https://rowelldionicio.com/listing-meraki-network-devices-using-the-api-devnet/
Please, if this post was useful, leave your kudos and mark it as solved.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks a lot, I will definitely look into that, I glanced at it real quick and I am certain it will help me learn!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much Raphael!!
I will test it soon and will let you know if it worked!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 )
🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Were you able to find a solution to this?