Original article address:
【Detailed Tutorial】OpenWrt Public IP Address Change Automatic Email Notification (Both IPv4 and IPv6 are supported)
(Source: Enshan Wireless Forum)
1. Preparation Work
Ensure that OpenWRT has the following packages installed:
msmtp: for sending emails
curl: for obtaining IP addresses
bash: to support running scripts
Go to the web interface of your email client, find the settings for "POP3/SMTP/IMAP", and this page will usually indicate the SMTP server address of your email provider somewhere, for example, for 163 email it is smtp.163.com, which you will need later.
(Optional step: On the "POP3/SMTP/IMAP" settings page, enable the SMTP service. After enabling, you will receive an authorization password; copy and save this password as you will need it later.)
2. Configure msmtp to Send Emails
Create or edit the msmtp configuration file; you can directly use the WinSCP tool to edit without typing code.
WinSCP Tool extraction code 9tsm
Next, we will need to use the WinSCP mentioned above. Open the software, and it will pop up a new site creation box.
File protocol: scp
Hostname: 192.168.2.1 Port: 22 # Router management address
Username: root Password: 123456 # Your router's account and password
Then click save, check the box to save the password in the pop-up box.
After that, just click login, and you will be able to see all the files in the router.
Use the tool to open the /etc/ directory, right-click to edit msmtprc.
Taking 163 email as an example, if you have not configured /etc/msmtprc before, you can delete all the content inside and enter the following:
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
account default
host smtp.163.com # If not using 163 email, replace with your email's SMTP server address and port
port 465 # The SMTP server address for 163 email is: smtp.163.com, port is: 465
from [email protected]
user [email protected]
password **************** # Here fill in your email password or SMTP authorization password
tls_starttls off
echo "Subject: OpenWRT" | msmtp -a default [email protected] # Test email sending function, you can use this command to test if you can successfully send emails
If the email is not successfully sent, you can also use the command below to check the log to locate the problem:
cat /var/log/msmtp.log
3. Write a Script to Detect WAN IP Address Changes
Create an IP detection script, create the script /root/ip.sh in the /root directory, which will be used to check the WAN IP address and send an email when it changes:
Copy the following code into the script file:
#!/bin/bash
current_ipv4=$(ip -4 addr show devpppoe-wan | grep -oE 'inet [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | awk '{print $2}') # Get the current WAN IPv4 address; if you only need to get the WAN port's IPv4 address, delete the related command for obtaining the WAN port's IPv6 address
current_ipv6=$(ip -6 addr show devpppoe-wan | grep -oE 'inet6 [0-9a-fA-F:]+(/[0-9]+)?' | awk '{print $2}') # Get the current WAN IPv6 address; if you only need to get the WAN port's IPv6 address, delete the related command for obtaining the WAN port's IPv4 address
if [ ! -f /root/wan_ip.txt ]; then
echo "$current_ipv4 $current_ipv6" > /root/wan_ip.txt # Check if the wan_ip.txt file exists; if not, create the file and send the initial email
echo -e "Subject: OpenWRT WAN IP\n\nFirst run of the script, WAN IPv4 address is: $current_ipv4\nWAN IPv6 address is: $current_ipv6" | msmtp -a default [email protected] # Send initial email notification
fi
saved_ip=$(cat /root/wan_ip.txt) # Read the saved IP address
if [ "$current_ipv4 $current_ipv6" != "$saved_ip" ]; then # If the current IPv4 or IPv6 address is inconsistent with the saved one, send an email notification and update the record
echo -e "Subject: OpenWRT WAN IP Changed\n\nWAN IPv4 has changed to: $current_ipv4\nWAN IPv6 has changed to: $current_ipv6\nPrevious IP address was: $saved_ip" | msmtp -a default [email protected]
echo "$current_ipv4 $current_ipv6" > /root/wan_ip.txt # Update IP record
fi
Set the script's execution permissions: chmod +x /root/ip.sh
Here we directly use the software operation, right-click to change permissions and check all three Xs on the right.
4. Configure the Script to Run Automatically at System Startup
Edit the /etc/rc.local file to make the system execute this script at startup or reboot:
Open /etc/rc.local with the software and right-click to edit.
Add the script running command before exit 0:
Wait 10 seconds after booting to run the script to avoid mistakenly believing that the two IPs are the same and not sending emails or sending emails with blank IP addresses because the script runs before PPPOE dials to obtain the public IP address.
sleep 10 && /bin/bash /root/ip.sh
5. Set Up a Cron Job to Check IP Changes
Edit the cron configuration, use crontab to set a scheduled task to check the IP address every 5 minutes:
Open the router - system - scheduled tasks.
Add the following line to run the script every 5 minutes:
*/5 * * * * /bin/bash /root/ip.sh
6. Manually Run the Script for Testing
Run the script manually with the following command to ensure it works correctly:
/bin/bash /root/ip.sh
If this is not the first time running the script but you want to test if the script runs normally, directly running the script will not receive any emails. Follow the next steps, first delete /root/wan_ip.txt:
rm /root/ip.txt
Then use the command to run the script:
/bin/bash /root/ip.sh
You can also add logging in the script or check the execution status of the script after the router starts. For example, add the following content in the script (do not add it between "if" and "fi") to log whether the script is executed:
echo "Script executed at $(date)">> /root/check_wan_ip.log
Check the log records:
cat /root/check_wan_ip.log
If everything is configured smoothly following this tutorial, you should receive an email notification with the current WAN port IP address shortly after the script runs.
Thus, the OpenWRT system will automatically send notifications when the WAN port IP address changes or when the system starts.