What's new

Need a script that auto reboot if internet is down

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

I used telnet from a WIN-10 machine into my router and used 'cd' to go to the /jffs/scripts directory.
There I used the build-in editor 'nano' to create your script on the router

Hope the script helps.

NOTE: Rather than use in-secure Telnet?? :eek: it is better to use SSH, and as a bonus, there are more user friendly clients e.g. Xshell6, MobaXterm or even PuTTY.

Also being a Windows user, you may find Mini WinSCP tutorial better than nano to create/edit scripts on the router.
 
Hope the script helps.

NOTE: Rather than use in-secure Telnet?? :eek: it is better to use SSH, and as a bonus, there are more user friendly clients e.g. Xshell6, MobaXterm or even PuTTY.

Also being a Windows user, you may find Mini WinSCP tutorial better than nano to create/edit scripts on the router.

Hi, thanks for your reply and advice!

I worked with UNIX (and lookalikes) many moons ago, so for me i was a bit rusty to get things on the router at first, also my use of telnet is really incidental and only on my local network.

I do have another beginners question about your script:

In the logging I now see the successful ping to 8.8.8.8 every 30 seconds.
Will the script switch to a second Google IP-address like 8.8.4.4 after a fail on 8.8.8.8?

So, can I conclude that when I only see succesfull pings to 8.8.8.8 it never had a problem so far?

Thanks again, un saludo,

Leo
 
I do have another beginners question about your script:

In the logging I now see the successful ping to 8.8.8.8 every 30 seconds.
Will the script switch to a second Google IP-address like 8.8.4.4 after a fail on 8.8.8.8?
Yes it will try other PING targets …. see below

So, can I conclude that when I only see succesfull pings to 8.8.8.8 it never had a problem so far?
Yes.

You can view the default five PING targets (Gateway, 2xDNS and 2xServers) used by the script:
Code:
 ./ChkWAN.sh   noaction   once   nowait

(ChkWAN.sh): 21479 v1.09 Monitoring WAN connection using 5 target PING hosts (ISP.GW.xxx.xxx ISP.DNS.xxx.xxx ISP.DNS.xxx.xxx 8 8.8.8.8 1.1.1.1) (Tries=3)

The script will try them in sequence until a successful PING occurs.

NOTE: You may customise/change the list of PING targets
see the script's help
Code:
./ChkWAN.sh   -h
 
Yes it will try other PING targets …. see below


Yes.

You can view the default five PING targets (Gateway, 2xDNS and 2xServers) used by the script:
Code:
 ./ChkWAN.sh   noaction   once   nowait

(ChkWAN.sh): 21479 v1.09 Monitoring WAN connection using 5 target PING hosts (ISP.GW.xxx.xxx ISP.DNS.xxx.xxx ISP.DNS.xxx.xxx 8 8.8.8.8 1.1.1.1) (Tries=3)

The script will try them in sequence until a successful PING occurs.

NOTE: You may customise/change the list of PING targets
see the script's help
Code:
./ChkWAN.sh   -h

Hi, just a quick update on my first WAN recovery reboot thanks to your script :)

I did start the script from 'wan-start' like this: ' sh /jffs/scripts/ChkWAN.sh & ' and it was pinging 8.8.8.8 every 30 seconds sucessfully.
However, I could read from the logging that at one point the ping failed, so it tried to ping 8.8.8.8 four times in a row, and in the second attempt again 4x a ping to 8.8.8.8, and the same in the third attempt.

I guess using the same IP 8.8.8.8 has to do with the start options, so it only uses one IP address??

One remark: I do use the 8.8.8.8 and 8.8.4.4 in the WAN and DHCP settings of the router instead of the DNS servers I get from the provider.

If I manually run the script as you told me: './ChkWAN.sh noaction once nowait' I do get these five IP addresses: 10.70.16.1 8.8.8.8 8.8.4.4 8.8.8.8 1.1.1.1 with the remark it will skip the private LAN 10.70.16.1 address for WAN PING check.

Now I changed the start options like this: 'sh /jffs/scripts/ChkWAN.sh ping=8.8.4.4,8.8.8.8,9.9.9.9,1.1.1.1' to see if that changes things.

This is the result from my first script induced reboot with no start options:

ASUS ChkWAN.sh result.jpg

To be continued, thanks,

un saludo,

Leo
 
Hi all,

a quick follow-up:

Yesterday I changed the way the script is started, like this: 'sh /jffs/scripts/ChkWAN.sh ping=8.8.4.4,8.8.8.8,9.9.9.9,1.1.1.1'
This is the line in the wan-start script.

