Software & AppsOperating SystemLinux

How To Set Up a Server Script to Email You Every Time Your Global IP Changes

Ubuntu 7

In this tutorial, we will guide you through the process of setting up a server script that will email you every time your global IP changes. This can be particularly useful if you’re running a server from a location with a dynamic IP, and you need to keep track of any changes to your IP address.

Quick Answer

Yes, it is possible to set up a server script to email you every time your global IP changes. By creating a bash script that checks for changes in the IP address and using a cron job to run the script periodically, you can receive email notifications whenever your IP address changes. This can be useful for managing servers with dynamic IP addresses and staying updated on any changes that may affect server accessibility.

Prerequisites

Before we begin, you will need:

  • Access to a Linux server, preferably Ubuntu
  • Basic knowledge of Linux command line
  • An email account to receive notifications

Step 1: Creating the Bash Script

Firstly, we need to create a bash script that will check for changes in the global IP address. To do this, open a terminal on your server and create a new file for the script using the command:

nano ip_change_script.sh

This command uses the nano text editor to create a new file named ip_change_script.sh.

Next, copy and paste the following script into the file:

#!/bin/bash

# File to store the current IP address
IP_FILE="/path/to/ip.txt"

# Command to retrieve the current IP address
GET_IP_CMD="dig +short myip.opendns.com @resolver1.opendns.com"

# Retrieve the current IP address
CURRENT_IP=$(eval $GET_IP_CMD)

# Check if the IP file exists
if [ -f $IP_FILE ]; then
 # Read the previous IP address from the file
 PREVIOUS_IP=$(cat $IP_FILE)

 # Compare the current and previous IP addresses
 if [ "$CURRENT_IP" != "$PREVIOUS_IP" ]; then
 # IP has changed, send an email notification
 echo "IP address has changed. New IP: $CURRENT_IP" | mail -s "IP Address Change" your_email@example.com

 # Update the IP file with the new IP address
 echo $CURRENT_IP > $IP_FILE
 fi
else
 # IP file does not exist, create it and store the current IP address
 echo $CURRENT_IP > $IP_FILE
fi

Replace /path/to/ip.txt with the actual path where you want to store the IP address file. Also, replace your_email@example.com with your email address where you want to receive the notifications.

This script works by first checking if a file exists that contains the previous IP address. If the file exists, it reads the IP address from the file and compares it with the current IP address. If the IP addresses are different, it sends an email notification and updates the file with the new IP address. If the file does not exist, it creates the file and stores the current IP address.

Step 2: Making the Script Executable

After saving the file, we need to make the script executable. This can be done using the chmod command:

chmod +x ip_change_script.sh

The +x option makes the file executable.

Step 3: Testing the Script

Before setting up the script to run automatically, it’s a good idea to test it manually:

./ip_change_script.sh

This command will run the script. If the IP address has changed since the last time the script was run, it will send an email notification.

Step 4: Setting Up a Cron Job

To make the script run automatically, we can set up a cron job. Cron is a time-based job scheduler in Unix-like operating systems. To edit the cron file, use the command:

crontab -e

In the cron file, add the following line to run the script every hour:

0 * * * * /path/to/ip_change_script.sh

This line tells cron to run the script at the start of every hour. Save the cron file and exit the text editor.

Conclusion

Now, you have a server script that will automatically check for changes in your global IP address and send you an email notification whenever it changes. This can be a valuable tool for managing servers with dynamic IP addresses, and it can help you stay on top of any changes that might affect your server’s accessibility.

What is a global IP address?

A global IP address, also known as a public IP address, is a unique identifier assigned to a device connected to the internet. It allows other devices on the internet to communicate with that device.

Why would I need to set up a server script to email me when my global IP changes?

If you are running a server from a location with a dynamic IP address, meaning your IP address changes periodically, it can be difficult to keep track of the changes. By setting up a server script to email you when your global IP changes, you can easily stay updated on any changes that might affect your server’s accessibility.

Can I use a different Linux distribution instead of Ubuntu for this tutorial?

Yes, you can use a different Linux distribution as long as it supports bash scripting and has the necessary tools installed (such as dig and mail). However, the commands and file paths used in this tutorial might vary slightly depending on the distribution.

Can I use a different text editor instead of nano to create the bash script?

Yes, you can use a different text editor of your choice to create the bash script. Just replace the nano command with the command for your preferred text editor when creating the script.

How often will the script check for changes in the global IP address?

The script is set to run every hour by default, as specified in the cron job. However, you can modify the cron job to run the script at a different interval if desired.

Where will the script store the previous and current IP addresses?

The script stores the previous and current IP addresses in a file specified by the IP_FILE variable. You can choose the location and name of the file by modifying the IP_FILE variable in the script.

Can I receive email notifications on multiple email addresses?

Yes, you can modify the your_email@example.com part in the script to include multiple email addresses separated by commas. For example, your_email@example.com, another_email@example.com.

How can I stop receiving email notifications?

To stop receiving email notifications, you can either remove the cron job entry from the crontab file or comment it out by adding a # at the beginning of the line. Alternatively, you can modify the script to disable the email sending functionality by removing the lines related to email notifications.

Leave a Comment

Your email address will not be published. Required fields are marked *