Created
January 7, 2019 18:19
-
-
Save JamesOBenson/299f510c765467fe97bd464378887582 to your computer and use it in GitHub Desktop.
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
## For shell only: | |
Using your text editor once again, edit /boot/boot.rc (this assumes you have already renamed this file to boot.rc If not, see RPi_Advanced_Setup). For example: | |
sudo vi /boot/boot.rc | |
Add the following at the end of the file, making changes to the path for your directory tree and save. | |
python /home/pi/Code/RPi_IP_emailer.py | |
## For Rasbian: | |
sudo vi /etc/rc.local | |
# rc.local | |
# | |
# This script is executed at the end of each multiuser runlevel. | |
# Make sure that the script will "exit 0" on success or any other | |
# value on error. | |
# | |
# In order to enable or disable this script just change the execution | |
# bits. | |
# | |
# By default this script does nothing. | |
# Print the IP address if it doesn't work ad sleep 30 before all your code | |
sleep = 10 <<<------------------------------------------ ADD THIS LINE IF DOESN'T WORK IMMEDIATELY!!! | |
_IP=$(hostname -I) || true | |
if [ "$_IP" ]; then | |
printf "My IP address is %s\n" "$_IP" | |
python /home/pi/Code/RPi_IP_emailer.py <<<------------------------------------------ ADD THIS LINE!!! | |
fi | |
exit 0 |
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
__author__ = 'Cody Giles' | |
__license__ = "Creative Commons Attribution-ShareAlike 3.0 Unported License" | |
__version__ = "1.0" | |
__maintainer__ = "Cody Giles" | |
__status__ = "Production" | |
import subprocess | |
import smtplib | |
from email.mime.text import MIMEText | |
import datetime | |
def connect_type(word_list): | |
""" This function takes a list of words, then, depeding which key word, returns the corresponding | |
internet connection type as a string. ie) 'ethernet'. | |
""" | |
if 'wlan0' in word_list or 'wlan1' in word_list: | |
con_type = 'wifi' | |
elif 'eth0' in word_list: | |
con_type = 'ethernet' | |
else: | |
con_type = 'current' | |
return con_type | |
# Change to your own account information | |
# Account Information | |
to = '[email protected]' # Email to send to. | |
gmail_user = '[email protected]' # Email to send from. (MUST BE GMAIL) | |
gmail_password = 'gmailpassword' # Gmail password. | |
smtpserver = smtplib.SMTP('smtp.gmail.com', 587) # Server to use. | |
smtpserver.ehlo() # Says 'hello' to the server | |
smtpserver.starttls() # Start TLS encryption | |
smtpserver.ehlo() | |
smtpserver.login(gmail_user, gmail_password) # Log in to server | |
today = datetime.date.today() # Get current time/date | |
arg='ip route list' # Linux command to retrieve ip addresses. | |
# Runs 'arg' in a 'hidden terminal'. | |
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE) | |
data = p.communicate() # Get data from 'p terminal'. | |
# Split IP text block into three, and divide the two containing IPs into words. | |
ip_lines = data[0].splitlines() | |
split_line_a = ip_lines[0].split() | |
split_line_b = ip_lines[1].split() | |
# con_type variables for the message text. ex) 'ethernet', 'wifi', etc. | |
ip_type_a = connect_type(split_line_a) | |
ip_type_b = connect_type(split_line_b) | |
"""Because the text 'src' is always followed by an ip address, | |
we can use the 'index' function to find 'src' and add one to | |
get the index position of our ip. | |
""" | |
ipaddr_a = split_line_a[split_line_a.index('src')+1] | |
ipaddr_b = split_line_b[split_line_b.index('src')+1] | |
# Creates a sentence for each ip address. | |
my_ip_a = 'Your %s ip is %s' % (ip_type_a, ipaddr_a) | |
my_ip_b = 'Your %s ip is %s' % (ip_type_b, ipaddr_b) | |
# Creates the text, subject, 'from', and 'to' of the message. | |
msg = MIMEText(my_ip_a + "\n" + my_ip_b) | |
msg['Subject'] = 'IPs For RaspberryPi on %s' % today.strftime('%b %d %Y') | |
msg['From'] = gmail_user | |
msg['To'] = to | |
# Sends the message | |
smtpserver.sendmail(gmail_user, [to], msg.as_string()) | |
# Closes the smtp server. | |
smtpserver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment