1

I am fed up with constant IP address changes which prevents me to reach my RPI via SSH. How can I write a script which sends me the current IP address when a change occurs?

ybaylav
  • 13
  • 3

2 Answers2

3

Remember that this question is not specific to the Raspberry Pi. You could find answers on all sorts of general Linux sites.

The following bash script should find your router's IP address.

#!/bin/bash

NOWIP=$(curl ipecho.net/plain 2>/dev/null)
OLDIP=$(cat /tmp/myoldip 2>/dev/null)

if [[ "$NOWIP" = "$OLDIP" ]]; then
   echo "IP address is still $NOWIP"
else
   echo "old was $OLDIP, new is $NOWIP"
   echo $NOWIP >/tmp/myoldip
   # send e-mail here
fi

Put the text in a file called getip and make it executable with chmod +x getip.

The method of sending e-mail will depend on what mail packages you have installed and any mail servers to which you have access.

joan
  • 71,852
  • 5
  • 76
  • 108
2

You should use a dynamic dns client as you can get loads of free domains. The Pi pushes its IP to the DNS Domain every so often and all you have to remember is mydomain.free-dns.com

Here is a tutorial

sudo apt-get install ddclient

edit the configuration file

/etc/ddclient.conf

You can use ddclient on most free dynamic dns's. I like to use no-ip.

In my book I explain how to push dynamic IP's to a real domain, like pi.my.house - I use this technique for loads of people and all i have to remember is. friend.my.house or nan.my.solutions or server.ppumkin.me

Piotr Kula
  • 17,336
  • 6
  • 66
  • 105