Shell Script to Control Power of USB Port

I am using the AR750 Slate tethered to my Android phone for internet, to monitor surveillance cameras at a small cabin. It works fine until I have a power outage. When power is restored the cable from the router to the phone has to be manually unplugged/plugged back in again to enable tether. So I lose my cameras until I visit the cabin again and do it manually.
I contacted tech support and was told that it would be possible to write a script in the router. This was there response…

"You can control the power of the USB port. This will be the same as unplug and replug the USB cable. Pls note, I am not sure if 0 or 1 is power on. You may need to try.

echo 0 > /sys/class/leds/gl-ar750s:white:usbpower/brightness
sleep 1
echo 1 > /sys/class/leds/gl-ar750s:white:usbpower/brightness

So if you can write a script and let it run after the router is powered (put in the end of /etc/rc.local with & at the end), using it to determine the Internet like 1 minute per check. If no Internet, just reset the USB power."

Anyone familiar with writing a shell script that could help out with this? I can do the testing but don’t know where to start with writing the script.
TIA

Can you check this post

First create the script, called /root/wan-watchdog.sh

cat << "EOF" > /root/wan-watchdog.sh
#!/bin/sh

. /lib/functions/network.sh
network_flush_cache
network_find_wan NET_IF
network_get_gateway NET_GW "${NET_IF}"

TRIES=“0”
while [ "${TRIES}" -lt 5 ]
do
  if ping -w 3 "${NET_GW}" &> /dev/null
  then exit 0
  else let TRIES++
  fi
done

echo 0 > /sys/class/leds/gl-ar750s:white:usbpower/brightness
sleep 1
echo 1 > /sys/class/leds/gl-ar750s:white:usbpower/brightness

EOF
chmod +x /root/wan-watchdog.sh

then, add it to scheduled task (cron) and execute this every minute. You can change the number to 2 (every two minutes) etc.

cat << "EOF" >> /etc/crontabs/root

*/1 * * * * /root/wan-watchdog.sh
EOF

Sorry I didn’t test this.

Thanks alzhao! Now just to figure out how to actually do this. :thinking: Totally new to this…

Can the writing of the script and adding to scheduled task (cron) all be done using WinSCP? Sorry for dumb questions but totally new to this. I have Putty and WinSCP up and running. Any guides you can point me to, that will help me get this script written and scheduled on my router?

You’d better do it in putty. Just copy and paste to the terminal.

Here is modified script for B1300. Also not tested. Use at your own risk.

cat << "EOF" > /root/wan-watchdog.sh
#!/bin/sh

. /lib/functions/network.sh
network_flush_cache
network_find_wan NET_IF
network_get_gateway NET_GW "${NET_IF}"

TRIES=“0”
while [ "${TRIES}" -lt 5 ]
do
  if ping -w 3 "${NET_GW}" &> /dev/null
  then exit 0
  else let TRIES++
  fi
done

echo 0 > /sys/class/gpio/gpio0/value
sleep 1
echo 1 > /sys/class/gpio/gpio0/value

EOF
chmod +x /root/wan-watchdog.sh

then, add it to scheduled task (cron) and execute this every minute. You can change the number to 2 (every two minutes) etc.

cat << "EOF" >> /etc/crontabs/root

*/1 * * * * /root/wan-watchdog.sh
EOF

What might help someone who later come to this forum:

  • OS of GL-iNet router is a custom image of OpenWRT, which is one of the Linux distributions
  • OpenWRT is available to root by default and supports basic linux commands (ls, cd, cat, etc.)
  • by default, ssh uses port 22 and accepts connections from all interfaces on the LAN side
  • For anyone who wants to learn about OpenWRT, OpenWrt wiki is usually the best documentation

Hi! I’m new to this whole concept.
How would this go for the AR150?
Thanks in advance!

Hi joebass3 , I have exactly the same setup and problem, did the solution listed work for you? Thanks!