Is there any way for my OpenWrt router to send me an email when it detects that my IP address has changed?
Topic: Email when my IP address changes
The content of this topic has been archived on 1 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.
You have two options:
1) Write a cron script around the following:
. /lib/functions/network.sh; network_flush_cache; network_get_ipaddr ip wan; echo $ip
2) Hook into the hotplug events detailed here and use the script above on the bound event for the wan interface:
https://forum.openwrt.org/viewtopic.php … 12#p231212
Option 2 seems more elegant and efficient. Just compare $ip to a file, send the email if the IP has changed, and update the file to the current $ip. You can use mailsend or your preferred smtp client to send the email.
I may go ahead and write the hotplug script myself, because it seems handy to have in the lineup. I'll follow-up if you need help.
(Last edited by fecaleagle on 7 Sep 2015, 03:46)
This seems a lot easier:
https://forum.openwrt.org/viewtopic.php … 26#p232726
Initial tests seem good; just add the following to the end of your /etc/hotplug.d/iface/20-firewall script:
touch /root/lastwanip
lastwanip=`cat /root/lastwanip`
. /lib/functions/network.sh; network_flush_cache; network_get_ipaddr currentwanip wan
if [ $lastwanip != $currentwanip ]; then
mailsend -to <username>@gmail.com -from <username>@gmail.com -starttls -port 587 -auth -smtp smtp.gmail.com -sub "WAN IP Changed" +cc +bc -M "WAN IP changed from $lastwanip to $currentwanip" -v -user <username>@gmail.com -pass <password>
echo $currentwanip > /root/lastwanip
else
mailsend -to <username>@gmail.com -from <username>@gmail.com -starttls -port 587 -auth -smtp smtp.gmail.com -sub "WAN IP Not Changed" +cc +bc -M "WAN IP did not change from $lastwanip" -v -user <username>@gmail.com -pass <password>
fi
Note that I am using mailsend, which is not installed by default. I am also writing to /root/lastwanip; obviously, you can write to anywhere you prefer. The only question remaining is whether the firewall reloads every time the WAN ip changes. I'm taking for granted that it does. The else condition is just for testing purposes.
(Last edited by fecaleagle on 7 Sep 2015, 05:04)
I just saw your other post. Do you mean that you want an email to be sent when your VPN tunnel goes up? Is that where you're going with this? I think you may be failing to distinguish between OpenVPN clients and servers.
I just saw your other post. Do you mean that you want an email to be sent when your VPN tunnel goes up? Is that where you're going with this? I think you may be failing to distinguish between OpenVPN clients and servers.
Sorry for not being specific.
I would like my OpenWRT router to inform me, via email, when my ISP changes my IP address. This has to include times when the router and moden are reset, so it has to survive restarts.
While this fits into my attempts to set up a VPN to the home network, it doesn't directly correlate to it. If I want to wake on lan, remote desktop, or set up a VPN, I need to know if my ISP has changed my IP address.
I'm researching two possibilities: setting up my router to email me when my IP address changes or having my server email me when my IP address changes. I am leaning towards having the router email me, and just having my server email me my server status.
I want to try what you suggested in your second post as soon as I have some time. Thanks for your time, I will get back with you soon.
Ah, I see. The IP assigned by your ISP is generally referred to as the WAN IP. That is what the script I posted seeks to accomplish. The second suggestion in my first reply seems to give finer-grained control over the hooked event. I will attempt to implement that method as well and follow up here.
I generally don't need to do this, because I pay for No-IP DNS service, and I use their daemon on my server to post my IP address to them whenever it changes, but I am interested in determining the best way to accomplish this on my own as well. Unfortunately, my WAN IP has not changed since I implemented the script. I am keeping my eye on lease times and waiting for that e-mail!
Let me know how it goes. It will be a couple more days before I can try anything. I decided to stick with tasking my router to email me when the IP address changes because I am sure if there is a poweroutage that the router and modem will both come back online. I was also going to look into having the router email me monthly with a status update regardless of whether the IP address changes.
When you said you had mailsend installed on OpenWRT, how did you install it. It doesn't appear to be a package available on opkg
Are you on Barrier Breaker? My guess is that it's only available in the Chaos Calmer package feeds.
I don't know how familiar you are with OpenWrt and opkg, but you generally need to run this before attempting to install anything:
opkg update
Just figured I should mention it in case you hadn't done that.
(Last edited by fecaleagle on 8 Sep 2015, 00:03)
There are a number of mail clients available. msmtp and ssmtp are the standard clients, I believe: http://wiki.openwrt.org/doc/howto/smtp.client and http://wiki.openwrt.org/doc/howto/smtps.client.
I actually prefer ssmtp over mailsend, but I was having trouble sending attachments, so I switched to mailsend. ssmtp is probably the way to go, as you can configure your credentials and don't have to specify them in-line.
Edit: Whoops! I was referring to it as sendmail, but it is, in fact, mailsend.
Edit, Edit: By the way, my lease just renewed and I did get an email reporting that my IP did not change. So, it does seem to be the case that the firewall reloads when the lease renews.
You can test by forcing the lease to drop and renew:
killall -SIGUSR2 udhcpc
killall -SIGUSR1 udhcpc
(Last edited by fecaleagle on 8 Sep 2015, 03:40)
I have been extremely busy, and haven't had much time to work on this project. However, recently I tried to flash my DGND3700v1 to Chaos Calmer so I could use mail protocols. When I did, the 2.4 ghz wifi became unreliable and the 5 ghz wouldn't work at all; reference https://forum.openwrt.org/viewtopic.php … 17#p295117
Because of this I had to flash back to Barrier Breaker so that my family and I can use wifi. I want to look into options for going to Chaos Calmer because my wifi doesn't work after restarting the router in BB. I have to go to each wifi options, deselect and reselect "lan" before it will work.
Anyway, I am still working on the project. Thanks for keeping up with me.
The discussion might have continued from here.