Skip to content

Instantly share code, notes, and snippets.

@JoshMock
Created October 27, 2014 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshMock/d7f765a2c15b00ad04d4 to your computer and use it in GitHub Desktop.
Save JoshMock/d7f765a2c15b00ad04d4 to your computer and use it in GitHub Desktop.
Get an informative email when your Raspberry Pi starts up
#!/usr/bin/python
# put this in in a file in your Pi's /etc/init.d/ directory
import urllib2
import subprocess
import smtplib
from email.mime.text import MIMEText
import datetime
# Change to your own account information
to = 'joe@email.com'
gmail_user = 'joe@email.com'
gmail_password = 'foobar'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
now = datetime.datetime.now()
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
ext_ip = '???'
try:
ext_ip = urllib2.urlopen('http://icanhazip.com/').read().strip()
except:
pass
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 Raspberry Pi started up with an internal IP address of %s on %s.\n\nIts external IP is %s.' % (ipaddr, now.strftime('%A, %B %d, %Y at %I:%M:%S %p %Z').strip(), ext_ip)
msg = MIMEText(my_ip)
msg['Subject'] = 'Raspberry Pi started up at %s' % now.strftime('%Y-%m-%d %H:%M:%S')
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment