First, Please don't laugh at my code, I'm a network engineer, I've pieced this together from various sources. Second, its incomplete. So I know there are things that just don't work. So here is what it does. I have cameras that are in close to 200 different networks. In most cases there is 1 camera per network. For quality\compliance reasons I need to review each cameras placement and status at least once quarterly. I found it some what annoying to tackle this via the dashboard so I decided to figure out how to use the API. Enter my python script below. When run it asks for the API Key and then I lists the available orgs. Eventually I'll have 3 options 1) snap a pic from all cameras, 2) choose the camera to snap a pic from 3) exit. The first option allows me to just let it run and look later, the second option allows me to pull a specific camera if I want to review one, or check placement after adjustments are made, it lists out networks that have cameras Okay - now that we are past what it does, I can say it does work.... so long as the camera is online. If you look at somewhere around line 39 (if copy and paste works) i have the line "snapurl = dashboard.camera.generateDeviceCameraSnapshot(indv_device['serial'])" I need help with some error handling here - if the camera is offline it can't create the snapshot url and throws a 500 error. It still tries because the dashboard knows about the device, but fails. I've tried to use try: with request response but maybe I'm doing something wrong? With the 500 error it just crashes the script with this message - "meraki.exceptions.APIError: camera, generateDeviceCameraSnapshot - 500 Internal Server Error, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm" Does anyone have a tip to error handle a 500 error that maybe just prints "Camera is Unresponsive or Offline" or is there a way to test before the 500 if the device is online? Thanks, import getpass
import requests
import meraki
import time
USER_KEY = getpass.getpass('Enter your Meraki API Value: ')
dashboard = meraki.DashboardAPI(USER_KEY, suppress_logging=True)
available_orgs = dashboard.organizations.getOrganizations()
print("\n---------------------------\nAvailable Organizations\n---------------------------")
for i in range(len(available_orgs)):
option = f"{i}: {available_orgs[i]['name']}"
print(option)
org_selection = input(f"\nSelect an organization [0-{len(available_orgs)-1}]: ")
org = available_orgs[int(org_selection)]
answer = input("Choose what you would like to do:\n 1) Capture snapshot from all camers\n 2) Capture snapshot from a single camera\n 3) Exit\nPlease type 1,2 or 3: ")
answer = '2'
if answer == '1':
print("I don't do this yet")
if answer == '2':
networks = dashboard.organizations.getOrganizationNetworks(org['id'])
print("\n-------------------------------\nAvailable Networks With Cameras\n-------------------------------")
num_networks = len(networks)
for n in range(num_networks):
network = networks[n]
dev_network = dashboard.networks.getNetworkDevices(network['id'])
num_device = len(dev_network)
for s in range(num_device):
indv_device = dev_network[s]
if indv_device['model'] == 'MV12N':
netoption = f"{n}: {networks[n]['name']}"
print(netoption)
net_selection = input(f"\nSelect a Network to capture image from [0-{len(networks)-1}]: ")
camera_net = networks[int(net_selection)]
dev_network = dashboard.networks.getNetworkDevices(camera_net['id'])
num_device = len(dev_network)
for s in range(num_device):
indv_device = dev_network[s]
if indv_device['model'] == 'MV12N':
snapurl = dashboard.camera.generateDeviceCameraSnapshot(indv_device['serial'])
file_name = indv_device['name']
snapshot = snapurl['url']
time.sleep(5)
with open(file_name + '.jpg', 'wb') as handle:
response = requests.get(snapshot, stream=True, headers={'User-Agent': 'Custom'})
if not response =='200':
print("Faild to download automaticly - see if image exists at: ", snapshot)
for chunk in response.iter_content(1024):
if not chunk:
break
handle.write(chunk)
print("Snapshot Saved from ", indv_device['name'])
if answer == '3':
quit
... View more