And this morning, the script worked again :) and rebooted my router when shortly after the connection dropped.
However, again it only used one IP-address (the first from the start-options) during the three attempts, the 8.8.4.4 Google DNS IP.

Like this: Screenshot_2018-11-29-08-57-22-401_com.android.chrome.png

For test purposes I will change the two DNS servers back to the two offered by my provider.

To be continued,

Un saludo,

Leo
 
Yesterday I changed the way the script is started, like this: 'sh /jffs/scripts/ChkWAN.sh ping=8.8.4.4,8.8.8.8,9.9.9.9,1.1.1.1'

And this morning, the script worked again :) and rebooted my router when shortly after the connection dropped
So your prime objective was achieved :D
However, again it only used one IP-address (the first from the start-options) during the three attempts, the 8.8.4.4 Google DNS IP.

Hmm, can you try giving the script two PING addresses that are expected to fail and two that should work
e.g.
Code:
./ChkWAN.sh   noaction   nowait   once   fails=1   tries=1   ping=1.2.3.4,6.7.8.9,208.67.222.222,8.8.8.8
So ignoring the fact that v1.09 script reports the wrong PING IP when it fails :oops::oops:, can you post the output?

EDIT: v1.10 uploaded to correctly display the PING IP that fails.
 
Last edited:
So your prime objective was achieved :D


Hmm, can you try giving the script two PING addresses that are expected to fail and two that should work
e.g.
Code:
./ChkWAN.sh   noaction   nowait   once   fails=1   tries=1   ping=1.2.3.4,6.7.8.9,208.67.222.222,8.8.8.8
So ignoring the fact that v1.09 script reports the wrong PING IP when it fails :oops::oops:, can you post the output?

EDIT: v1.10 uploaded to correctly display the PING IP that fails.

Hi,

I tested like you asked with V1.09 like this:

I changed the start method back to 'sh /jffs/scripts/ChkWAN.sh ping=1.2.3.4,2.3.4.5,8.8.8.8,8.8.4.4 &'
If that runs in the background only the first IP is logged as failed, but when I run it as ./wan-start from my telnet window, I DO see the proper IP addresses, so it's probably just the output to the system-log?

This is from my telnet window where it DOES use the different IP addresses in a round robin way:
=================================================================================================================
(ChkWAN.sh): 17996 v1.09 Monitoring WAN connection using 4 target PING hosts (1.2.3.4 2.3.4.5 8.8.8.8 8.8.4.4) (Tries=3)

(ChkWAN.sh): 17996 Monitoring pass 1 out of 3

PING 1.2.3.4 (1.2.3.4): 56 data bytes

--- 1.2.3.4 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
PING 1.2.3.4 (1.2.3.4): 56 data bytes

--- 1.2.3.4 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
PING 1.2.3.4 (1.2.3.4): 56 data bytes
admin@Router_Storage:/jffs/scripts#
--- 1.2.3.4 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
admin@Router_Storage:/jffs/scripts# (ChkWAN.sh): 17996 v1.09 Monitoring WAN connection using PING method to 1.2.3.4 check FAILED


PING 2.3.4.5 (2.3.4.5): 56 data bytes

--- 2.3.4.5 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
PING 2.3.4.5 (2.3.4.5): 56 data bytes

--- 2.3.4.5 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
PING 2.3.4.5 (2.3.4.5): 56 data bytes

--- 2.3.4.5 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss

(ChkWAN.sh): 17996 v1.09 Monitoring WAN connection using PING method to 1.2.3.4 check FAILED

PING 8.8.8.8 (8.8.8.8): 56 data bytes

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 50.899/50.899/50.899 ms

(ChkWAN.sh): 17996 Monitoring WAN connection OK.....(Successful ping to '8.8.8.8'). Will check WAN again in 30 secs

PING 1.2.3.4 (1.2.3.4): 56 data bytes

--- 1.2.3.4 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
PING 1.2.3.4 (1.2.3.4): 56 data bytes

--- 1.2.3.4 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
PING 1.2.3.4 (1.2.3.4): 56 data bytes
ano
--- 1.2.3.4 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss

(ChkWAN.sh): 17996 v1.09 Monitoring WAN connection using PING method to 1.2.3.4 check FAILED

PING 2.3.4.5 (2.3.4.5): 56 data bytes

--- 2.3.4.5 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
PING 2.3.4.5 (2.3.4.5): 56 data bytes

--- 2.3.4.5 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
PING 2.3.4.5 (2.3.4.5): 56 data bytes

--- 2.3.4.5 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss

(ChkWAN.sh): 17996 v1.09 Monitoring WAN connection using PING method to 1.2.3.4 check FAILED

PING 8.8.8.8 (8.8.8.8): 56 data bytes

--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 40.518/40.518/40.518 ms

(ChkWAN.sh): 17996 Monitoring WAN connection OK.....(Successful ping to '8.8.8.8'). Will check WAN again in 30 secs
=================================================================================================================


By now I see you have uploaded V1.10, so I will install that one now.

Thanks for your work, besides the minor? output issue, your script kept my internet alive several times already :)

Will report back about V1.10.

Un saludo,

Leo
 
Last edited:
HI @Martineau ,

me, being the optimist, since so far my router did not need a reboot yet while using your V1.10, I added to the startup options the 'wan' option, just to see if restarting the WAN would be enough to get the connection back again. if a lost WAN connections occurs again.

This is my output in the system logging now:
.......
Nov 30 11:45:51 (ChkWAN.sh): 381 Private LAN 10.70.16.1 will be skipped for WAN PING check!
Nov 30 11:45:51 (ChkWAN.sh): 381 Monitoring WAN connection OK.....(Successful ping to '8.8.8.8'). Will check WAN again in 30 secs
Nov 30 11:46:21 (ChkWAN.sh): 381 Private LAN 10.70.16.1 will be skipped for WAN PING check!
Nov 30 11:46:21 (ChkWAN.sh): 381 Monitoring WAN connection OK.....(Successful ping to '8.8.8.8'). Will check WAN again in 30 secs
Nov 30 11:46:51 (ChkWAN.sh): 381 Private LAN 10.70.16.1 will be skipped for WAN PING check!
Nov 30 11:46:51 (ChkWAN.sh): 381 Monitoring WAN connection OK.....(Successful ping to '8.8.8.8'). Will check WAN again in 30 secs
etc. etc.

FYI: I changed my DNS-servers back to the Google one's again ( 8.8.8.8 and 8.8.4.4 )

To be continued,

Un saludo,

Leo
 
HI @Martineau ,

me, being the optimist, since so far my router did not need a reboot yet while using your V1.10, I added to the startup options the 'wan' option, just to see if restarting the WAN would be enough to get the connection back again. if a lost WAN connections occurs again.

This is my output in the system logging now:
.......
Code:
Nov 30 11:45:51 (ChkWAN.sh): 381 Private LAN 10.70.16.1 will be skipped for WAN PING check!
Nov 30 11:45:51 (ChkWAN.sh): 381 Monitoring WAN connection OK.....(Successful ping to '8.8.8.8'). Will check WAN again in 30 secs
Nov 30 11:46:21 (ChkWAN.sh): 381 Private LAN 10.70.16.1 will be skipped for WAN PING check!
Nov 30 11:46:21 (ChkWAN.sh): 381 Monitoring WAN connection OK.....(Successful ping to '8.8.8.8'). Will check WAN again in 30 secs
Nov 30 11:46:51 (ChkWAN.sh): 381 Private LAN 10.70.16.1 will be skipped for WAN PING check!
Nov 30 11:46:51 (ChkWAN.sh): 381 Monitoring WAN connection OK.....(Successful ping to '8.8.8.8'). Will check WAN again in 30 secs
etc. etc.

The only difference between v1.09 and v1.10 is
Code:
if [ -z "$METHOD" ];then
changed to
Code:
if [ -z "$METHOD" ] || [ -n "$(echo "$METHOD" | grep "PING")" ];then    # v1.10
simply to correct the minor issue where the first PING target in the list is always printed rather than the current one.

Functionally there is no difference.

If the WAN restart works, then you can use the techniques to try a WAN restart, then say after 3 attempts use the REBOOT option.

NOTE: When posting command output, to make it easier to read/scroll you should enclose the output in code tags

upload_2018-11-30_13-31-45.png
 
The only difference between v1.09 and v1.10 is
Code:
if [ -z "$METHOD" ];then
changed to
Code:
if [ -z "$METHOD" ] || [ -n "$(echo "$METHOD" | grep "PING")" ];then    # v1.10
simply to correct the minor issue where the first PING target in the list is always printed rather than the current one.

Functionally there is no difference.

If the WAN restart works, then you can use the techniques to try a WAN restart, then say after 3 attempts use the REBOOT option.

NOTE: When posting command output, to make it easier to read/scroll you should enclose the output in code tags

View attachment 15280

Hello @Martineau ,

never too old to learn :)

It did work!
Your script detected the WAN connection was lost, retried al four IP's and decided to restart the WAN after which the connection was re-established.

