Get notified of your Raspberry Pi’s IP address

I often find that if I connect my Raspberry Pi to a new network, it can be difficult to connect to it via SSH. When it connects, it should register its name on the network, so you should be able to just run ssh pi@raspberrypi, assuming your username is ‘pi’ and your Pi’s hostname is ‘raspberrypi’. But for me, for whatever reason, that name registration rarely works on my network, so I need to work out the device’s IP address in order to login to it.

Wouldn’t it be great if, as soon as your Pi is connected to the network, it could send you an email with its local IP address, so that you can instead connect by running a command like ssh pi@192.168.1.90? DNS doesn’t need to work, and this technique will send you an email whenever the Pi’s IP address changes.

This method requires knowledge of Amazon Web Services, and assumes you already have the AWS command line interface set up on the Pi. If you’re unfamiliar with AWS, you might still find this post somewhat useful, but you’ll need to substitute the part of my code that sends the notification.

AWS setup

  • Create an SNS topic to publish the notifications to
  • Subscribe your email address to the topic. You’ll receive an email with a link to click before the subscription takes effect
  • Make sure the Pi has permission to publish notifications to that topic

On the Pi

To set this up, you’ll already need access to the Pi’s terminal. A way to do this might be to plug in a monitor and keyboard.

Add some code

This script looks at your local IP addresses (v4 only), and gets your external IP address using Amazon’s checkip service. It compares those addresses against those stored in a file /tmp/ip.txt and, if there’s a difference between the previous time it was run, it will send a notification on Amazon SNS.

Put this wherever you want, but a good example might be ~/bin/ipnotify.sh

This file requires an environment variable to be set called TOPIC_ARN. We’ll get to that in the next step.

The handy thing about having ip.txt live in the /tmp directory is that the file gets deleted when the Pi turns off, so this will always trigger a notification the first time it is run.

Systemd timers

To make the code run once a minute,we’re going to create a systemd service and timer.

/etc/systemd/system/ipnotify.service

[Unit]
Description=Notify on SNS when IP address changes

[Service]
Type=oneshot
Environment="TOPIC_ARN=arn:aws:sns:eu-west-2:123456789:my-topic"
ExecStart=/bin/bash /home/pi/bin/ipnotify.sh
User=pi

Remember to change the Topic ARN to the one that matches your SNS topic, and the ExecStart script path to wherever you put that script from the above step.

You’ll want to set the User to ‘pi’ so that the script runs as the ‘pi’ user, otherwise you’ll need to configure the AWS CLI to run as root, and that’s probably not the right thing to do.

/etc/systemd/system/ipnotify.timer

[Unit]
Description=Notification timer

[Timer]
OnBootSec=1min
OnUnitActiveSec=1min

[Install]
WantedBy=timers.target

And this is the bit that makes it run every minute. OnBootSec starts the script 1 minute after booting up, and OnInitActiveSec repeats the script every minute.

To enable this timer so that it runs when you next boot up the Pi, run sudo systemctl daemon-reload followed by sudo systemctl enable ipnotify.timer. If you want to start that timer right now without restarting the Pi, also run sudo systemctl start ipnotify.timer.

Connect the Pi to a new network

If your Raspberry Pi has an Ethernet port, use an Ethernet cable to plug it into your router. Job done.

Setting up WiFi on your Pi is a little more complicated, but there are some handy instructions on the Raspberry Pi website.

A really handy thing I’ve done is configure the Pi to use my phone’s Internet connection using tethering. I edited the /etc/wpa_supplicant/wpa_supplicant.conf file to contain multiple networks like this:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
  ssid="my-home-network"
  psk="hunter2"
  key_mgmt=WPA-PSK
  priority=1
}

network={
  ssid="Dan's phone"
  psk="rosebud"
  key_mgmt=WPA-PSK
  priority=10
}

The priority field is a little confusing. If my home WiFi and phone’s tethering connection are both visible to the Pi, it will prefer the network with the largest value for priority, so in this case the Pi will prefer to connect to my phone.

Tethering

It turns out, yes you can SSH from your laptop to your Raspberry Pi using your phone’s tethering connection.

When you enable tethering on your phone then power on your Pi, the Pi should connect to your phone then send a notification to AWS. With your email address subscribed to that AWS topic, and with your laptop also connected to your phone, you should receive an email from the Pi.

Now that you know the Pi’s IP address, you can SSH into it using ssh pi@192.168.0.33.

Hope this helps someone!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s