Skip to content

Email you IP at start up

Nik Martelaro edited this page Feb 15, 2017 · 1 revision

How to email you Raspberry Pi's IP at startup

If you need to know the IP address of your Raspberry Pi, it can be useful to have a startup script that runs. This is most easily done using a Python script connected to your GMAIL account.

Save the following script in your HOME directory as email_my_ip.py

import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
# Change to your own account information
to = '[email protected]'
gmail_user = '[email protected]'
gmail_password = 'yourpassword'
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]
my_ip = 'Your ip is %s' %  ipaddr
msg = MIMEText(my_ip)
msg['Subject'] = 'IP for your IXE on %s' % today.strftime('%b %d %Y')
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()

You can test this script by running python email_my_ip.py

Getting it to run every time you restart your Pi

To have this script run every time you restart, you can add it to the start-up script called rclocal

Edit rclocal using

sudo nano .rclocal

Then add the following line to the end of the file

python email_my_ip.py

Then save the file.

Reboot and if you put in the correct email info you should see and email from your Pi!

IxE Workshop

For Novices

  1. Unix command review

  2. Hello You Introduction

  3. Log on to your IxE

  4. IxE overview

  5. Run the Hello You example

  6. Understanding Hello You

  7. Logging Out & Shutting Down

Post Novice
0. Logging in as Pi

  1. Preview the larger IxE image
  2. [Project Ideas](Project Ideas)

Also [Related Links](Related Links)

Clone this wiki locally