WireGuard IP Change Notication Script

Below is a script that I use to notify via e-mail and update my WireGuard enabled devices of any IP changes. FTR I used an AI agent to build. If you have better ideas, would be interested in hearing.

================================================================================

IPFIRE: EMAIL NOTIFICATION ON WAN IP CHANGE

STEP 1: CONFIGURING THE IPFIRE MAIL SERVICE

  1. Log into your IPFire Web User Interface (WUI).
  2. Navigate to System > Mail Service.
  3. Configure your upstream SMTP server details, username, password, and recipient.
  4. Click Save, then click “Send test mail” to verify it works.

STEP 2: DEPLOYING THE MONITORING SCRIPT

  1. Log into IPFire via SSH as root and create the tracking directory:
    mkdir -p /var/tmp/mail

  2. Create and open the script file using nano:
    EDITOR=nano nano /var/tmp/mail/check_wan_ip.sh

  3. Paste the following script into the file (be sure to replace the placeholder
    emails at the bottom with your actual addresses):

#!/bin/bash

Detect current RED interface IP

CURRENT_IP=$(/sbin/ip address show dev red0 | /bin/grep inet | /usr/bin/awk ‘{print $2}’ | /usr/bin/cut -d’/’ -f1)

if [ -z “$CURRENT_IP” ]; then
CURRENT_IP=$(/usr/bin/curl -s https://ifconfig.me)
fi

Tracking file must reside in a globally accessible directory

IP_FILE=“/var/tmp/mail/ip_red_alt”

if [ ! -f “$IP_FILE” ]; then
echo “$CURRENT_IP” > “$IP_FILE”
chmod 666 “$IP_FILE”
exit 0
fi

OLD_IP=$(cat “$IP_FILE”)

if [ “$CURRENT_IP” != “$OLD_IP” ] && [ ! -z “$CURRENT_IP” ]; then
echo “$CURRENT_IP” > “$IP_FILE”

# !!! CONFIGURATION SECTION !!!
TO="your-recipient-email@domain.com"
FROM="your-configured-email@domain.com"

(
  echo "Subject: IPFire Alert: WAN IP Changed"
  echo "From: $FROM"
  echo "To: $TO"
  echo ""
  echo "The IPFire RED WAN IP address has changed."
  echo "Old IP: $OLD_IP"
  echo "New IP: $CURRENT_IP"
  echo "Timestamp: $(date)"
) | /usr/sbin/sendmail -t -f "$FROM"

fi

  1. Save and exit (Ctrl+O, Enter, then Ctrl+X).
  2. Make the script executable:
    chmod 755 /var/tmp/mail/check_wan_ip.sh

STEP 3: CONFIGURING THE CRON JOB AUTOMATION

  1. Open the crontab configuration using nano:
    EDITOR=nano fcrontab -e

  2. Scroll to the very bottom line and add the 5-minute interval trigger:
    */5 * * * * /var/tmp/mail/check_wan_ip.sh

  3. Save and exit (Ctrl+O, Enter, then Ctrl+X).

  4. Verify the cron job is active by running:
    fcrontab -l

STEP 4: TESTING THE BACKEND PIPELINE

Run these commands to simulate an IP change and force an alert:

  1. Create a fake old IP file:
    echo “1.2.3.4” > /var/tmp/mail/ip_red_alt && chmod 666 /var/tmp/mail/ip_red_alt

  2. Run the script manually:
    /var/tmp/mail/check_wan_ip.sh

  3. Confirm you received the email.

  4. Verify the file updated to your real IP:
    cat /var/tmp/mail/ip_red_alt