Disconnect then connect Wifi Client script on Slate (GL-AR750S-Ext)

Hello everyone,

i need to disconnect sometimes from WiFi Client and reconnect it again if internet doesnt works anymore.

I write a script on my raspberry pi but it doesnt works on my GL-AR750S.

it checks via cron every minute the ping if internet is down then it disconnect and connect wifi again.

here is the code:

#!/bin/bash

The IP for the server you wish to ping (8.8.8.8 is a public Google DNS server)

SERVER=8.8.8.8

Only send two pings, sending output to /dev/null

ping -c2 ${SERVER} > /dev/null

If the return code from ping ($?) is not 0 (meaning there was an error)

if [ $? != 0 ]
then
# Restart the wireless interface
ifconfig wlan0 down
sleep 15
ifconfig wlan0 up
fi

what i do wrong?

I am running something similar, within a loop instead of using cron, and my if statement looks like:

  ping -q -c 1 $SITE
  if [ "$?" -eq 0 ]
  then
      #saw remote site, so all is good
      # do something!
  fi

You may want to try β€œ-ne” instead of β€œ!=” in your β€œif” statement.

Also, change:

#!/bin/bash 
to
#!/bin/ash

As bash is normally not available.

1 Like

Thank you eric it works!

here is my code if some one need it in future (saved under /usr/bin/wifi_rebooter.sh):

#!/bin/ash
SERVER=8.8.8.8

ping -c2 ${SERVER} > /dev/null
if [ $? -ne 0 ]
then
   echo "Offline"
   ifconfig wlan-sta down
   sleep 25
   ifconfig wlan-sta up
else
   echo "Online"
fi

then make it executable:

chmod +x /usr/bin/wifi_rebooter.sh

Now set a cron in http://192.168.8.1/cgi-bin/luci/ under System β†’ Scheduled Tasks i added this line to check every 2min if internet connection exists if not restart wifi client:

*/2 * * * * /usr/bin/wifi_rebooter.sh

Now go to System β†’ Startup and restart cron.

1 Like