• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Maker Hacks

Maker Hacks

Ideas, news & tutorials for makers and hackers – Arduino/Raspberry Pi, 3D printing, robotics, laser cutting, and more

  • Home
  • About
  • YouTube
  • Recommendations
  • Contact

Email Your Raspberry Pi IP Address on Boot

You are here: Home / Hacks, Tips, and Tutorials / Email Your Raspberry Pi IP Address on Boot
FacebookTweetPin
Author: Chris Garrett

pi-ip

A common complaint from Raspberry Pi owners is when running headless (no monitor or keyboard). If the Pi reboots then you lose your login session and often the previous IP address.

Using a something.local hostname sometimes works, but knowing the IP address is often more useful.

Here is a quick Python script to add to your rc.local file so it emails you on bootup.

Code

In this script I am using the Mailgun API so the message is less likely to end up in spam. You get a ton of free emails with Mailgun so it will not cost you a penny.

Add a text file containing your API key to your directory and call it mailgun-api.txt – that way you don’t need to store the key in your script.

import subprocess
import requests
# load API key from file
inFile = open('/home/pi/mailgun-api.txt', 'r')
api_key = inFile.readline().rstrip()
inFile.close()
# get host and IP
this_host = subprocess.check_output("hostname", shell=True).rstrip()
IP_line = subprocess.check_output("hostname -I", shell=True).rstrip()

# send via Mailgun
def send_message():
    return requests.post(
        "https://api.mailgun.net/v3/YOURMAILGUNHOST.mailgun.org/messages",
        auth=("api", api_key),
        data={"from": "Bot <[email protected]>",
              "to": ["[email protected]"],
              "subject": "MY IP: " + IP_line,
              "text": this_host + " = host and " + IP_line + " is my IP!"})
print send_message()

Related

Category: Hacks, Tips, and TutorialsTag: linux, pi, programming, python, raspberry pi
FacebookTweetPin

About Chris Garrett

Marketing Director by day, maker, retro gaming, tabletop war/roleplaying nerd by night. Co-author of the Problogger Book with Darren Rowse. Husband, Dad, 🇨🇦 Canadian.

Check out Retro Game Coders for retro gaming/computing.

☕️ Support Maker Hacks on Ko-Fi and get exclusive content and rewards!

Previous Post:Linux and BASHLinux Automation Tip: Repeat Commands with Loops and Delays
Next Post:Building a Cryptocurrency Mining Rig is Easier than You Might ThinkCryptocurrency Mining Rig

Sidebar

  • Facebook
  • Twitter
  • Instagram
  • YouTube

Recently Popular

  • Gweike Cloud Review
  • How to choose the right 3D printer for you
  • Glowforge Review – Glowforge Laser Engraver Impressions, Plus Glowforge Versus Leading Laser Cutters
  • Original Prusa i3 Mk3S Review
  • Best 3D Printing Facebook Groups
  • Elegoo Mars Review – Review of the Elegoo Mars MSLA Resin 3D Printer
  • Glowforge ‘Pass-Through’ Hack: Tricking the Front Flap of the Glowforge with Magnets to Increase Capacity
  • How to Make a DIY “Internet of Things” Thermometer with ESP8266/Arduino
  • Wanhao Duplicator i3 Review
  • IKEA 3D Printer Enclosure Hack for Wanhao Di3
  • Creality CR-10 3d printer review – Large format, quality output, at a low price!
  • 3D Printed Tardis with Arduino Lights and Sounds
  • Anet A8 Review – Budget ($200 or less!) 3D Printer Kit Review
  • Make your own PEI 3D printer bed and get every print to stick!
  • Upgrading the Wanhao Di3 from Good to Amazing
  • How to Install and Set Up Octopi / Octoprint
  • Creality CR-10 S5 Review

Copyright © 2023 · Maker Hacks · All Rights Reserved · Powered by Mai Theme