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.
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.
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:
This command uses the
nano text editor to create a new file named
Next, copy and paste the following script into the file:
# File to store the current IP address
# Command to retrieve the current IP address
GET_IP_CMD="dig +short myip.opendns.com @resolver1.opendns.com"
# Retrieve the current IP address
# Check if the IP file exists
if [ -f $IP_FILE ]; then
# Read the previous IP address from the 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" email@example.com
# Update the IP file with the new IP address
echo $CURRENT_IP > $IP_FILE
# IP file does not exist, create it and store the current IP address
echo $CURRENT_IP > $IP_FILE
/path/to/ip.txt with the actual path where you want to store the IP address file. Also, replace
firstname.lastname@example.org 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 +x ip_change_script.sh
+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:
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:
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.
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.
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.
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.
Yes, you can use a different Linux distribution as long as it supports bash scripting and has the necessary tools installed (such as
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.
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.
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.
Yes, you can modify the
email@example.com part in the script to include multiple email addresses separated by commas. For example,
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.