Referencing a client's lastSeen time

Solved
lonestar
Comes here often

Referencing a client's lastSeen time

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')

 

1 Accepted Solution
John_on_API
Meraki Employee
Meraki Employee

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')

View solution in original post

9 Replies 9
John_on_API
Meraki Employee
Meraki Employee

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`

lonestar
Comes here often

Hmmm that's strange, I only installed the packages not three weeks ago. I will check again.

lonestar
Comes here often

It went from 1.10.0 to 1.12.0, so I guess that's something! Thanks!

John_on_API
Meraki Employee
Meraki Employee

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'
)

 

lonestar
Comes here often

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.

 

 

John_on_API
Meraki Employee
Meraki Employee

This is the Python SDK you want. What you're referencing doesn't look familiar and may be part of the problem.

 

https://pypi.org/project/meraki/

lonestar
Comes here often

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.

John_on_API
Meraki Employee
Meraki Employee

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')
lonestar
Comes here often

Thanks! That's it.

Get notified when there are additional replies to this discussion.