cisco – Calling up ‘last seen’ time in Meraki API

[ad_1]

I’m writing a script to check if a small device connected to my company’s public wifi is online. But since Meraki’s API doesn’t have a status option, I’m having the script email me an alert if the ‘last seen’ time goes over 5 minutes.

The problem is that I’m a system admin and not a programmer, so API script things are all new to me. (The solution is probably something super simple.)

I have taken parts of other Meraki Python scripts and I’ve had some success with smaller attempts. But now I’m getting the following error:

Traceback (most recent call last):
File “mac_time_test.py”, line 25, in
lastOnline = meraki.getclients(apikey, device[‘lastSeen’], suppressprint=True)
NameError: name ‘device’ is not defined

Except I thought this was the line where ‘device’ was defined? What is another way I can call up the lastSeen value?

This script is based another script I found that states whether a MAC address is on the network. I thought it would work if I took the MAC and paired it with its respective last seen time.

Here’s the current script:

#!/usr/bin/python

import meraki
import json
import subprocess
import datetime

# API key, will soon be changed to an environment variable
apikey = '987xyz987xyz987xyz987xyz987xyz'

# Organization ID
organizationid = '54321'

# MAC address
mac="ab:cd:ef:12:34:56"

#Current time in Unix time format
from datetime import datetime, timezone    
currentTime =int(datetime.now().timestamp() * 1000)

#Network lookup
networks = meraki.getnetworklist(apikey, organizationid, suppressprint=True)

#Last seen lookup  <-- This is the main part in question
lastOnline = meraki.getclients(apikey, device['lastSeen'], suppressprint=True)

#Loop through Network
for row in networks:

        #Device Lookup
        devices = meraki.getnetworkdevices(apikey, row['id'], suppressprint=True)
        #print(format(str(row['id'])))

        #Loop through each device in network
        for device in devices:

            #Client lookup
            clients = meraki.getclients(apikey, device['serial'], suppressprint=True)
            
            #Loop to find client
            for client in clients:
                
                #Condition match statement, change 1000 to better number based on time conversion
                if mac == (client['mac']) and currentTime > lastSeen + 1000:
                    subprocess.run(["sh ~/email_alert.sh"], shell=True)

                    break
                else:
                    continue
                break
            else:
                continue
            break
        else:
            continue
        break

    #Print statement
else:
    print ('Everything is under control. Situation normal.')

[ad_2]

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *