Created
November 15, 2018 19:49
-
-
Save puccaso/bad9e50ceb922d77059717bf3b798dff to your computer and use it in GitHub Desktop.
A python script that emails the local and external IP address when the Pi starts up. Original version from: http://elinux.org/RPi_Email_IP_On_Boot_Debian
This file contains 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/env python | |
# modified from http://elinux.org/RPi_Email_IP_On_Boot_Debian | |
import subprocess | |
import smtplib | |
import socket | |
from email.mime.text import MIMEText | |
import datetime | |
import urllib2 | |
# Change to your own account information | |
to = 'user@gmail.com' | |
gmail_user = 'user@gmail.com' | |
gmail_password = 'password' | |
smtpserver = smtplib.SMTP('smtp.gmail.com', 587) | |
smtpserver.ehlo() | |
smtpserver.starttls() | |
smtpserver.ehlo | |
smtpserver.login(gmail_user, gmail_password) | |
today = datetime.date.today() | |
# Very Linux Specific | |
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] | |
extipaddr = urllib2.urlopen("http://icanhazip.com").read() | |
my_ip = 'Local address: %s\nExternal address: %s' % (ipaddr, extipaddr) | |
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment