What's new

Email notification when switching to secondary WAN?

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

stylish_me

Regular Contributor
Hi!
I've been using a script posted here a long ago that sends emails when router reconnects to internet and writes it's current ip in the letter.
Today i connected second internet line as secondary WAN and made all the settings. I was sure that the script will send me different ips - so i will be able to see which line is used (primary or secondary, because primary is unlimited and secondary is $ per MB and i use it only wher primary is down). But unfortunately the script always sends the same ip from primary wan even if secondary wan is connected. Primary wan uses fixed ip and secondary wan uses automatic ip.
As far as i understand - router logs 2 ips: for primary and secondary WAN. So ip in the letter will not help me at all. So i need a script line which indicates, which line is used: primary or secondary.
How can i make this?
Thanx!
 
It helps if you were to post the script.
 
I am interested in this as well, I just don't know how to configure script to report router IP address, if router makes fail-over from PPPoE (VDSL) to 3G/4G Internet

in case your ISP doesn't request SMTP authorization

Code:
#!/bin/sh
SMTP="smtp.yourisp.com"
FROM="router-email@something.com"
FROMNAME="ASUS Router"
TO="your-email@something.com"

echo "Subject: WAN state notification" >/tmp/mail.txt
echo "From: \"$FROMNAME\"<$FROM>" >>/tmp/mail.txt
echo "Date: `date -R`" >>/tmp/mail.txt
echo "ASUS router got connected to the Internet." >>/tmp/mail.txt
echo "New IP is: `nvram get wan0_ipaddr`" >>/tmp/mail.txt

cat /tmp/mail.txt | /usr/sbin/sendmail -S"$SMTP" -f"$FROM" $TO

rm /tmp/mail.txt
 
Last edited:
Have a go at it, tested with wired WAN and USB WAN fail-over.

Download from the website in section Other scripts

How to use this script
1. Change credentials and settings in User settings part
If you want to use your own mail server, change smtp server and port accordingly

2. Place this script in /jffs/scripts/ and name it wan_connect_notification
You should now have this script at this location:
/jffs/scripts/wan_connect_notification

