Startup Mailer

by .

I needed a way to get the IP address of my Raspberry when it connects to the internet with a dynamic IP. Found this script from the good people at eLinux.org.

Copy script and edit /etc/rc.local to call it on boot

pi@raspberrycam2$ sudo pico /home/pi/code/startup_mailer.py

Copy and paste startup_mailer.py (see below) into pico

pi@raspberrycam2$ sudo chmod +x /home/pi/code/startup_mailer.py

pi@raspberrycam2$ sudo pico /etc/rc.local

/etc/rc.local should look like this

 # 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
 _IP=$(hostname -I) || true
 if [ "$_IP" ]; then
   printf "My IP address is %s\n" "$_IP"
   python /home/pi/code/startup_mailer.py
 fi
 exit 0

Once this is set up you can call startup_mailer.py with

python ./code/startup_mailer.py 'this_will_apear_in_email_subject'

Here is startup_mailer.py

import subprocess
import smtplib
import socket
import os
from email.mime.text import MIMEText
import datetime
import sys

eventText = ''
if len( sys.argv ) > 1:
    eventText = sys.argv[1]
# Change to your own account information
to = 'email to send to'
gmail_user = 'email to send from'
gmail_password = 'password to send from'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)

#a linux command line call
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]
routerAddress = split_data[split_data.index('via')+1]

#eventText is parameter to this function
mail_body = 'raspberrycam2 %s:\n' % eventText
mail_body += 'IP: %s\n' % ipaddr
mail_body += 'router: %s' % routerAddress

today = datetime.date.today()
timeStr = today.strftime('%b %d %Y')

mail_subject = 'raspberrycam1 pi@' + ipaddr + ' ' + eventText + ' on %s' % timeStr

msg = MIMEText(mail_body)
msg['Subject'] = mail_subject
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()

Linux Commands

pi@raspberrycam2$ ip route list

default via 192.168.1.1 dev wlan0 
192.168.1.0/24 dev wlan0  proto kernel  scope link  src 192.168.1.70

Take what we get from ‘ip route list’ and add this to startup_mailer.py.

#a linux command line call
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]
routerAddress = split_data[split_data.index('via')+1]

#eventText is parameter to this function
mail_body = 'raspberrycam2 %s:\n' % eventText
mail_body += 'IP: %s\n' % ipaddr
mail_body += 'router: %s' % routerAddress

 

Leave a Reply

Your email address will not be published. Required fields are marked *