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!

Pretty sure 192.168.20.1 IP is the modem that the router is bridged behind, so the same target/device as xxx.xxx.x.x , as I use completely different IP range for LAN & any network devices. Guess this is why I asked about having script ping google.com, or where I'd change the script to do this.

NAT: It is enabled, not sure about double NAT? How/where would I double check that?
Local DNS Server: Pretty sure no. LAN -> DNS Server 1 & 2 is blank. I do have VPN Server on the router but that wouldn't be related?
The script uses the NVRAM variables as shown below
Code:
# List of PING hosts to check
HOSTS="$(nvram get wan0_gateway) $(nvram get wan_dns) 8.8.8.8 8.8.4.4"      # Target PING hosts
in order to use the your appropriate WAN/DNS config, then appends the two Google DNS servers.

I have uploaded v1.07 of the script.

i.e. I have now filtered any LAN IP PING target, and also added the option to explicitly ignore the NVRAM variables and only use the two Google DNS servers as valid PING targets.
 
Last edited:
The script uses the NVRAM variables as shown below
Code:
# List of PING hosts to check
HOSTS="$(nvram get wan0_gateway) $(nvram get wan_dns) 8.8.8.8 8.8.4.4"      # Target PING hosts
in order to use the your appropriate WAN/DNS config, then appends the two Google DNS servers.

I have uploaded v1.07 of the script.

i.e. I have now filtered any LAN IP PING target, and added the option to ignore the NVRAM variables and only use the two Google DNS servers as valid PING targets.

working perfectly thanks Martineau
 
@Martineau if not too much time or effort is there any way for the script to be tweaked to restart the wan and if the wan is still down reboot?

Thinking behind this is that sometimes a restart of the wan is enough and others a reboot is in order to establish connectivity again.

I was going to create two cron jobs where one would run “ChkWAN wan force nowait” and the other ”ChkWAN reboot force nowait” but does not look like the best way to go about this.
 
@Martineau if not too much time or effort is there any way for the script to be tweaked to restart the wan and if the wan is still down reboot?
Thinking behind this is that sometimes a restart of the wan is enough and others a reboot is in order to establish connectivity again.
Let's be honest, if there is a choice between a recovery action that only works 'sometimes' and a recovery action that always works, why risk taking a chance that you may get lucky? o_O

Clearly you are free to create a custom script to meet your edge-case requirements (or preferably dump your flaky ISP/modem etc.), or even better, write a supplementary script (see SyslogEventMonitor.sh) that is triggered whenever a 'service restart_wan' entry is seen in Syslog to simply call 'ChkWAN.sh reboot' ?
I was going to create two cron jobs where one would run “ChkWAN wan force nowait” and the other ”ChkWAN reboot force nowait” but does not look like the best way to go about this.
What is your concern with having two cron jobs?
e.g. Every 3mins check and restart the WAN if it is DOWN, but make every 5th check a call to REBOOT if WAN is DOWN
Code:
cru a Restart_WAN "0,3,6,9,   15,18,21,24,   30,33,36,39,   45,48,51,54,   * * * *" /jffs/scripts/ChkWAN.sh wan    force nowait
cru a Reboot_WAN  "        12,            27,            42,            57 * * * *" /jffs/scripts/ChkWAN.sh reboot force nowait
However, given the goal is to restore the WAN as soon as possible? - if you really wanted, you could alternate the cron jobs as a pair, so that every 2nd call is a REBOOT call.
 
Last edited:
e.g. Every 3mins check and restart the WAN if it is DOWN, but make every 5th check a call to REBOOT if WAN is DOWN
Code:
cru a Restart_WAN "0,3,6,9,   15,18,21,24,   30,33,36,39,   45,48,51,54,   * * * *" /jffs/scripts/ChkWAN.sh wan    force nowait
cru a Reboot_WAN  "        12,            27,            42,            57 * * * *" /jffs/scripts/ChkWAN.sh reboot force nowait
However, given the goal is to restore the WAN as soon as possible? - if you really wanted, you could alternate the cron jobs as a pair, so that every 2nd call is a REBOOT call.

So just making sure I got this right;

If I place following in "wan-start" file & nothing else;
Code:
#!/bin/sh
sh /jffs/scripts/ChkWAN.sh &
cru a Restart_WAN "15,45,   * *" /jffs/scripts/ChkWAN.sh wan   force nowait googleonly
cru a Reboot_WAN  "0,30,   * *" /jffs/scripts/ChkWAN.sh reboot   force nowait googleonly

This will check & restart WAN every 30mins, but failing that, will check WAN & reboot router every 30mins, and only ping google? Still learning this/scripts & just trying to get my head around all this still.

Thanks!

EDIT: nope, have tried this & getting parse errors with the above. I'm obviously doing something wrong, but what?
 
Last edited:
Code:
cru a Restart_WAN "15,45,   * *" /jffs/scripts/ChkWAN.sh wan   force nowait googleonly
cru a Reboot_WAN  "0,30,   * *" /jffs/scripts/ChkWAN.sh reboot   force nowait googleonly
That's a syntax error. Should be:
Code:
cru a Restart_WAN "15,45 * * * * /jffs/scripts/ChkWAN.sh wan force nowait googleonly"
cru a Reboot_WAN  "0,30 * * * * /jffs/scripts/ChkWAN.sh reboot force nowait googleonly"

@Martineau There is a typo in line 20 of your script.
Code:
#                      'cru a ChkWAN "*/5 0 * * * /jffs/scripts/ChkWAN.sh"'
Should be:
Code:
#                      'cru a ChkWAN "*/5 * * * * /jffs/scripts/ChkWAN.sh"'
 
Last edited:
So just making sure I got this right;

If I place following in "wan-start" file & nothing else;
Code:
#!/bin/sh
sh /jffs/scripts/ChkWAN.sh &
cru a Restart_WAN "15,45,   * *" /jffs/scripts/ChkWAN.sh wan   force nowait googleonly
cru a Reboot_WAN  "0,30,   * *" /jffs/scripts/ChkWAN.sh reboot   force nowait googleonly

This will check & restart WAN every 30mins, but failing that, will check WAN & reboot router every 30mins, and only ping google? Still learning this/scripts & just trying to get my head around all this still.

Thanks!

EDIT: nope, have tried this & getting parse errors with the above. I'm obviously doing something wrong, but what?

The creation of the cru (cron) jobs should be performed either in init-start or services-start, as these two scripts are only executed once @boot, and this eliminates the cru (cron) jobs being unnecessarily recreated every time the WAN starts.

Also you have completely mangled the cru (cron) syntax. I suggest you use a cron generator to validate your required schedules

e.g. you have specified the following time specification '15,45, * *' which is shown as 'blank' i.e invalid on the web page, whereas you really meant '15,45 * * * *' which now helpfully shows a confirmation description of the desired time specification and when the next occurrence will fire in real-time

i.e.
Code:
“At minute 15 and 45.”
next at yyyy-mm-dd hh:45:00
 
Last edited:
There are many such scripts on the forum/Google search etc., and most use PING to a target host.

#============================================================================================ © 2016-2018 Martineau v1.05
#
# Monitor WAN connection state using PINGs to multiple hosts, or a single curl data request and optionally a 12MB WGET/CURL data transfer.
# Usually the Recovery action (REBOOT or restart the WAN) occurs in about 90 secs (PING ONLY) or in about 03:30 mins for 'force' data download

This is very nice! Installed on a Thank you Martineau!

sone
 
Let's be honest, if there is a choice between a recovery action that only works 'sometimes' and a recovery action that always works, why risk taking a chance that you may get lucky? o_O

Clearly you are free to create a custom script to meet your edge-case requirements (or preferably dump your flaky ISP/modem etc.), or even better, write a supplementary script (see SyslogEventMonitor.sh) that is triggered whenever a 'service restart_wan' entry is seen in Syslog to simply call 'ChkWAN.sh reboot' ?

What is your concern with having two cron jobs?
e.g. Every 3mins check and restart the WAN if it is DOWN, but make every 5th check a call to REBOOT if WAN is DOWN
Code:
cru a Restart_WAN "0,3,6,9,   15,18,21,24,   30,33,36,39,   45,48,51,54,   * * * *" /jffs/scripts/ChkWAN.sh wan    force nowait
cru a Reboot_WAN  "        12,            27,            42,            57 * * * *" /jffs/scripts/ChkWAN.sh reboot force nowait
However, given the goal is to restore the WAN as soon as possible? - if you really wanted, you could alternate the cron jobs as a pair, so that every 2nd call is a REBOOT call.

Thanks @Martineau I forgot to report back that I used your suggestions and it has been working flawlessly and bailed me out on the rare occasions my internet goes haywire.
 
Hi Martineau,

Thanks again for creating this script.. working great however I do have some questions:

1. When using "force" mode and the scripts downloads 12Mb.. (100 megabits/12.5 megabyte) is true if the intervals are every few seconds throughout the day this can rack up pretty high daily downloads from the ISP?

2. If I don't use the cron/cru option in the script, once initiated from reboot or manually from wan-start is there a way to kill the script that's running in the background? What I'm concerned about is an infinite boot loop if for some odd scenario happens and I'm trying to troubleshoot remotely.

3. Is running the script via cron/cru option a better choice especially with the above scenario because I assume I could just delete the cron entry.

4. Lastly, if I decide to not use "force" mode, of the options you have available in your script, which would be the best ping method.. I'm working with a flaky DSL ISP.

thank you for your time,
sone
 
Is running the script via cron/cru option a better choice especially <snip> because I assume I could just delete the cron entry.
Yes, using cron is the preferred method, as for most users the need to check the WAN in 'second' intervals (rather than 'minutes') is overkill.
When using "force" mode and the scripts downloads 12Mb.. (100 megabits/12.5 megabyte) is true if the intervals are every few seconds throughout the day this can rack up pretty high daily downloads from the ISP?
The 12MB transfer need not be performed on every invocation, but on a metered connection then yes it would eat into the data allowance.
If I don't use the cron/cru option in the script, once initiated from reboot or manually from wan-start is there a way to kill the script that's running in the background? What I'm concerned about is an infinite boot loop if for some odd scenario happens and I'm trying to troubleshoot remotely.
The script doesn't have a formal 'sigterm' feature/function to initate a controlled termination, so you will need to manually perform
Code:
kill $(pidof ChkWAN.sh)
If I decide to not use "force" mode, of the options you have available in your script, which would be the best ping method
The script (v1.08) allows the choice between PINGing to a custom list of Internet targets or a curl to retrieve a few bytes of data from an external public web url.
Now in an ideal world PING should suffice, but performing a physical data transfer thru' the WAN interface may possibly reduce the chance of a false positive but if the url is unavailable then obviously this would inspire less confidence in the script.
I'm working with a flaky DSL ISP.
This makes the use of a script such as mine a moot point i.e. what are the chances that the WAN goes into a DOWN state a millisecond after a successful ChkWAN.sh PING/CURL request?

NOTE: Use of a pro-active method may have its drawbacks, but using a passive SyslogEventMonitor for appropriate trigger messages in Syslog should also be considered.[/QUOTE]
 
Yes, using cron is the preferred method, as for most users the need to check the WAN in 'second' intervals (rather than 'minutes') is overkill.

The 12MB transfer need not be performed on every invocation, but on a metered connection then yes it would eat into the data allowance.

The script doesn't have a formal 'sigterm' feature/function to initate a controlled termination, so you will need to manually perform
Code:
kill $(pidof ChkWAN.sh)

The script (v1.08) allows the choice between PINGing to a custom list of Internet targets or a curl to retrieve a few bytes of data from an external public web url.
Now in an ideal world PING should suffice, but performing a physical data transfer thru' the WAN interface may possibly reduce the chance of a false positive but if the url is unavailable then obviously this would inspire less confidence in the script.

This makes the use of a script such as mine a moot point i.e. what are the chances that the WAN goes into a DOWN state a millisecond after a successful ChkWAN.sh PING/CURL request?

NOTE: Use of a pro-active method may have its drawbacks, but using a passive SyslogEventMonitor for appropriate trigger messages in Syslog should also be considered.
[/QUOTE]

Martineau - did you mean V1.07 - or have you updated the script to V1.08?
 

Martineau - did you mean V1.07 - or have you updated the script to V1.08?[/QUOTE]

There is indeed v1.08Beta - I will PM you with the link.
 
As I have handled Internet breaks for years, I may have few usable pointers.

- ping is useless, please, just trust me - skip it completely! There is an explanation there any expert can give you, no need to trust me. I tested it with over 10 different solutions for long time in parallel.

- verify two way. I am doing this for over 3 years, have tried several tests, and this proved to be the best simple and short (I use a multiple variant of it; not every site can be used for this test):

Code:
[ "$(curl -Is -m3 http://cisco.com)" ] && echo works || echo not

I actualy did isolate these tests into a simple script for a friend. Script is running on Asus and RPI (LibreElec) plus others (tested on standalone Linux and QNAP NAS). It logs problems if found.

- there are other problems: I have been given a private IP address (10.). Recently my provider grew a problem where routing would not work within their network if a 109.227.*.* was isssued as a WAN IP. One could be given something like 100.75.*.* which is Shared Address space RFC6598, and CGN NAT is also a real problem. For all these cases, VPN and SSH will not work while the Interenet will work normally.

- for DoubleNat: one must pull WAN IP address directly form the modem or one will be unable to recognize problems from the previous point

- you must reboot the modem. No need to reboot the router in most cases, unless router has a problem. Restarting the wan inteface is not a solution. I reboot the router itself only if it is connected to the USB modem powered by the router.

- it is quite simple to add automatic Cru (cron) to the program. Simple version using the /jffs/scripts/ddns-start as the filename and DoubleDDNS as the label and run at every 5 minutes is:
Code:
if [ ! "$(cru l | grep DoubleDDNS)" ]; then
   cru a DoubleDDNS "3-59/5 * * * * /jffs/scripts/ddns-start"
fi
I am commenting here just the simplest single use case. My program is a tad complex, it handles custom Double DDNS, multiple routers and locations, notifications, logging, statistics and a few more things, runs on several of my Asus Routers and the RPi. It is close to 3000 lines of code.
 
Can you post the version 1.08 i will like to try it

see post #2

The basic functionality remains the same, but allows more command line options...as per the help (-h)

e.g. a custom PING target list rather than the default five 'WAN gateway/ISP DNS/Google DNS/Cloudflare DNS' and also allows a cURL data transfer to be either 15Bytes, 500Bytes or 12Mbytes.
 
I'm in the same boat. While I get to the bottom of why my VDSL2 connection keeps flaking out, I need a re-start script. I would prefer to just restart WAN. A full reboot seems like an overkill, as a WAN restart should be fairly quick. My previous ISP modem seemed to handle this transparently and I didn't notice until I bridged to the new Asus RT-AC86U router.

Perhaps I could set up a simple check to a ISP server (so close by on the network) and as suggested above, restart WAN and if no luck after say three tries, reboot the device (although I'm still not sure what more a reboot would provide that a WAN restart does not, if the device is just the router to a bridged modem).

Merlin has a 'DNS Probe' option in the firmware to check WAN access. Can this be leveraged? Ideally, something baked in to the firmware for a simple WAN restart would be awesome.

My last ISP supplied modem seemed to do this without even asking it to so I'm sure it can be done.
 
On scripts and the Interet life: my network changed branding from VIP to A1 days ago. Since then - disaster.

Last night: entire routing outise of their network failed. My DNS tests detected that. But, no rebooting I could do could fix this.

2 Days ago, they had power outage in one area her my own networks are. Afterwards, I had luck they roatated one of my 3 WAN IP addresses. But, the area where it was and where powerwent down, would not update DNS and ignored ttl. So, next day I had to reboot the modem to fix things. In differena area, no such problem.

Just saying: many things can go wrong. Some we can detect and fix, some we can detect if we are very good but can not fix. Sometimes, we can only sit and watch.

Btw. When I called them this morning, they had absolutelly no idea their entire outside routing was dead for over an hour!
 
Hello @Martineau,

thank you for you work!

I am using an ASUS RT-66U using Merlin's firmware 380.70, and I recently changed from provider here in Spain.
The previous provider only needed a wired WAN connection to their antenna and was using DHCP for my IP and a check on my MAC address to see it was me.
I never had any real problems with this connection, other than it was slow.

Now my new provider is using PPPoe and a check on my MAC address, and now I was losing the WAN connection quite frequently (every day - two days) , but it could be resolved by a reboot of the router, so I went looking for a possible solution and found your script.

Here is how I implemented it:

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, I also used 'nano' to create the wan-start script there.

After that I performed a 'chmod a+rx /jffs/scripts/*' to make the scripts executable.

In the 'administration' section of the router I had to enable 'jffs custom scripts' so it would execute the 'wan-start' script.
After that I performed a reboot of the router, and now I can see the scripts 'ping-logging' in the system-log.

Here some pictures:

Asus router jffs settings.jpg Asus router reboot script wan-start.jpg Asus router reboot script.jpg

I will post a follow-up when it actually rebooted my router :)

Un saludo,

Leo
 
Last edited:

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