Skip to content

Instantly share code, notes, and snippets.

@pato
Created February 6, 2014 06:31
Show Gist options
  • Save pato/8839267 to your computer and use it in GitHub Desktop.
Save pato/8839267 to your computer and use it in GitHub Desktop.
Python script to email Raspberry Pi on boot
'''
Rasberry Pi IP Emailer
Save this script somewhere
Add the following to /etc/rc.local (if using Raspbian)
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
python /LOCATION_OF_SCRIPT/ipmail.py
fi
'''
import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
to = ''
gmail_user = ''
gmail_password = ''
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
arg='ip route list'
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data = p.communicate()
split_data = data[0].split()
ipaddr = split_data[split_data.index('src')+1]
my_ip = 'Your ip is %s' % ipaddr
msg = MIMEText(my_ip)
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()
@djiangnz
Copy link

    String storedEmail = sp.getString("fromEmail", "dianyijnz@gmail.com");
    String storedPasswd = sp.getString("fromPasswd", "iooacphplseogzly");
    String storedHose = sp.getString("fromHost", "");
    String storedPort = sp.getString("fromPort", "");
    String storedTo1 = sp.getString("to1", "lastobject@gmail.com");
    String storedTo2 = sp.getString("to2", "lastobject@gmail.com");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment