- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Get networks by country
Hi all,
I am stuck in a what seems to be a small python script.. I might be just lost as hell hehe.. (not the first time hehe)
But I would like to have a script that do the following:
Find all networks in my org.
That can be done by this "default" python script:
LinkedIn - Twitter
Found this helpful? Give me some Kudos! Much Thanks
Solved! Go to solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ah yes, I was a bit quick in the script. 😛
If you wan't to write the data to a simple text file, it would be something like
#!/usr/bin/env python3
import meraki
def do_stuff(p_id,p_name):
with open("filename.txt","a") as fp:
line = f"id: {p_id} \t name: {p_name}"
fp.writelines(line)
def main():
API_KEY = 'aaa'
dashboard = meraki.DashboardAPI(API_KEY)
organization_id = 'bbb'
sites = dashboard.organizations.getOrganizationNetworks(
organization_id, total_pages='all'
)
for site in sites:
if "DK" in site['name']:
do_stuff(site['id'],site['name'])
if __name__ == "__main__":
main()
If we're talking about CSV data try something like, where you'd like all sites where the name contains "DK", we can do something like;
#!/usr/bin/env python3
import meraki
import csv
def main():
API_KEY = 'aaa'
dashboard = meraki.DashboardAPI(API_KEY)
organization_id = 'bbb'
sites = dashboard.organizations.getOrganizationNetworks(
organization_id, total_pages='all'
)
ListOfDkSites = []
for site in sites:
if "DK" in site['name']:
ListOfDkSites.append([site['id'],site['name']])
header = ["id","name"]
with open("filename.csv","w") as fp:
writer = csv.writer(fp)
writer.writerow(header)
writer.writerows(ListOfDkSites)
if __name__ == "__main__":
main()
Depending on how much data there is, you'd might want to break the CSV writer up in multiple writes, and append the file, rather than writing it all in one go.
I haven't run the code myself, but it's the gist of it.
Like what you see? - Give a Kudo ## Did it answer your question? - Mark it as a Solution 🙂
All code examples are provided as is. Responsibility for Code execution lies solely your own.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unfortunately, there isn't any endpoint from Meraki that will tell you exactly what country a given network is in. You'll have to either
1) Rely on information that is configured on the network it self, or
2) Cross-reference your MX WAN IP with whois tools, like ifconfig.co.
Ad 1) you can use the endpoint that you already are using, and check the timeZone field, or hope there is some country information in the tags and notes fields.
You can also use the Network Devices endpoint, and read the address field, assuming that also is configured.
Ad 2) Using this endpoint, you can cross-reference the WAN1 (or 2) uplink with e.g. ifconfig.co which you can also use to query on the IP address.
Like what you see? - Give a Kudo ## Did it answer your question? - Mark it as a Solution 🙂
All code examples are provided as is. Responsibility for Code execution lies solely your own.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
So I can see my description of the problem is a bit unclear. 🙂
So when I run the default "Get Organization Networks"
import requests
import json
sites = ["L_*********"]
payload = json.dumps({
"mode": "enabled"
})
headers = {
'X-Cisco-Meraki-API-Key': '*********',
'Content-Type': 'application/json'
}
for site in sites:
print (site)
url = "https://api.meraki.com/api/v1/networks/" + site + "/appliance/security/malware"
response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)
There might be a easier way on doing this, and hope you can guide me in some "smarter" direction 🙂
Does this makes sense??
I know that I can take the hole list out of the GET Orginazation function, but if this could be more specific it would be great, but I don´t know if there is a way to filter things here?? (I am NOT an expert in python, still learning)
Thanks for all your guides here.
Frank
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ah, it's just that? Try somthing like this;
#!/usr/bin/env python3
import meraki
def do_stuff(p_id,p_name):
print("id: ",p_id,"\t name: ",p_name)
def main():
API_KEY = 'aaa'
dashboard = meraki.DashboardAPI(API_KEY)
organization_id = 'bbb'
sites = dashboard.organizations.getOrganizationNetworks(
organization_id, total_pages='all'
)
for site in sites:
if "DK" in site['name']:
do_stuff(site[id],site['name'])
if __name__ == "__main__":
main()
Edit: fixed two small typos in the example.
Like what you see? - Give a Kudo ## Did it answer your question? - Mark it as a Solution 🙂
All code examples are provided as is. Responsibility for Code execution lies solely your own.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Rasmus,
When I edit your script and run it in my powershell on windows I am getting this error:
PS C:\Python\Python310\Scripts> python .\GET-Org-DK.py
2022-09-09 08:12:35 meraki: INFO > Meraki dashboard API session initialized with these parameters: {'version': '1.24.0', 'api_key': '************************************fb59', 'base_url': 'https://api.meraki.com/api/v1', 'single_request_timeout': 60, 'certificate_path': '', 'requests_proxy': '', 'wait_on_rate_limit': True, 'nginx_429_retry_wait_time': 60, 'action_batch_retry_wait_time': 60, 'retry_4xx_error': False, 'retry_4xx_error_wait_time': 60, 'maximum_retries': 2, 'simulate': False, 'be_geo_id': None, 'caller': None, 'use_iterator_for_get_pages': False}
2022-09-09 08:12:35 meraki: INFO > GET https://api.meraki.com/api/v1/organizations/*********/networks
2022-09-09 08:12:35 meraki: INFO > organizations, getOrganizationNetworks; page 1 - 200 OK
Traceback (most recent call last):
File "C:\Python\Python310\Scripts\GET-Org-DK.py", line 22, in <module>
main()
File "C:\Python\Python310\Scripts\GET-Org-DK.py", line 19, in main
do_stuff(site[id],site['name'])
KeyError: <built-in function id>
PS C:\Python\Python310\Scripts>
Something in line 19 and 22.
I am guessing your have made your script to run on mac? 🙂 But it should be the same for windows right?^:)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
From looking at the example script, change...
do_stuff(site[id],site['name'])
...to...
do_stuff(site['id'],site['name'])
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Nice that worked. 🙂
If I want the output to be put into a file what then? 🙂
Thanks for all your help on this. 🙂 (I need to learn more Python... any good links that I should study??)
Frank
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There are loads of websites, try...
https://www.python.org/about/gettingstarted/
https://www.w3schools.com/python/default.asp
Aside from just redirecting the script's output, you could open/create a file and write direct to it, for instance...
https://www.w3schools.com/python/python_file_write.asp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ah yes, I was a bit quick in the script. 😛
If you wan't to write the data to a simple text file, it would be something like
#!/usr/bin/env python3
import meraki
def do_stuff(p_id,p_name):
with open("filename.txt","a") as fp:
line = f"id: {p_id} \t name: {p_name}"
fp.writelines(line)
def main():
API_KEY = 'aaa'
dashboard = meraki.DashboardAPI(API_KEY)
organization_id = 'bbb'
sites = dashboard.organizations.getOrganizationNetworks(
organization_id, total_pages='all'
)
for site in sites:
if "DK" in site['name']:
do_stuff(site['id'],site['name'])
if __name__ == "__main__":
main()
If we're talking about CSV data try something like, where you'd like all sites where the name contains "DK", we can do something like;
#!/usr/bin/env python3
import meraki
import csv
def main():
API_KEY = 'aaa'
dashboard = meraki.DashboardAPI(API_KEY)
organization_id = 'bbb'
sites = dashboard.organizations.getOrganizationNetworks(
organization_id, total_pages='all'
)
ListOfDkSites = []
for site in sites:
if "DK" in site['name']:
ListOfDkSites.append([site['id'],site['name']])
header = ["id","name"]
with open("filename.csv","w") as fp:
writer = csv.writer(fp)
writer.writerow(header)
writer.writerows(ListOfDkSites)
if __name__ == "__main__":
main()
Depending on how much data there is, you'd might want to break the CSV writer up in multiple writes, and append the file, rather than writing it all in one go.
I haven't run the code myself, but it's the gist of it.
Like what you see? - Give a Kudo ## Did it answer your question? - Mark it as a Solution 🙂
All code examples are provided as is. Responsibility for Code execution lies solely your own.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Option 2 works for a csv file, but not the first one. 🙂
I can do it when I run the command with a > command:
python .\GET-Org-NL.py > Get-Org-NL.txt fx.
When running option 1 I get this error:
PS C:\Python\Python310\Scripts> python .\GET-Org-DK.py
File "C:\Python\Python310\Scripts\GET-Org-DK.py", line 5
line = f"id: {p_id} \t name: {p_name}"
But I can fix this with the above command 🙂
I will mark your idea / solution here Rasmus
Thanks to all, here for your help. 🙂
