OpenWrt Forum Archive

Topic: HOW TO: Send WAN IP address by email if that changes

The content of this topic has been archived on 29 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

For semi advanced users. Here's a script that you run as a scheduled task. The script will send an email with the wan ip address if and when it changes.
you will need to enter your email and mail server in the script. Take a look at the script for more info

-----------------------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/sh
sleep 20

    CURR_WAN_IPADDR=`ifconfig eth1 | grep "inet addr" | egrep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -m 1 ".*"`
      
    ##Get last IP and store in file. Where /mnt/sda1 is my mounted USB drive on the router
   
    LAST_WAN_IPADDR=`cat /mnt/sda1/scripts/LAST_WAN_IPADDR.txt`
               
    if [ $CURR_WAN_IPADDR != $LAST_WAN_IPADDR ]; then
    # generate email and send out
                                                                                 
    echo -ne "NEW WAN IP $CURR_WAN_IPADDR \n Previous WAN IP: $LAST_WAN_IPADDR" > /mnt/sda1/scripts/email.txt

    ##Specify outgoing mail SMTP server and also the TO email address. Notice no space between "-ssmtp.server.com"    and mail subject "-f"New_WAN_IP"
    cat /mnt/sda1/scripts/email.txt | /usr/sbin/sendmail  -ssmtp.server.com -f"New_WAN_IP" my.email@domain.com

     rm -f /mnt/sda1/scripts/email.txt
                                                                                                                                                                                                   
    ifconfig eth1 | grep "inet addr" | egrep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -m 1 ".*" > /mnt/sda1/scripts/LAST_WAN_IPADDR.txt
fi

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Doesn't a hotplug event always fire when WAN IP changes?

ubus call network.interface.wan status

Replace ifconfig nowadays.
Well, to get only the the IP is already done:

. /lib/functions/network.sh

if network_get_ipaddr ipw wan; then
    echo "IP: $ipw"
fi

(Last edited by Nilfred on 5 Mar 2015, 04:47)

The discussion might have continued from here.