What's new

Email notification on new connection

  • 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!

coloxim

Regular Contributor
Hi,

Newbie...
I follow this tutorial for email notification,



but it doesn't work..
I've to put the txt did il /it's/script?

Where have i to write the script? There is an addon also?

Thanks
 
Here is my wan-event script. It emails me when a connected state happens. Th smail script is another general purpose script I use to call when ever I need something mailed to me.

EDIT: The spoiler code really looks bad, changed to use just code instead


Code:
#!/bin/sh

# variables passed to wan-event
# $1 > Wan Adaptor
# $2 > Wan state
#        init
#        connecting
#        connected
#        disconnected
#        stopped
#        disabled
#        stopping

if ! [ -d "/jffs/wan" ]; then
    mkdir /jffs/wan
fi

case $2 in
    "disconnected")
        echo "" > /jffs/wan/disconnected.txt
        echo "Wan$1 Port reported disconnected on $(date)" >> /jffs/wan/disconnected.txt
        echo "" >> /jffs/wan/disconnected.txt
    ;;
    "connected")

        sleep 5

        T1="$(nvram get ntp_server0)"
        T2="$(nvram get ntp_server1)"

        C1="True"
        C2=0

        while [ "$C1" = "True" ]
        do
            if [ "$(nvram get ntp_ready)" = "1" ]; then

                IP=$(nvram get wan${1}_ipaddr)
                PUBLIC=$(wget -qO - http://ipecho.net/plain | xargs echo)

                if [ -e "/jffs/wan/disconnected.txt" ]; then
                    cat /jffs/wan/disconnected.txt > /jffs/wan/connected.txt
                    rm /jffs/wan/disconnected.txt
                else
                    echo "" > /jffs/wan/connected.txt
                    echo "There is no record of the last time WAN port become disconnected" >> /jffs/wan/connected.txt
                    echo "" >> /jffs/wan/connected.txt
                fi

                echo "Wan$1 Port reported connected at $(date)" >> /jffs/wan/connected.txt
                echo "" >> /jffs/wan/connected.txt
                echo "The Wan$1 Address is: $IP" >> /jffs/wan/connected.txt
                echo "The Public Address is: $PUBLIC" >> /jffs/wan/connected.txt
                echo "" >> /jffs/wan/connected.txt
                echo "Waited for $C2 loops for time to be synced" >> /jffs/wan/connected.txt
                echo "" >> /jffs/wan/connected.txt

                /jffs/addons/young/smail.sh /jffs/wan/connected.txt "AC86U Router Connected to the Internet"
                C1="False"
            else
                sleep 3
                let "C2=C2+1"

                if [ "$C2" -gt 10 ]; then
                    C1="False"
                    logger "wan-event - wan event script did not start - ntp server failed to respond"
                fi
            fi
        done

        # Start SoftEtherVPN Server
#        /jffs/addons/young/start_vpn_server

        /jffs/addons/young/wakedc.sh
       
        exit 0
    ;;
    esac
 
Last edited:
Here is my wan-event script. It emails me when a connected state happens. Th smail script is another general purpose script I use to call when ever I need something mailed to me.

#!/bin/sh

# variables passed to wan-event
# $1 > Wan Adaptor
# $2 > Wan state
# init
# connecting
# connected
# disconnected
# stopped
# disabled
# stopping

if ! [ -d "/jffs/wan" ]; then
mkdir /jffs/wan
fi

case $2 in
"disconnected")
echo "" > /jffs/wan/disconnected.txt
echo "Wan$1 Port reported disconnected on $(date)" >> /jffs/wan/disconnected.txt
echo "" >> /jffs/wan/disconnected.txt
;;
"connected")

sleep 5

T1="$(nvram get ntp_server0)"
T2="$(nvram get ntp_server1)"

C1="True"
C2=0

while [ "$C1" = "True" ]
do
if [ "$(nvram get ntp_ready)" = "1" ]; then

