#!/usr/bin/python
import meraki
import json
#
# Python Script Using Meraki API to find last seen time of client device
# Prints out the time last seen
#
# Enter Organization ID Here, to be converted into an environment variable later
apikey = 'abcdeabcdeabcdeabcdeabcdeabcdeabcde'
# Enter Organization ID Here
organizationid = '54321'
# Enter Client MAC Address
mac = 'ab:cd:ef:01:23:45'
#Network lookup
networks = meraki.getnetworklist(apikey, organizationid, suppressprint=True)
#Loop through Network
for row in networks:
#Device Lookup
devices = meraki.getnetworkdevices(apikey, row['id'], suppressprint=True)
#Loop through each device in network
for device in devices:
#Client Lookup
clients = meraki.getclients(apikey, device['serial'], suppressprint=True)
#print(format(str(device['serial'] + '\n')) + (str(device['mac'])))
#Loop to find client
for client in clients:
#LastSeen lookup***This is the main line that is giving me trouble
lastOnline = client['lastSeen']
#print(format(str(client['mac'] + '\n')))
print(format('Last Seen: ') + str(['lastSeen'] + '\n'))
break
else:
continue
break
else:
continue
break
#Print Statement for not found in loop
else:
print ('Made it to the end of the script')
Solved! Go to solution.
Nothing wrong with basic questions!
Pulling the lastSeen time is trivial with the official SDK.
import meraki
dashboard = meraki.DashboardAPI(suppress_logging=True)
clients = dashboard.networks.getNetworkClients(
"network_id_here"
)
for client in clients:
print(f'lastSeen: {client["lastSeen"]}')
print('done')
Based on the names of these methods, it looks like you're using an old version of the SDK.
The current Python library is fetchable via `pip install --update meraki`
Hmmm that's strange, I only installed the packages not three weeks ago. I will check again.
It went from 1.10.0 to 1.12.0, so I guess that's something! Thanks!
This syntax looks like something from a very old version of the SDK, or from API v0 instead of v1.
networks = meraki.getnetworklist(apikey, organizationid, suppressprint=True)
What package exactly is 'import meraki' importing? It seems to me it's not the official SDK.
From the docs: https://developer.cisco.com/meraki/api-v1/#!get-organization-networks
we can find an example of what using the current SDK looks like:
dashboard = meraki.DashboardAPI(API_KEY)
organization_id = '549236'
response = dashboard.organizations.getOrganizationNetworks(
organization_id, total_pages='all'
)
To my knowledge I'm running the most recent versions of all the Meraki packages. The code I posted is adapted from scripts I found online here. My first script that checks if the MAC is on the network runs perfectly. My main issue is calling up the 'lastSeen' element, which really seems to be a syntax error, which is what I'm looking for help on.
Also, 'import meraki' is importing meraki.py from the scripts I linked above.
This is the Python SDK you want. What you're referencing doesn't look familiar and may be part of the problem.
That was the very first thing I installed, using that exact page as a guide.
I get that my questions are super basic and I'm sorry for that, but I really do go through the Meraki API documentation - and have been for a few weeks now. I've come along way, especially for never having done anything like this ever. If reinstalling everything is actually going to help, then I will gladly do that. But I'm a little hesitant, because I'm so close to finishing the one thing I need this for and I'm concerned that this will just be taking two steps back.
Nothing wrong with basic questions!
Pulling the lastSeen time is trivial with the official SDK.
import meraki
dashboard = meraki.DashboardAPI(suppress_logging=True)
clients = dashboard.networks.getNetworkClients(
"network_id_here"
)
for client in clients:
print(f'lastSeen: {client["lastSeen"]}')
print('done')
Thanks! That's it.