jxyblog

jxyblog

email

【Detailed Tutorial】Openwrt Public IP Address Change Automatic Email Notification (Both IPv4 and IPv6) 【Reprinted and Restructured】

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 the IP address
bash: to support running scripts

Go to the desktop web interface of your email provider, find the settings for "POP3/SMTP/IMAP," which will typically indicate your email provider's SMTP server address somewhere on the page, 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 it without typing code.
WinSCP Tool extraction code 9tsm

Next, we will use the WinSCP mentioned above. Open the software, and a new site creation box will pop up.

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, and a box will pop up to check "Save password."
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 contents 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  # Replace with your email's SMTP server address and port if not using 163 email  
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 an email  

If the email is not successfully sent, you can also use the command below to check the log to locate the issue:
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 getting 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 getting 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 run command before exit 0:

Wait 10 seconds after booting to run the script to avoid mistakenly thinking the two IPs are the same and not sending an email or sending an email with a blank IP address because the script runs before the PPPOE dial-up obtains the public IP address.

sleep 10 && /bin/bash /root/ip.sh

5. Set Up a Scheduled Task 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 properly:

/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 run the script with the command:

/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 was executed:

echo "Script executed at $(date)">> /root/check_wan_ip.log

Check the log record:

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 a few seconds after the script runs.
Thus, the OpenWRT system will automatically send notifications when the WAN port IP address changes or when the system starts.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.