I'm familiar with Meraki but this is my first rodeo for API/Python things, so I imagine the problem is my missing something super simple. I have one script that depends on the last seen time of some wireless clients, and I'm having a difficult time pulling that value. So I made the below script just to print the 'lastSeen' time and I'll adapt it later into the larger script.
Depending on the syntax in the lastSeen lookup below, I either get "KeyError: 'lastSeen'" or "NameError: name 'lastSeen' is not defined".
I know the issue is probably my not being clear enough that I'm trying to pull the last seen time, but I don't know what I'm supposed to write instead. Specifically what am I missing?
Thanks!
#!/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')