IP=$(nvram get wan${1}_ipaddr)
PUBLIC=$(wget -qO - http://ipecho.net/plain | xargs echo)

if [ -e "/jffs/wan/disconnected.txt" ]; then
cat /jffs/wan/disconnected.txt > /jffs/wan/connected.txt
rm /jffs/wan/disconnected.txt
else
echo "" > /jffs/wan/connected.txt
echo "There is no record of the last time WAN port become disconnected" >> /jffs/wan/connected.txt
echo "" >> /jffs/wan/connected.txt
fi

echo "Wan$1 Port reported connected at $(date)" >> /jffs/wan/connected.txt
echo "" >> /jffs/wan/connected.txt
echo "The Wan$1 Address is: $IP" >> /jffs/wan/connected.txt
echo "The Public Address is: $PUBLIC" >> /jffs/wan/connected.txt
echo "" >> /jffs/wan/connected.txt
echo "Waited for $C2 loops for time to be synced" >> /jffs/wan/connected.txt
echo "" >> /jffs/wan/connected.txt

/jffs/addons/young/smail.sh /jffs/wan/connected.txt "AC86U Router Connected to the Internet"
C1="False"
else
sleep 3
let "C2=C2+1"

if [ "$C2" -gt 10 ]; then
C1="False"
logger "wan-event - wan event script did not start - ntp server failed to respond"
fi
fi
done

# Start SoftEtherVPN Server
# /jffs/addons/young/start_vpn_server

/jffs/addons/young/wakedc.sh

exit 0
;;
esac

Tha

Thanks, I'm usingg google...how can I add your scrip here?


#!/bin/sh
FROM="your-gmail-address"
AUTH="your-gmail-username"
PASS="your-gmail-password"
FROMNAME="Your Router"
TO="your-email-address"

ntpclient -h pool.ntp.org -s &> /dev/null
sleep 5

echo "Subject: WAN state notification" >/tmp/mail.txt
echo "From: \"$FROMNAME\"<$FROM>" >>/tmp/mail.txt
echo "Date: $(date -R)" >>/tmp/mail.txt
echo "" >>/tmp/mail.txt
echo "I just got connected to the internet." >>/tmp/mail.txt
echo "" >>/tmp/mail.txt
echo "My WAN IP is: $(nvram get wan0_ipaddr)" >>/tmp/mail.txt
echo "Uptime is: $(uptime | cut -d ',' -f1 | sed 's/^.\{12\}//g')" >>/tmp/mail.txt
echo "" >>/tmp/mail.txt
echo "---- " >>/tmp/mail.txt
echo "Your friendly router." >>/tmp/mail.txt
echo "" >>/tmp/mail.txt

cat /tmp/mail.txt | sendmail -H"exec openssl s_client -quiet \
-CAfile /jffs/configs/google_root.pem \
-connect smtp.gmail.com:587 -tls1 -starttls smtp" \
-f"$FROM" \
-au"$AUTH" -ap"$PASS" $TO

rm /tmp/mail.txt



And i didn't understand I've to do txt file?

Sorry for question but I don't find any tutorial...
 
EDIT: The spoiler code looks really bad, I changed it to general code so that the code looks better and is easier to follow.

You would have to put the contents you listed into the /jffs/scripts/wan-event script.

That said, as my wan-event script shows above, you need to test to see if wan-event is being called with the "connected" argument as $2 and also make sure the router time is set via ntp or the send mail function will fail (time disparity will be too great).

Also, the google SSL CA certificate /jffs/configs/google_root.pem will need to be downloaded. You can get google's certificate with;

wget https://pki.google.com/roots.pem
 
Ok, I've already downloaded the certificate, sorry but I didn't understand where I've to write the email in your script...

I don't know nothing about programming
 
Ok, I've already downloaded the certificate, sorry but I didn't understand where I've to write the email in your script...

I don't know nothing about programming

Yikes! That will make things difficult. Do have entware installed at all?

You will need to SSH into your router and create a script called wan-event in the /jffs/scripts directory with a text editor like vi (really ancient and not user friendly) or nano (which has to be installed, I believe - been to long now).

Once the script is created, you will need to make it executable;

chmod a+rx wan-event

Check out this page on the merlin-wiki for the basics;


A lot of this stuff does depend on the person knowing some basics on linux bash script writing. Lots of beginner tutorials on the web.

I can't this weekend, or this coming week, but I could whip up a script for you. You would still have to get it onto your router though.
 
Thanks! I'm using ssh to communicate with router..
I read something about entered but is it necessary?

Anyway I made a file with nano..and put it in used/scripts but doesn't work..
 
Did you make the file executable (chmod a+rx wan-event)?

You can simulate the WAN connection event by typing at the command prompt (you need to be in the /jffs/scripts directory);

./wan-event 0 connected

What is the output or any errors that you get when you type the above?
 
.... also make sure the router time is set via ntp or the send mail function will fail (time disparity will be too great).
Hi all,
@Jeffrey Young, you can please further explain the above ? I noticed that the ntpclient -h statement from the wiki returns an error .... (ntpclient not found)

thx,
GS
 
Hi all,
@Jeffrey Young, you can please further explain the above ? I noticed that the ntpclient -h statement from the wiki returns an error .... (ntpclient not found)

thx,
GS

The whole NTP thing was a real head banger when I was first making my wan-event script. The script would work fine when I run it manually, but not when the router rebooted. It was not until I read a technical brief somewhere that said Google's mail servers would ignore any inbound mail requests if the client's time and Google's time was too far apart.

Looking around on the forums, it seemed that the most common way of telling if the router's clock had been synced to NTP time was to use the NVRAM variable "ntp_ready". This variable gets set to 1 when the clock has been synced. Thus, I set up a loop counter in wan-event to to stall until "ntp_ready" was set to 1 - to a maximum of 30 seconds. Once I added this in, my wan-event script worked fine on router reboot.

Using the nvram variable probably is not the most ideal way of checking if NTP (or chrony if using NTPMerlin) is good and up to date, but given that the wan-event will always get called at some point during a reboot, I figured using the nvram variable was good enough for the purpose.

Hope this helps.
 
Yikes! That will make things difficult. Do have entware installed at all?

You will need to SSH into your router and create a script called wan-event in the /jffs/scripts directory with a text editor like vi (really ancient and not user friendly) or nano (which has to be installed, I believe - been to long now).

Once the script is created, you will need to make it executable;

chmod a+rx wan-event

Check out this page on the merlin-wiki for the basics;


A lot of this stuff does depend on the person knowing some basics on linux bash script writing. Lots of beginner tutorials on the web.

I can't this weekend, or this coming week, but I could whip up a script for you. You would still have to get it onto your router though.


Hi, sorry but can you help me...because I want to try this thing..sorry for request...
Moreover i try to find in the report the connection to wan, but I don't find it... I've to put log more than info??
Thanks
 
Can you post your script? Redact any personal info (password, userid, etc.)

Are using a gmail account just for the router? If so, did you log into the gmail account and create an application password for your router? Without an application password, gmail will reject any comms as gmail can't figure out where it is coming from (over simplified explanation).

If you are not using a gmail account specific to your router, then you may have to turn on "allow less secure access" in your gmail account.

You can run the script manually in order to troubleshoot. When you in the /jffs/scripts directory, simply enter ./wan-event

During trouble shooting, I will often put a bunch of 'echo' statements in the code to see where execution is going hey wire.
 
Yes i used password for app!!

I used this:

#!/bin/sh
FROM="your-gmail-address"
AUTH="your-gmail-username"
PASS="your-gmail-password"
FROMNAME="Your Router"
TO="your-email-address"

ntpclient -h pool.ntp.org -s &> /dev/null
sleep 5

echo "Subject: WAN state notification" >/tmp/mail.txt
echo "From: \"$FROMNAME\"<$FROM>" >>/tmp/mail.txt
echo "Date: $(date -R)" >>/tmp/mail.txt
echo "" >>/tmp/mail.txt
echo "I just got connected to the internet." >>/tmp/mail.txt
echo "" >>/tmp/mail.txt
echo "My WAN IP is: $(nvram get wan0_ipaddr)" >>/tmp/mail.txt
echo "Uptime is: $(uptime | cut -d ',' -f1 | sed 's/^.\{12\}//g')" >>/tmp/mail.txt
echo "" >>/tmp/mail.txt
echo "---- " >>/tmp/mail.txt
echo "Your friendly router." >>/tmp/mail.txt
echo "" >>/tmp/mail.txt

cat /tmp/mail.txt | sendmail -H"exec openssl s_client -quiet \
-CAfile /jffs/configs/google_root.pem \
-connect smtp.gmail.com:587 -tls1 -starttls smtp" \
-f"$FROM" \
-au"$AUTH" -ap"$PASS" $TO

rm /tmp/mail.txt


Works only on time, after reboot not...I've to write it in jfcs, correct?

And is it also possible send by mail a report app usage?
I don't find anything...

Very complicated for me..
 
What router are you using? There is no ntpclient command in Merlin, unless you have installed something through entware.

See my wan-event script above for how I use the NVRAM variable "ntp_ready" to check to see if the router's clock has been set. Otherwise, you could simply just wait the 5 or 10 seconds and just hope the clock gets set.

The command to actually send the email looks OK. The only real thing I see is the line; echo "From: \"$FROMNAME\"<$FROM>" >>/tmp/mail.txt maybe should be;

echo "From: $FROMNAME <$FROM>" >>/tmp/mail.txt

You can try to remove the "rm /tmp/mail.txt" statement. That way, you can have a look at the mail.txt file after you run the script to see if everything looks right.
 
I just re-read your last email an noted that you said that the script works if you run it manually right? But not after a reboot?

The script is called /jffs/scripts/wan-event right?

If so, then it sure does sound like it is failing because the clock is not set. Don't forget too that wan-event gets called for other wan events than just connected. You are testing that the wan-event is "connected" before you try to email the status?

I don't know what you mean by a "report app usage"
 
What router are you using? There is no ntpclient command in Merlin, unless you have installed something through entware.
He's copied that code from the wiki. The ntpclient stuff was added in 2013 when it was the router's NTP client. John's firmware still uses it.

 
I mean app report i mean this (file attached)

The thing that I've installed is this for google...

wget -c -O /jffs/configs/google_root.pem https://pki.goog/roots.pem --no-check-certificate

My clock seems ok...

I want to try you script but I don't understand where put Gmail account e password..when I'm at home I'll send the output to simulate the connection...
Thanks for patience!!
It's hard for following some your instructions...
 

Attachments

  • Screenshot_20210514-203422_Chrome.jpg
    Screenshot_20210514-203422_Chrome.jpg
    75.4 KB · Views: 121
He's copied that code from the wiki. The ntpclient stuff was added in 2013 when it was the router's NTP client. John's firmware still uses it.

Gotcha, that makes sense and explains @GSpock post as well. Thanks Colin
 
I mean app report i mean this (file attached)

The thing that I've installed is this for google...

wget -c -O /jffs/configs/google_root.pem https://pki.goog/roots.pem --no-check-certificate

My clock seems ok...

I want to try you script but I don't understand where put Gmail account e password..when I'm at home I'll send the output to simulate the connection...
Thanks for patience!!
It's hard for following some your instructions...

Are you replacing these lines with your site specific arguments or are you using them as is;

FROM="your-gmail-address"
AUTH="your-gmail-username"
PASS="your-gmail-password"
FROMNAME="Your Router"
TO="your-email-address"

PASS is the google password for the account you are sending the email from, etc.

I mean no offence here, but I am getting the impression that your scripting skills/knowledge are not quite there. That's OK, we all start somewhere. It is just I don't think I am the right person to help at this point.

To your other question re the app statistics. I do not know where they are stored, or how so the quick answer is no.
 

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

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