Well... this should get you going:
import requests
import random
import string
import smtplib
import ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
api_key = "your_api_key" # enter your meraki api key here
network_id = "L_123456789123456789" # enter the network id of the network you
want to change here
ssid_number = 1 # enter the number of the ssid you want to change here 0 - 14
password_length = 15 # enter the desired length of the new PSK min 8 max 63
character_types = string.ascii_lowercase + string.ascii_uppercase + string.digits # remove or change according to needs
gmail_password = "gmail_app_password" # enter a gmail app password here
sender_email = "jane.doe@gmail.com" # sending gmail address
receiver_email = "john.doe@doeville.com" # receiving address(es)
def random_string(stringlength=password_length):
return ''.join(random.choice(character_types) for i in range(stringlength))
def set_new_psk(new_psk, ssid=ssid_number):
url = "h t t p s://api.meraki.com/api/v0/networks/" + network_id + "/ssids/" + str(ssid) #remove the spaces the I inserted to get around a community bug
payload = "{\r\n \"psk\": \""+str(new_psk)+"\"\r\n}"
headers = {
'Accept': "*/*",
'Content-Type': "application/json",
'cache-control': "no-cache",
'X-Cisco-Meraki-API-Key': api_key
}
response = requests.request("PUT", url, data=payload, headers=headers)
print(str(response.status_code) + " - " + response.text)
return(response.status_code)
def send_password_email(new_psk_param):
message = MIMEMultipart("alternative")
message["Subject"] = "New Wi-Fi PSK"
message["From"] = sender_email
message["To"] = receiver_email
# Create the plain-text and HTML version of your message
text = """\
Hi,
We've changed the Wi-Fi password to: {new_psk}
Kind regards,
pskscript""".format(new_psk=new_psk_param)
html = """\
<html>
<body>
<div>Hi,<br>
<br>
We've changed the Wi-Fi password to: <br>
<h1>{new_psk}</h1>
Kind regards,<br>
pskscript
</div>
</body>
</html>
""".format(new_psk=new_psk_param)
# Turn these into plain/html MIMEText objects
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
# Add HTML/plain-text parts to MIMEMultipart message
# The email client will try to render the last part first
message.attach(part1)
message.attach(part2)
# Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(sender_email, gmail_password)
server.sendmail(
sender_email, receiver_email, message.as_string()
)
new_psk = random_string(password_length)
if set_new_psk(new_psk) == 200:
send_password_email(new_psk)
You could run the script on a schedule.
Note: I'm not a professional programmer, so use at your own risk.
Props to https://realpython.com/python-send-email/ and https://pynative.com/python-generate-random-string/ for some of the source.