__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 def send_email(recipient, subject, msg): """Sends an email to the specified email address. Args: recipient: Email address to send to. subject: Email subject. msg: Body of email to send. """ msg = MIMEText(msg) msg['Subject'] = subject msg['To'] = recipient msg['From'] = 'techhelp@mths.ca' try: mail = smtplib.SMTP('localhost', 25) mail.sendmail('techhelp@mths.ca', recipient, msg.as_string()) mail.quit() except: print ("Oops. Something went wrong") 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[1].split() #split_line_b = ip_lines[2].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) #msg = MIMEText(my_ip_a + "\n" + my_ip_b) msg = MIMEText(my_ip_a) # Sends the message send_email('chris.atkinson@ocsb.ca', 'IPs For RaspberryPi', msg.as_string()) ## Configure Postfix as per http://www.richlynch.com/2013/08/04/pi_garage_alert_2/ ### Add this to /etc/rc.local: ## printf "My IP address is %s\n" "$_IP" ## python /home/pi/startup_mailer.py