element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • 'Choose another store...'
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs Get an Email from Your Raspberry Pi with its IP Address
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mikedavis
  • Date Created: 5 Jan 2017 4:29 PM Date Created
  • Views 1663 views
  • Likes 6 likes
  • Comments 6 comments
  • tutorial
  • python
  • ipaddress
  • raspberry pi3
  • raspberry pi
  • email
Related
Recommended

Get an Email from Your Raspberry Pi with its IP Address

mikedavis
mikedavis
5 Jan 2017

This came from my a recent post I did on my own blog, but I wanted to share it with this community as well. 

 

I often struggle with finding the IP address of my Raspberry Pi.  I usually need to plug it into a monitor, get its address, and then log in to it through SSH.  It is a hassle, and it is significant enough to delay a lot of my work.

 

Not anymore!

I combined a couple programs that will:

  1. Use a python script to get the IP address of the Raspberry Pi.
  2. Use a python script to send me an email with the IP address in the body.
  3. Do this every time the Raspberry Pi boots up.

There are some pre-requisites for being able to do this.  First you will need to be sure that your Raspberry Pi can send email from the command line.  I have written a tutorial on this, and you will need to do this before attempting this tutorial.

Finding the IP address

If your Raspberry Pi is online, you can find the IP address by typing

ifconfig 

image

This will return a lot of information, including the IP address if you are online through a wired ethernet connection (eth0) or through a wireless connection (wlan0).  As you can see, I have an IP address on the wireless network.  Knowing that IP Address is handy, because it gives me the opportunity to control the Pi from another computer through SSH.

Again, my goal is to find the IP address using a python program.  When I get that as an output, I can embed it in an email.  So I did some searching, and found this program for getting the IP address.

Lets create a file called ipaddress.py.

nano ipaddress.py

Copy and paste the program below into that file.  We will go over what it does below.

import socket
import fcntl
import struct 

def get_ip_address(ifname):
  s
= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
    s
.fileno(),
    0x8915, 
    struct
.pack('256s', ifname[:15])
  )[20:24]) 

address = get_ip_address
('wlan0') #change wlan0 to eth0 if you want the wired IP address
print(address)

This program uses a pre-packaged library (socket) to get the IP address.  When the method is run it returns the IP address of the eth0 or wlan0.  That output is captured in the variable ‘address’ and then printed out.  You should run this to make sure you are getting the IP address you expect.

python ipaddress.py

image

Adding this Address to an Email

Again, you need to be sure your Raspberry Pi can send email using SMTP.  This tutorial will walk you through it.

We are going to add some code to the program above.  Again, this came from a quick Google search with other people having similar problems.  I have used other approaches, but this one worked out the best.

This code will be added to our code from above.

import smtplib 

fromaddr
= 'user_me@gmail.com' #gmail address this is coming from
toaddrs 
= 'user_you@gmail.com' #the address this is going to
msg
= "\r\n".join([  
"From:user_me@gmail.com", #gmail address this is coming from
"To: user_you@gmail.com", #address this is going to
"Subject: The RPi IP Address", #this could be anything
"", "Message text" #body of the message
])
username
= 'user_me@gmail.com' #your gmail login
password
= 'pwd' #the app specific password, not your user password
server
= smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server
.starttls()
server
.login(username,password)
server
.sendmail(fromaddr, toaddrs, msg)
server
.quit()

In this code, python is being used to send an email.  If you save this as its own program, it would be a good test to make sure it works.  Assuming it does, you can combine the two programs.  Open ipaddress.py again.

nano ipaddress.py

And copy the mail program in.  At the end it should look like this

import socket 
import fcntl
import struct
import smtplib 

def get_ip_address(ifname):
  s
= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  return socket.inet_ntoa(fcntl.ioctl(
    s
.fileno(), 0x8915, 
    struct
.pack('256s', ifname[:15])
  )[20:24]) 

address = get_ip_address
('wlan0') #change wlan0 to eth0 if you want the wired IP address #print(address) 

fromaddr = 'user_me@gmail.com' #gmail address this is coming
from
toaddrs  = 'user_you@gmail.com' #the address this is going to
msg
= "\r\n".join([  
"From:user_me@gmail.com", #gmail address this is coming from
"To: user_you@gmail.com", #address this is going to
"Subject: The RPi IP Address", #this could be anything
"",
"The IP Address of the Raspberry Pi is:", #body of the message
address
])

username
= 'user_me@gmail.com' #your gmail login
password
= 'pwd' #the app specific password, not your user password
server
= smtplib.SMTP('smtp.gmail.com:587')
server.ehlo() server.starttls()
server
.login(username,password)
server
.sendmail(fromaddr, toaddrs, msg)
server
.quit()

Here the programs are combined, and I made a couple changes.  I updated the message text to say ‘The IP Address of the Raspberry Pi is:’ and then the address variable gets dumped in on the next line.  If this works, you would get a two line email.

image

Start at Reboot

What really makes this program handy is that it can run whenever the Pi gets turned on.  That way, the Pi would get plugged in, go online, get an IP address and tell me what it is via email.

To do that we will need to set up a cron job, which is something that happens at regular intervals that you can set.

sudo crontab -e

That will open up a document.  At the bottom you can put this command.

@reboot sleep 60 && python /home/pi/ipaddress.py

Close the document.  Reboot.  A little more than one minute after it reboots, you will get an email from the Pi that will give you the IP Address.

NOTE:  One important thing to note is that this will really only work on a network that doesn’t require any special permission to join.  So a Raspberry Pi would need to be able to get on to a network in order to get an IP address and send an email.  I wrote another tutorial about adding wireless networks to your Raspberry Pi through the command line.  That works for things like home networks, or other password protected networks.  You will need to configure the Pi to work on those.

  • Sign in to reply

Top Comments

  • clem57
    clem57 over 6 years ago +1
    Good tutorial and very functional as well. Thanks mikedavis
  • gadget.iom
    gadget.iom over 6 years ago +1
    Neat!
  • DAB
    DAB over 6 years ago +1
    Very cool. I need to make some time to implement this idea. Can you attack files to the email using this technic? DAB
  • mconners
    mconners over 6 years ago in reply to mikedavis

    LOL, I had forgotten about that.

     

     

    attaching files to python email sent from raspberry pi

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mikedavis
    mikedavis over 6 years ago in reply to DAB

    Yes, that should be possible. mconners posted a similar reply with a python example for adding an attachment.  I haven't tried it, but it looks like it should work.  https://docs.python.org/2/library/email-examples.html

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mconners
    mconners over 6 years ago

    That seems like a useful utility. Good job.

     

    Mike

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 6 years ago

    Very cool.

     

    I need to make some time to implement this idea.

     

    Can you attack files to the email using this technic?

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • gadget.iom
    gadget.iom over 6 years ago

    Neat! image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube