Help, which of these scripts is best to restart the WAN

I just installed LEDE / Openwrt on my router. I think it is necessary to restart the WAN, what is the best script to do that job?

#!/bin/sh

tries=0
while [[ $tries -lt 5 ]]
do
        if /bin/ping -c 1 8.8.8.8 >/dev/null
        then
                exit 0
        fi
        tries=$((tries+1))
done

ifdown wan
sleep 5

or

#!/bin/sh

tries=0
while [[ $tries -lt 5 ]]
do
    if /bin/ping -c 1 8.8.8.8 >/dev/null
    then
        exit 0
    fi
    tries=$((tries+1))
done

/etc/init.d/network restart

Thanks! :nerd_face:

I'm not sure if it's a good approach, but between the two only the second one is a valid script. The first one looks to be auto-translation of the script to spanish which would just generate a bunch of errors.

I'd find out why you (think you) need to restart the WAN and fix the problem, rather than poking it in the eye with a sharp stick. it's quite uncommon to need to regularly restart an interface on a Linux-based system.

Neither of those scripts are particularly robust and the first one won't work at all (it never brings the interface back up). What happens if 8.8.8.8 isn't up? Boom, you're down. How long does 5 tries at ping take? What is ping timeout? What happens if ping fails for a different reason?

If you've still got a problem once you figure out why you think you need to restart the interface, we can come back to more robust ways of detecting the failure condition.

Sorry, I just corrected the first scritp :sweat_smile:
Which one is better to reconnect the WAN?

In the first script I miss the ifup command. :smiley:

1 Like

I have tried to use a script to reconnect the WAN because my ISP usually has connection problems. I have a small TP-Link router that every time I look to connect increases the use of its 400Mhz CPU, looking for a new IP account. That's why I was interested in using a script to reconnect the WAN or in case to restart the router to give it a new break ... :grin:

I would also investigate why is that happening, it does not look normal to me.

1 Like

Why not use cron (Scheduled Tasks) to restart the wan ?
I'm using cron to restart/reconnect my pppoe connection (pppoe-wan) at a specific time and it works just fine...
Example: 00 08 * * * ifup wan
(it restarts my wan interface every morning at 08:00)

3 Likes

PPPoE has an LCP-based mechanism to detect a filed connection and restart it automatically. I have never had to restart my internet connection (manually or programmatically), that protocol takes care of everything.

Lemme try to explain....
My ISP has a 24h disconnect @PPPoE which results in a fresh IP.
So if my modem loses the DSL connection and re establish it again i do recieve a PPPoE disconnect exactly 24 hours later and that could be in the primetime where some ppl are using the internet or VOIP...
Thats why i force the pppoe disconnect/reconnect @08:00 in the morning to get around a random disconnect, which could happen for example in the prime time (16:00-22:00).
Hope u understand what i mean. :wink:

Some ISPs are weird...

Lol, well at least my ISP is a bit weird...
Look at my PAP AuthAck: rcvd [PAP AuthAck id=0x1 "Pedo mellon a minno : ######.....
The Technicians at my ISP are true "The Lord of the Rings" fanboys... :wink:

your first script is still incomplete. It needs to be
ifdown wan
sleep 5
ifup wan
(otherwise it would just disable "wan", but not restart it)

The 2 scripts would do the same, if modify the first script even more:
ifdown -a
sleep 5
ifup -a

(side note: the "sleep for 5 seconds" might be optional in your case)

"network restart" restarts all interfaces (wan, lan, bridge, loopback, all your wifi's). Also does ifdown/ifup with "-a".

In contrast the combo "ifdown wan; ifup wan" will just restart the wan interface.

So without questioning your reasons, the technical answer to your question is:
Technically both of your scripts will work, as both will restart "wan" (as long as you add the missing "ifup wan" to the end of the first script).
Restarting all interfaces is a bit slower and you will also notice a short downtime on the other interfaces compared to just restarting the "wan" interface.

2 Likes

Performing a bit of thread-necromancy here instead of starting a new one, in hopes that whatever hapless soul find this in the future is glad to have all the info in one place.

Problem: Once in a while, my WAN interface stops passing traffic. I know it's the WAN interface because I can no longer ping the 192.168.100.1 address which lives in my cable modem (and serves a nice little status page on port 80). I've emailed logs to my router's support folks because I'd like to get to the bottom of this, but I'm going out of town soon and needed a quick-and-dirty solution ASAP.

I can log into LuCI, find my way over to "interfaces", and hit the "restart" button, and it instantly fixes the problem. I needed an automatic scriptable version of that "restart" button.

Notable detail: In my case since the address I'm pinging is part of the cable modem a few inches away from the router, a single missed ping means trouble. I have no need for counting "tries" or anything like that.

Solution: I found this thread and didn't love the solution, so I found some noob-friendly shell scripting resources and had a go at it myself. Here's my script, with some debugging echoes commented out but left in place for explanation:

#!/bin/ash
# This should ping the cable modem and if it's not reachable, bounce the wan interface

sleep 30 		# Since this script gets called at bootup, give things some time to settle

while :
do

ping -c 1 -w 1 192.168.100.1 > /dev/null
if [ $? -ne 0 ]; then
	#echo "Ping didn't exit cleanly"
	ifdown wan
	#echo "ifdown fired"
	sleep 4	# I have no idea if this is needed. 
	ifup wan
	#echo "ifup fired"
	sleep 20	# again, give it a moment to come back up and settle
else
	#echo "ping was OK"
fi

sleep 10		# just to give me time to kill it in case it goes haywire, could shorten to 1. 
done

I just dropped this in /root/ as pingtest.sh because I'm lazy and frankly I've never gotten my head around the unix directory structure anyway. Where does it belong?

Anyway, then I tacked a line onto the end of /etc/rc.local like so:

/root/pingtest.sh &

And finally, rebooted to watch it take effect. It works, it works! I can now watch the network page in my browser, momentarily unplug the WAN cable, and watch my script bounce the interface every 35 seconds until it's plugged back in. Presumably this should also catch the real problem when it happens, but I expect to be ignorant of such events now.

I hope this helps someone.

4 Likes

Hi, I'm one of that hapless souls. I just wanna ask if this script works in loop, or does it just fires for only once after reboot? Thanks

Do what I did; dig up some bash scripting resources and read a little! :slight_smile:

The "do" and "done" lines form a looping structure. It sleeps for 10 seconds before looping, but you can easily change any of the delays.

(Oh and yes, six weeks on, it's working perfectly; my WAN link has never gone down long enough for me to notice.)

1 Like

This is the fastest way to renew my ip. Netflix does not stop doing this.

Thanks.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.