Initial commit.
This commit is contained in:
parent
d4c429705d
commit
8f4bf8dcd4
3 changed files with 113 additions and 0 deletions
79
deviceCheck.py
Normal file
79
deviceCheck.py
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#Imports and such.
|
||||
import requests, time, smtplib, os
|
||||
from datetime import datetime, timezone
|
||||
from email.message import EmailMessage
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load.dotenv()
|
||||
|
||||
#Variable declaration.
|
||||
API_KEY = os.getenv("API_KEY")
|
||||
NETWORK_ID = "N_573646002536361150"
|
||||
CLIENT_MAC = "e4:f1:4c:7e:eb:7a"
|
||||
|
||||
#Passes the API key to the header.
|
||||
HEADERS = {
|
||||
"X-Cisco-Meraki-API-Key": API_KEY
|
||||
}
|
||||
|
||||
#Function to get the clients from the specified network.
|
||||
def get_clients():
|
||||
|
||||
#Queries the Meraki API using the network ID, and runs a get command to get a response in .JSON format.
|
||||
url = f"https://api.meraki.com/api/v1/networks/{NETWORK_ID}/clients"
|
||||
response = requests.get(url, headers=HEADERS)
|
||||
|
||||
#Debugging print outs.
|
||||
print("Status:", response.status_code)
|
||||
print("Response:", response.text)
|
||||
|
||||
#Return the resopnse in .JSON form.
|
||||
return response.json()
|
||||
|
||||
#Function to send email using SMTP.
|
||||
def send_email(subject, body, to_email):
|
||||
|
||||
#SMTP credentials.
|
||||
from_email = os.getenv("SMTP_EMAIL")
|
||||
password = os.getenv("SMTP_PASS")
|
||||
smtp_server = os.getenv("SMTP_SERVER")
|
||||
smtp_port = os.getenv("SMTP_PORT")
|
||||
|
||||
#Create email object.
|
||||
msg = EmailMessage()
|
||||
msg["Subject"] = subject
|
||||
msg["From"] = from_email
|
||||
msg["To"] = to_email
|
||||
msg.set_content(body)
|
||||
|
||||
# Connect to specified SMTP server, and then send the email.
|
||||
with smtplib.SMTP_SSL(smtp_server, smtp_port) as smtp:
|
||||
smtp.login(from_email, password)
|
||||
smtp.send_message(msg)
|
||||
|
||||
while True:
|
||||
|
||||
#Constantly update the clients, and get the newest data.
|
||||
clients = get_clients()
|
||||
|
||||
#Search thruogh the client list for the specified MAC address.
|
||||
for client in clients:
|
||||
|
||||
#When it finds the specified MAC address, determine when it was last seen, and also set a variable for the current time.
|
||||
if client["mac"] == CLIENT_MAC:
|
||||
print("Status: " + client["status"])
|
||||
print("IP Address: " + client["ip"])
|
||||
last_seen = datetime.fromisoformat(client["lastSeen"].replace("Z", "+00:00"))
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
#If the client has not been seen for 3 minutes, send an alert email.
|
||||
if (now - last_seen).seconds > 180:
|
||||
print("Sending email alert...")
|
||||
send_email(
|
||||
subject="Meraki Alert",
|
||||
body="Client " + CLIENT_MAC + " is offline!",
|
||||
to_email="tanner@acoservices.com"
|
||||
)
|
||||
|
||||
#Wait a minute before checking again.
|
||||
time.sleep(60)
|
||||
32
getMerakiNetworks.py
Normal file
32
getMerakiNetworks.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import requests, os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
API_KEY = os.getenv("API_KEY")
|
||||
|
||||
HEADERS = {
|
||||
"X-Cisco-Meraki-API-Key": API_KEY,
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
# Step 1: get organizations
|
||||
orgs = requests.get(
|
||||
"https://api.meraki.com/api/v1/organizations",
|
||||
headers=HEADERS
|
||||
).json()
|
||||
|
||||
for org in orgs:
|
||||
print("Org:", org["name"], org["id"])
|
||||
|
||||
# Step 2: get networks in each org
|
||||
nets = requests.get(
|
||||
f"https://api.meraki.com/api/v1/organizations/{org['id']}/networks",
|
||||
headers=HEADERS
|
||||
).json()
|
||||
|
||||
for net in nets:
|
||||
try:
|
||||
print(" Network:", net["name"], net["id"])
|
||||
except Exception as e:
|
||||
print(f"Something went wrong: {e}")
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
python-dotenv
|
||||
requests
|
||||
Loading…
Add table
Reference in a new issue