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!

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.
Maybe.is my English the problem...i meat:

I the script which I posted i known where place username and password...

But I want to use yours, and there aren't the "variables" before the code..
 
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"
sorry jeffy,
now seems work, but after reboot i receive 3 mails....mnnn
 
Sorry @coloxim just seeing this now. Been busy with yard work today.

The wan-event gets called for various reasons. Unless you test $2 for "connected", you are going to get emails for every wan-event (init, starting, connected, etc. ).

That would be my guess why you are getting multiple emails.

Glad you got it figured out.
 
Spiacenti, [USER = 76109] @coloxim [/ USER] ha visto questo messaggio. Oggi sono stato impegnato con il lavoro in giardino.

L'evento pallido viene richiamato per vari motivi. A meno che non provi $ 2 per "connesso", riceverai email per ogni evento wan (init, avvio, connessione, ecc.).

Questa sarebbe la mia ipotesi perché stai ricevendo più email.

Sono contento che tu abbia capito.
come può implementare $ 2 mio codice?

io'm non sicuri di fare un lavoro buono .....

i added also wan event in cru, so i can receive and email every day in evening so see the time of connection...
 
If you look at https://github.com/RMerl/asuswrt-merlin.ng/wiki/User-scripts

You can see that Merlin passes two variables to wan-event. They are the interface (0 or 1) and the event being handled.

from inside the wan-event script, you can access those variables as $1 as the interface and the $2 as the event. If you look at my wan-event script, you see where I test $2 to see if it is equal to "connected". If it is, then I build my email body, then call another script that is my email code. I have a number if scripts that email me for various reasons, so my scripts builds the email body, then I call another script that actually does the email work.

Since you are also running your script via a cron job, you can call the wan-event script manually by entering "/jffs/scripts/wan-event 0 connected" to simulate how Merlin would call the script.

BTW, don't worry about doing a "good" job. If your script works and does what you want, then it's a "good" job. Sure, there is better ways to make code, fancy ways to make code, maybe more efficient ways to make code, but for me, a good code is one that works.
 
In cron i call the script, i didn't out all script of course...anyway if I call manually the script i received only one mail, after reboot or if I disconnect that cable i receive 5 email... Ye, i read about $1 and $2... I've to understand how put inside the script..i read yours, everything seems a bit complicated... eheheh

Thanks for patience...
 
It's not easy, but I'll try....
Another question, in my script i receive an email with up time of system from last reboot. Is possible have also timeup of the connection? How can I find it?
 
It's not easy, but I'll try....
Another question, in my script i receive an email with up time of system from last reboot. Is possible have also timeup of the connection? How can I find it?

Not any easy way that I can think of.
 
ok add $1 and $2 fro me is impossible....
anyway when i send command: ./wan-event 0 connnected or only ././wan-event i receive only one mail adn theoutpur is:


depth=2 OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign
verify return:1
depth=1 C = US, O = Google Trust Services, CN = GTS CA 1O1
verify return:1
depth=0 C = US, ST = California, L = Mountain View, O = Google LLC, CN = smtp.gmail.com
verify return:1
250 SMTPUTF8
read:errno=0



but if i reboot router the email as you said are 4/5...
better than nothing
 
Ok, so using the script you posted in post 14, you would build your wan-event like this to test for connected state;

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

if [ $2 = "connected" ]
then

    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

fi
 
Ok, so using the script you posted in post 14, you would build your wan-event like this to test for connected state;

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

if [ $2 = "connected" ]
then

    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

fi
Thanks! Tomorrow I'm at home and I'll try your script...i made something similar but I was thinking to declare $2 as variable...

Thanks¡ I'll tell you how it works!
 
No need to declare $2 as a variable. It gets passed to the script from the command line. Thus, the command "wan-event 0 connected" passes "0" to the script as $1 and "connected" as $2.
 
No need to declare $2 as a variable. It gets passed to the script from the command line. Thus, the command "wan-event 0 connected" passes "0" to the script as $1 and "connected" as $2.
Ok...thanks! I never programming...i just learned something Right now...this afternoon I'll test your script...
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
Need to install Entware for your script to work?
Something I can't understand how you send messages to yourself
 
Last edited:
I don't believe so.

There was another post recently about emailing using Google that did not require the use of the certificate.

When I built this script, I tried using it without the certificate, but only half of the emails would go through. Very odd.

I am away from my computer for the next couple weeks (camping season). When I have something better than a phone, I will look up the post (if someone else does not).
 
Something I can't understand how you send messages to yourself

Sorry, missed that part. I created a google account for my router, using an Google app password. The router than emails me from it's own account.
 
And what are these files responsible for?
/jffs/addons/young/smail.sh
/jffs/addons/young/wakedc.sh
Initially, they are not in the router. And why is SoftEtherVPN in your script?
 

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