#!/usr/bin/env python3 # # meraki-gps takes the current GPS co-ordinates and updates a devices location in the # Meraki dashboard. It was designed so it can be used on a moving device to provide # the dashboard overview page with a constantly updated view of where the device is. # It was designed to run on a Raspberry Pi fitted with a GPS chip. The following link # was used to put this hardware together. # https://www.rs-online.com/designspark/add-gps-time-and-location-to-a-raspberry-pi-project # # The program uses gpsd, so if you have a network based GPS available you could also just # point this script at that server. # # Installation: # meraki-gps uses dotenv to safely store your credentials. Create a file called .meraki.env # in your home directory. For Linux this is typically /home/username. For Windows this # is typically # c:\users\. # Into .meraki.env put this line: # x_cisco_meraki_api_key= # If you don't have an API key yet then follow the instructions on this page: # https://documentation.meraki.com/zGeneral_Administration/Other_Topics/The_Cisco_Meraki_Dashboard_API # # Prior to running this script on a raspberry pi you'll need to run the below commands to # install the extra components required. # sudo apt install python3-pip # pip3 install gpsd-py3 # pip3 install meraki # pip3 install -U python-dotenv # # After you have installed the script I would suggest marking it executable to make running # it simpler. # chmod +x meraki-gps.py # # Usage: # Just run the command to get the command line options. An example usage is: # ./meraki-gps.py -o "Your org name" -n "Your network name" -s device-serial-number # # Once you are happy it works it can be scheduled it to run regularly using cron # to provide a constant GPS fix for moving devices. # import gpsd,os,argparse,meraki from dotenv import load_dotenv load_dotenv() load_dotenv(dotenv_path=os.path.join(os.path.expanduser("~"),".meraki.env")) dashboard = meraki.DashboardAPI( api_key=os.getenv("x_cisco_meraki_api_key"), base_url='https://api-mp.meraki.com/api/v0/', output_log=False, print_console=False ) # Update a devices location in the Meraki Dashboard with the current GPS co-ordinates. def updateDeviceLocation(orgName,netName,serial,lat,long): orgId=None netId=None # Search for the org orgs = dashboard.organizations.getOrganizations() for org in orgs: if org['name'] == orgName: orgId=org['id'] break; if orgId == None: print("Invalid organization name supplied: "+orgName) exit(-1) # Search for the network for net in dashboard.networks.getOrganizationNetworks(orgId): if net['name'] == netName: netId=net['id'] break; if netId == None: print("Invalid network name supplied: "+netName) exit(-1) dashboard.devices.updateNetworkDevice(netId,serial,lat=lat,lng=long) def main(): text=""" meraki-gps.py reads the current GPS position from gpsd and updates a Meraki device with the GPS location.` In your home diretory you should have a .meraki.env file containing x_cisco_meraki_api_key= """ parser = argparse.ArgumentParser(description = text) parser.add_argument("-o", "--orgName", required=True, help="Meraki org name") parser.add_argument("-n", "--netName", required=True, help="Meraki network name") parser.add_argument("-s", "--serial", required=True, help="Meraki device serial number") args=parser.parse_args() if not os.getenv("x_cisco_meraki_api_key"): print("x_cisco_meraki_api_key must be defined in .meraki.env in your home directory or in .env in the current directory") exit(-1) # Grab the current GPS location. If you have a network based gpsd already just # change the below to point to it. gpsd.connect(host="127.0.0.1", port=2947) packet = gpsd.get_current() # If we have a 2D fix or better update the dashboard if packet.mode >= 2: updateDeviceLocation(args.orgName,args.netName,args.serial,packet.lat,packet.lon) main()