Created
October 27, 2014 15:21
-
-
Save JoshMock/d7f765a2c15b00ad04d4 to your computer and use it in GitHub Desktop.
Get an informative email when your Raspberry Pi starts up
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
#!/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 = '[email protected]' | |
gmail_user = '[email protected]' | |
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