3. Set script executable (0755), enter this in terminal:
Code:
chmod a rx /jffs/scripts/*

4. Place a source link in /jffs/scripts/wan-start to run it whenever the WAN state changes.
Open wan-start and place the following line in the file at the bottom, leave the comment part in (# WAN ...):
Code:
source /jffs/scripts/wan_connect_notification # WAN change notification email

To test settings and function without forcing the WAN type change:
Run this in the terminal:
Code:
/jffs/scripts/wan_connect_notification
 
Last edited:
Have a go at it, tested with wired WAN and USB WAN fail-over.
[/CODE]

Thanx a lot!
Got some questions:
1. Is it a final version or some tests should be done?
2. As far as i remember - gmail needs certificates and so on - so information about gmail settings should be added. Info about gmail certificate is in the link above.
3. Are you sure that nvram get wan_ipaddr will show correct ip adress? The script from the link above always gives the same ip taken from primary wan - even if secondary wan is active.
4. Is it possible to add wan state to email's subject? Smth like "Router name" switched to Primary (Secondary) Wan"
5. May be you know the best way to make ifttt integration for this script to get notifications, not emails? For example some siren when secondary wan is connected and smth calm when primary wan is up.
6. I think people not that familiar with coding (people like me lol) need some information about the terminal. Where should i enter the commands? I installed the previous script many years ago - so i think i don't remember a thing about it.
7. Will it work with Dual wan with wan for primary and lan1 for secondary?

Thank you very much!
 
I did test, but it doesn't work with TLS (SMTP port 587)

I get this error:
Code:
curl: (35) error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
 
Thanx a lot!
Got some questions:
1. Is it a final version or some tests should be done?
2. As far as i remember - gmail needs certificates and so on - so information about gmail settings should be added. Info about gmail certificate is in the link above.
3. Are you sure that nvram get wan_ipaddr will show correct ip adress? The script from the link above always gives the same ip taken from primary wan - even if secondary wan is active.
4. Is it possible to add wan state to email's subject? Smth like "Router name" switched to Primary (Secondary) Wan"
5. May be you know the best way to make ifttt integration for this script to get notifications, not emails? For example some siren when secondary wan is connected and smth calm when primary wan is up.
6. I think people not that familiar with coding (people like me lol) need some information about the terminal. Where should i enter the commands? I installed the previous script many years ago - so i think i don't remember a thing about it.
7. Will it work with Dual wan with wan for primary and lan1 for secondary?

Thank you very much!
I have uploaded version 1.1, with the WAN state in the subject.

My email script does not need the certificate.
I'm sure it reports the correct WAN IP, I tested it with two routers.
You asked for the correct reporting of the IP address with a dual WAN setup. This is it.

And as it contains a version number, this might evolve to more. But at the moment, this does what OP has asked for.
 
I did test, but it doesn't work with TLS (SMTP port 587)

I get this error:
Code:
curl: (35) error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Check the PORT setting, if you use your own server make sure to enter the correct port number.
 
here is TLS version

Code:
#!/bin/sh

# Author: thelonelycoder
# http://www.snbforums.com/members/thelonelycoder.25480/

# Script version: 1.1
# Release date June 19 2017


### How to use this script ###
# 1. Change credentials and settings in the User settings part below

# 2. Place this script in /jffs/scripts and name it wan_connect_notification
# You should now have this script at this location:
# /jffs/scripts/wan_connect_notification

# 3. Set script executable (0755)
# Enter this in terminal, without the leading #:
# chmod a+rx /jffs/scripts/*

# 4. Place a source link in /jffs/scripts/wan-start to run it whenever the WAN state changes
# Open wan-start and place the following line in the file at the bottom, without the leading #
# but leave the comment part in (# WAN ...):
# source /jffs/scripts/wan_connect_notification # WAN change notification email


### User settings, must be set correctly to work ###

# Email settings (mail envelope) #
FROM_ADDRESS="you@gmail.com"
TO_NAME="Your Name"
TO_ADDRESS="you@gmail.com"

# Email credentials #
USERNAME="you@gmail.com"
PASSWORD="gmail-password"

# Server settings #
SMTP="smtp.gmail.com"
PORT="587"

# FROM Name in email
FROM_NAME="Router $(nvram get productid)"

# Wait time for IP assignment in seconds #
# set this higher if it takes longer for the IP assignment after WAN type change
WAIT_SECONDS=10


### Do not change below ###

# set environment PATH to system binaries
export PATH=/sbin:/bin:/usr/sbin:/usr/bin$PATH

# sleep time before sending the notification
sleep $WAIT_SECONDS

# detect WAN type currently in use
if [ $(nvram get link_wan) == 1 ];then
    # WAN is up
    WAN_TYPE="Primary WAN"
elif [ $(nvram get link_wan1) == 1 ];then
    # WAN1 is up
    WAN_TYPE="Secondary WAN"
fi

# assemble the message
echo "From: \"$FROM_NAME\" <$FROM_ADDRESS>" > /tmp/mail.txt
echo "To: \"$TO_NAME\" <$TO_ADDRESS>" >> /tmp/mail.txt
echo "Subject: $(nvram get productid) is now on $WAN_TYPE" >> /tmp/mail.txt
echo "Date: $(date -R)" >>/tmp/mail.txt
echo "" >> /tmp/mail.txt
echo "There's been a WAN type change on the router." >>/tmp/mail.txt
echo "" >> /tmp/mail.txt
echo "Router $(nvram get productid) is now connected through $WAN_TYPE." >>/tmp/mail.txt
echo "The new IP is: $(nvram get wan_ipaddr)" >>/tmp/mail.txt
echo "" >>/tmp/mail.txt
echo "--- " >>/tmp/mail.txt
echo "Your friendly router." >>/tmp/mail.txt

# send with curl
curl --url smtp://$SMTP:$PORT \
  --mail-from "$FROM_ADDRESS" --mail-rcpt "$TO_ADDRESS" \
  --upload-file /tmp/mail.txt \
  --ssl-reqd \
  --user "$USERNAME:$PASSWORD" --insecure

# remove temp file
rm /tmp/mail.txt

# notify Syslog of the event
logger "WAN type change detected, notification sent to $TO_ADDRESS via $0"

#eof

I did test, and I can confirm it works

SSL uses port 465: curl --url smtps://$SMTP:$PORT \
TLS uses port 587: curl --url smtp://$SMTP:$PORT \
 
It's almost working. But i face 2 problems:

1. I get the email:

Router RT-N66U is now connected through Primary WAN.
The new IP is: 192.168.0.111

But 192.168.0.111 - is secondary WAN IP. So this line is wrong.

And more - if i run this script when rebooting router - i get wrong time (1aug 2015, 3;00) and no wan status indication at all.

As for wrong time - i understand that script needs to wait for ntp but the previous script from my link above worked at the same moment router rebooted (even before UI appeared) and time was always correct.

May be it's possible to fix both problems?

As for time problem - my previous script had a line
ntpclient -h pool.ntp.org -s &> /dev/null
So it fixed the time problem. Can i use the same line here?
 
It's almost working. But i face 2 problems:

1. I get the email:

Router RT-N66U is now connected through Primary WAN.
The new IP is: 192.168.0.111

But 192.168.0.111 - is secondary WAN IP. So this line is wrong.

And more - if i run this script when rebooting router - i get wrong time (1aug 2015, 3;00) and no wan status indication at all.

As for wrong time - i understand that script needs to wait for ntp but the previous script from my link above worked at the same moment router rebooted (even before UI appeared) and time was always correct.

May be it's possible to fix both problems?

As for time problem - my previous script had a line
ntpclient -h pool.ntp.org -s &> /dev/null
So it fixed the time problem. Can i use the same line here?
How's your secondary WAN connected to your router?
What firmware version are you running?
 
Primary wan is wan
Secondary wan is Lan Port 1
Firmware is 380.66_4

ntpclient -h pool.ntp.org -s &> /dev/null - didn't help. I still get letters from 2015
Before I dig into it, how do you set LAN port 1 to be wan port? In the WebUI or is that another script you run?
The time is easily fixed.
 
Before I dig into it, how do you set LAN port 1 to be wan port? In the WebUI or is that another script you run?
The time is easily fixed.
Forget the LAN question, I see it now.
Will update the script as soon as time permits.
It will allow to set what secondary WAN connection is used.

It will also have a setting to use either SSL or TLS.
 
Will update the script as soon as time permits.
Oh great thank you very much! Will be waiting for it very much!
Please add force NTP time update on script start - i tried but failed.
As for IP i think it should be smth like is Primary then take ip from XX, if secondary - then take ip from YY.
I used USB modem for secondary wan untill yesterday, but decided to buy Huawei AF23 and connect my usb modem via lan1. Now it switches muuuch faster.
Is it possible to make time force WAN State indication? Because now i am playing with sleep number - even 15 sec is not enough..
 
Oh great thank you very much! Will be waiting for it very much!
Please add force NTP time update on script start - i tried but failed.
As for IP i think it should be smth like is Primary then take ip from XX, if secondary - then take ip from YY.
I used USB modem for secondary wan untill yesterday, but decided to buy Huawei AF23 and connect my usb modem via lan1. Now it switches muuuch faster.
Is it possible to make time force WAN State indication? Because now i am playing with sleep number - even 15 sec is not enough..
No problem I'll get it right now that I understand your predicament...
But first, I have other things to do, maybe in a few hours or tomorrow.
 
Version 1.2 of the WAN connect notification script is now available.
- correctly detects WAN type IP address in use
- updates date and time when rebooting
- smtp encryption (SSL or TLS) can be set in settings

Read the About and How to in the header of the file for explanations.

Downloads in Other Scripts section here.
 
Version 1.2
Thank you very much for the update! Not it works almost perfect.
Unfortunately i noticed that it takes my RT-N66 60 seconds sharp to show WAN state after reboot.
So i had to enter sleep time for the script 60 seconds. It's ok for manual or scheduled reboot, but not ok for switching from primary to secondary when smth happends to primary line.
May be it is possible for version 1.3 to add smth like a watchdog that doesn't wait for sleep timer but sends email as soon as WAN information is availible? For sure it will be availible within 3-4 seconds after switch and within 60 seconds after reboot. Or it is too complicated for such a script?
Thank you very much once more - i am very glad to monitor my connection with the help of your script!
 
May be it is possible for version 1.3 to add smth like a watchdog that doesn't wait for sleep timer but sends email as soon as WAN information is availible?
Sure. This evolved quite nicely...

Version 1.3 of the WAN connect notification script is now available.
- Instead of a wait time, an IP is pinged until successful (Google DNS 8.8.8.8 is the default), can be set to whatever upstream IP you want
- Set PING_TIMEOUT=2 and PING_COUNT=50 to your liking
- Syslog is notified of success or fail if above values are too low.
- If timeout is reached, no email will be sent, only Syslog and terminal output is generated (when the script is run manually)

The PING_TIMEOUT is how log the ping command waits until timeout. Generally, leave this at 2 seconds
The PING_COUNT is how many times the ping command should run, set this higher if the script times out
The total Wait time is PING_TIMEOUT * PING_COUNT: 2 * 50 = 100 seconds for standard setting

Download in Other Scripts section here.
 

Similar threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top