I tried to use the "code" option, but because of some weird coincidence I was blocked from the forum, so I'll try to attach a.txt file
Uploading the logging is somehow not allowed, because of its contents.
When I tried the 'CODE' way, I was blocked because of something in the text, and when I tried to upload the logging as a text-file, the system blocked that as well because of the content.
So, I am forced to do it as two images... ;-)

Asus logging 1-2.jpg Asus logging 2-2.jpg


Thanks again,

Un saludo,

Leo
 
Last edited:
Been working with this script with this weekend. Not sure what the intended logic is with the cron option, but if it is used, it does not create the cron job unless there is already an existing ChkWAN cron job in the cron table.

Once you manually create a ChkWAN cron job, then the code will find it, delete it and then create a new cron job.

Code:
    if [ "$(echo $@ | grep -cw 'cron')" -gt 0 ];then
        if [ -n "$(cru l | grep "$0")" ];then
            CRON_ARGS=$(echo $@ | sed -e 's/\<cron\>//g')    # Strip 'cron' from being passed to the cru entry
            cru d Wan_Check
            cru a Wan_Check "*/30 0 * * * /jffs/scripts/$0 $CRON_ARGS"        # Every 30 mins on the half hour
            CRONJOB=$(cru l | grep "$0")
            Say "ChkWAN scheduled by cron"
            echo -e "$CRONJOB"
        fi
    fi
 
Thanks for sharing the script. I want to use this script on my ac68s set up as media bridge. but since it is not connected to modem so wan-start won't get run. Where else should I put it so the script will get run in background whenever the ac68 gets rebooted after it sensed there is no internet?
 
The script should be called asynchronously at the end of wan-start
Hello Martineau.
I have used this script because sometime I lost the connection when I downloaded multiple files.

But this script doesn't work smoothly for me.
After few days, one more process is spawned and script stop the functioning.
One more thing, I can't find 'sleep 30' in process.
There is no special log in syslog.
I really don't know what is the problem.

Code:
32392 admin 3364 S    sh /jffs/scripts/ChkWAN.sh wan googleonly
32393 admin 3364 S    {cru} /bin/sh /usr/sbin/cru l
32394 admin 3368 S    grep /jffs/scripts/ChkWAN.sh
32395 admin 2952 S    nvram get http_username
32403 admin 3364 S    sh /jffs/scripts/ChkWAN.sh wan googleonly

wan-start
Code:
#!/bin/sh

ps | grep -i ChkWAN.sh | grep -v grep | awk '{print $1}' | xargs kill -9
sh /jffs/scripts/ChkWAN.sh wan googleonly &
 
Hello Martineau.
I have used this script because sometime I lost the connection when I downloaded multiple files.

But this script doesn't work smoothly for me.
After few days, one more process is spawned and script stop the functioning.

I really don't know what is the problem.
Neither do I. :confused: Your wan-start script appears to try and prevent duplicate spawned processes, although I would probably use 'killall' ChkWAN.sh
One more thing, I can't find 'sleep 30' in process.
In the current v1.11 of the script https://github.com/MartineauUK/Chk-WAN/blob/d338debc4f114bd449233fe8d7e2261d70151baf/ChkWAN.sh#L412 it isn't a literal, but is a user configurable variable value if the default 30 secs is deemed inappropriate.
 
Last edited:
Last edited:
I mean its process (sleep 30) didn't appear in ps monitor when script stopped functioning.

Edit: I removed cron check part in the loop.
I hope it work flawlessly.

If the script issues message
Code:
RT-AC86U (ChkWAN.sh): 7971 Monitoring connection OK.....(Successful ping to '8.8.8.8'). Will check again in 30 secs
yet there is no 'sleep 30' listed for the executing ChkWAN.sh script as would be expected
e.g.

2019-06-10_12-12-50.png


then this would imply that either the script correctly terminated due to a cron schedule conflict, or there is an (as yet) unidentified issue.

P.S. I'm not wholly convinced that removing the safe-guard cron check would fix the issue, unless the code to detect an existing cron scheduled instance of the script is syntactically flawed.
 
looks like this could help my issues with WAN dropping randomly every day or so. So I just download this script to a .sh file, place in /jffs/scripts and add it to the end of the wan_start script?
 
Could someone please provide step-by-step instructions for a complete beginner? I have got as far as logging in to the router using WinSCP and navigating to the following page:-

Screenshot - 10_12_2019 , 19_45_13.png
 

Similar threads

Latest threads

Sign Up For SNBForums Daily Digest

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