IP public address change

Mini-guide - 27/12/2016

An example script sending a notification when the public IP address changes.

BASH script

testonoti="Public ip are changed"

ipvecchio=$(cat /tmp/ip.txt)
ipnuovo=$(curl -s

if [[ "$ipnuovo" != "$ipvecchio" ]]
  echo $ipnuovo > /tmp/ip.txt
  curl --request GET "$token/text/$(echo ${testonoti// /%20})"

It works in a very simple way. The script compares the current public IP address with the one of the previous execution. If the two addresses do not correspond a notification is sent through Telegram with a GET call.
Script variables:

  • $tokenPersonal token assigned during the registration process.
  • $testonoti Notification text .
  • $ipvecchio IP address of the previous execution.
  • $ipnuovo Current IP address.

The call structure HTTP GET is very simple.$token/text/$testonoti

Variable name Description Format
$token Personal token. Alphanumeric string.
$testonoti Notification text. Text without spaces (use %20)

Execute the script every 5 minutes

The script is executed every 5 minutes and if the IP address has changed a notification is sent

You can use cron, a very popular tool to plan operations on linux.
systems Change contab with crontab -e. The syntax of the line to add is the following:

* * * * * /command/to/be/executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

The asterisk (*) operator specifies all possible values for a field. For example, an asterisk in the hour field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month. In this specific example, an asterisk in all fields means run job every minute.
We can edit our crontab with crontab -e and add the line (after last line) about this script.

*/5 * * * * /home/gab/

Where /home/gab/ is the position of the script in the device.

For the execution of the script authorizations are necessary. Authorizations can be given by entering chmod 755

RpiNotify is an Italian project supported by RaspberryItaly

The first Italian RaspberryPi community.