Last active
November 27, 2019 03:18
-
-
Save yujiterada/7dcdfb70f92534f70cb27e558c935779 to your computer and use it in GitHub Desktop.
Update Meraki PSK periodically with AWS Lambda + CloudWatch Events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
from botocore.vendored import requests | |
from botocore.exceptions import ClientError | |
import json | |
import random | |
import string | |
import os | |
import logging | |
logger = logging.getLogger() | |
# Configure logging level | |
logger.setLevel(logging.INFO) | |
# Configure the length of password | |
K = 8 | |
def get_orgs(): | |
url = "https://dashboard.meraki.com/api/v0/organizations" | |
headers = { | |
'X-Cisco-Meraki-API-Key': os.environ['API_KEY'], | |
} | |
response = requests.request("GET", url, headers=headers) | |
return json.loads(response.text) | |
def get_networks(): | |
url = "https://dashboard.meraki.com/api/v0/organizations/" + os.environ['ORG_ID'] + "/networks" | |
headers = { | |
'X-Cisco-Meraki-API-Key': os.environ['API_KEY'], | |
} | |
response = requests.request("GET", url, headers=headers) | |
return json.loads(response.text) | |
def get_ssids(): | |
url = "https://dashboard.meraki.com/api/v0/networks/" + os.environ['NET_ID'] + "/ssids" | |
headers = { | |
'X-Cisco-Meraki-API-Key': os.environ['API_KEY'], | |
} | |
response = requests.request("GET", url, headers=headers) | |
return json.loads(response.text) | |
def updating_psk_is_successful(psk_pass): | |
url = "https://dashboard.meraki.com/api/v0/networks/" + os.environ['NET_ID'] + "/ssids/" + os.environ['SSID_NUM'] | |
payload = { | |
"enabled": 'true', | |
"authMode": "psk", | |
"encryptionMode": "wpa", | |
"psk": psk_pass, | |
} | |
headers = { | |
'X-Cisco-Meraki-API-Key': os.environ['API_KEY'], | |
} | |
try: | |
response = requests.request("PUT", url, data=payload, headers=headers) | |
logger.info('[update_psk_ssid] ' + str(response.status_code)) | |
except requests.exceptions.ConnectionError as ece: | |
logger.info('[update_psk_ssid] Connection Error:', ece) | |
return False | |
except requests.exceptions.Timeout as et: | |
logger.info('[update_psk_ssid] Timeout Error:', et) | |
return False | |
except requests.exceptions.RequestException as e: | |
logger.info('[update_psk_ssid] Some Ambiguous Exception:', e) | |
return False | |
if response.status_code == 200: | |
return True | |
else: | |
logger.error('[update_psk_ssid] ' + response.text) | |
return False | |
def send_email(message): | |
# Replace [email protected] with your "From" address. | |
# This address must be verified with Amazon SES. | |
SENDER = os.environ['sender'] | |
# Replace [email protected] with a "To" address. If your account | |
# is still in the sandbox, this address must be verified. | |
RECIPIENT = os.environ['recipient'] | |
# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES. | |
AWS_REGION = "us-west-2" | |
# The subject line for the email. | |
SUBJECT = "PSK Update Notification" | |
# The email body for recipients with non-HTML email clients. | |
BODY_TEXT = (message | |
) | |
# The HTML body of the email. | |
BODY_HTML = """<html> | |
<head></head> | |
<body> | |
""" + message + """ | |
</body> | |
</html> | |
""" | |
# The character encoding for the email. | |
CHARSET = "UTF-8" | |
# Create a new SES resource and specify a region. | |
client = boto3.client('ses',region_name=AWS_REGION) | |
# Try to send the email. | |
try: | |
#Provide the contents of the email. | |
response = client.send_email( | |
Destination={ | |
'ToAddresses': [ | |
RECIPIENT, | |
], | |
}, | |
Message={ | |
'Body': { | |
'Html': { | |
'Charset': CHARSET, | |
'Data': BODY_HTML, | |
}, | |
'Text': { | |
'Charset': CHARSET, | |
'Data': BODY_TEXT, | |
}, | |
}, | |
'Subject': { | |
'Charset': CHARSET, | |
'Data': SUBJECT, | |
}, | |
}, | |
Source=SENDER | |
) | |
# Display an error if something goes wrong. | |
except ClientError as e: | |
logging.error('[send_email]' + e.response['Error']['Message']) | |
else: | |
logging.info("[send_email] Email sent! Message ID: " + response['MessageId']) | |
# Uncomment the below to find ORG_ID, NET_ID, and SSID_NUM | |
''' | |
print('### ORGANIZATIONS ###') | |
orgs = get_orgs(API_KEY) | |
for org in orgs: | |
print(org['id'], org['name']) | |
print() | |
print('### NETWORKS ###') | |
nets = get_networks(API_KEY, ORG_ID) | |
for net in nets: | |
print(net['id'], net['name']) | |
print() | |
print('### SSIDs ###') | |
ssids = get_ssids(API_KEY, NET_ID) | |
for ssid in ssids: | |
print(ssid['number'], ssid['name']) | |
print() | |
''' | |
def main(event, context): | |
psk_pass = ''.join(random.choices(string.ascii_uppercase + string.digits, k=K)) | |
if updating_psk_is_successful(psk_pass): | |
message = 'New Password: ' + psk_pass | |
logger.info('[main] ' + message) | |
send_email(message) | |
return('Success') | |
else: | |
return('Error') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment