From ce1c49dd6e306ffeaea33ac401f37ff4cedb7a3b Mon Sep 17 00:00:00 2001 From: tylen Date: Tue, 8 Jul 2025 22:33:42 +0300 Subject: [PATCH] router: init proejct --- router/restart-wan-guide.html | 319 ++++++++++++++++++++++++++++++++++ router/run.sh | 26 +++ router/scripts/init | 3 + router/scripts/restart-wan | 52 ++++++ 4 files changed, 400 insertions(+) create mode 100644 router/restart-wan-guide.html create mode 100755 router/run.sh create mode 100644 router/scripts/init create mode 100644 router/scripts/restart-wan diff --git a/router/restart-wan-guide.html b/router/restart-wan-guide.html new file mode 100644 index 0000000..cef5dcc --- /dev/null +++ b/router/restart-wan-guide.html @@ -0,0 +1,319 @@ + + + Restart WAN interface when internet is down - RMerl/asuswrt-merlin.ng GitHub Wiki + + + + + + + + + + + +
+ + +
+

Restart WAN interface when internet is down - RMerl/asuswrt-merlin.ng GitHub Wiki

+

If + you frequently lose internet connectivity with error like "Your ISP +DHCP does not function correctly" then you might want these script. It +is most useful for people with some router model like AC86U.

+

Advanced script

+

Fantastic scripts have been written by community users. It is better to use them. See these links.

+

https://www.snbforums.com/threads/need-a-script-that-auto-reboot-if-internet-is-down.43819/post-371791

+

https://github.com/MartineauUK/Chk-WAN

+

Simple script

+

Here below is a very basic script to run every 3 minutes and restart +WAN interface if 10 pings fail. Adjust as desired. Please see above for +more comprehensive scripts that do a better job.

+
    +
  1. Create and edit a new script file:
  2. +
+
nano /jffs/scripts/custom_wan_monitor
+
+
    +
  1. Write in this script:
  2. +
+
#!/bin/sh
+
+PING_HOST=1.1.1.1
+PING_WAIT=2
+MAX_TRIES=10
+
+wan_monitor(){
+    echo "WAN Monitor: $@"|/usr/bin/logger -s
+}
+restart_wan_interface(){
+    wan_monitor "Force restarting WAN interface now..."
+    service "restart_wan_if 0"
+    sleep 10
+    service "restart_wan_if 1"
+    wan_monitor "WAN interface is restarted."
+}
+ping_test(){
+    count_tries=0
+    ping_test_passed=0
+    wan_monitor "Running ping test..."
+    while [ $count_tries -lt $MAX_TRIES ]; do
+        if /bin/ping -c 1 -W $PING_WAIT $@ >/tmp/wan_check.log; then
+            ping_test_passed=1
+            wan_monitor "Ping test succeeded within $PING_WAIT secs."
+            break
+        else
+            sleep 1
+            let count_tries=count_tries+1
+            wan_monitor "Ping failed [$count_tries]"
+        fi
+    done
+}
+
+ping_test $PING_HOST
+
+if [ $ping_test_passed -gt 0 ]; then
+    wan_monitor "Internet was reachable. No need to restart WAN."
+    break
+else
+    wan_monitor "Pings failed. Internet must be down."
+    restart_wan_interface
+fi
+
+
    +
  1. Run this full command:
  2. +
+
touch /jffs/scripts/init-start ; echo 'cru a custom_wan_monitor "*/3 * * * * /jffs/scripts/custom_wan_monitor"' >> /jffs/scripts/init-start ; cat /jffs/scripts/init-start
+
+

See that it appended a new line to init-start.

+
    +
  1. Make both executable:
  2. +
+
chmod 755 /jffs/scripts/init-start /jffs/scripts/custom_wan_monitor 
+
+

Reboot router.

+

See cron job is now listed:

+
cru l
+
+

Check syslog to see that script runs continuously.

+

Read here for help customizing cron job timing. +https://crontab.guru/

+

References

+

Some relevant links for the ongoing issue:

+

https://www.snbforums.com/threads/your-isps-dhcp-does-not-function-correctly.43178/

+

https://www.snbforums.com/threads/new-rt-ax88u-no-wan-4-days-later.57960/

+

https://www.snbforums.com/threads/ac3100-primary-wan-assigned-show-your-isps-dhcp-does-not-function-correctly.61902/

+

https:// +www.snbforums.com/threads/asus-firmware-dhcp-continuous-mode-potential- +fix-for-isps-dhcp-did-not-function-properly.61907/

+

https://www.snbforums.com/threads/your-isps-dhcp-does-not-function-correctly.67033/

+

https://www.snbforums.com/threads/rt-ax88u-router-behind-modem-router-isp.67312/

+

https://www.snbforums.com/threads/rt-ax88u-internet-status-disconnected.67331/

+

https://www.snbforums.com/threads/ac86u-random-disconnects-but-stay-on.67927/

+

https://www.snbforums.com/threads/ax88u-behind-isp-router-internet-drops-vpn-isp-dhcp-does-not-function.68438/

+

https://www.snbforums.com/threads/wan_connection-isps-dhcp-did-not-function-properly.56226/

+ +
+
+ +
+ + + \ No newline at end of file diff --git a/router/run.sh b/router/run.sh new file mode 100755 index 0000000..f4fbdea --- /dev/null +++ b/router/run.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Variables +REMOTE_HOST="router" +REMOTE_DIR="/jffs/scripts/" # Replace with the destination directory on the remote server +RESTART_SCRIPT="$(git rev-parse --show-toplevel)/router/scripts/restart-wan" # Replace with the path to the first local file +INIT_SCRIPT="$(git rev-parse --show-toplevel)/router/scripts/init" # Replace with the path to the second local file + +# Prompt for the password +read -sp "Enter SSH password: " SSH_PASSWORD +echo + +# Init dir +sshpass -p "$SSH_PASSWORD" ssh "$REMOTE_HOST" "mkdir -p $REMOTE_DIR" + +# Copy files to the remote directory +sshpass -p "$SSH_PASSWORD" scp "$RESTART_SCRIPT" "$REMOTE_HOST:$REMOTE_DIR" +sshpass -p "$SSH_PASSWORD" scp "$INIT_SCRIPT" "$REMOTE_HOST:$REMOTE_DIR" + +# Change permissions to 775 +sshpass -p "$SSH_PASSWORD" ssh "$REMOTE_HOST" "chmod 775 $REMOTE_DIR/*" + +# Execute the init +sshpass -p "$SSH_PASSWORD" ssh "$REMOTE_HOST" "$REMOTE_DIR/$(basename $INIT_SCRIPT) &" + +echo "Files copied, permissions changed, and executed." diff --git a/router/scripts/init b/router/scripts/init new file mode 100644 index 0000000..40ad01a --- /dev/null +++ b/router/scripts/init @@ -0,0 +1,3 @@ +#!/bin/sh + +cru a restart-wan "* * * * * /jffs/scripts/restart-wan" \ No newline at end of file diff --git a/router/scripts/restart-wan b/router/scripts/restart-wan new file mode 100644 index 0000000..4f3385f --- /dev/null +++ b/router/scripts/restart-wan @@ -0,0 +1,52 @@ +#!/bin/sh + +# Configuration +PING_HOST="1.1.1.1" +PING_WAIT=2 +MAX_TRIES=4 + +# Function to log messages +wan_monitor() { + echo "WAN Monitor: $@" | /usr/bin/logger -s +} + +# Function to restart the WAN interface +restart_wan_interface() { + wan_monitor "Force restarting WAN interface now..." + service "restart_wan_if 0" # Bring down the interface + sleep 10 # Wait for 10 seconds + service "restart_wan_if 1" # Bring up the interface + wan_monitor "WAN interface is restarted." +} + +# Function to perform a ping test +ping_test() { + local count_tries=0 + local ping_test_passed=0 + wan_monitor "Running ping test..." + + while [ $count_tries -lt $MAX_TRIES ]; do + if /bin/ping -c 1 -W $PING_WAIT $@ >/tmp/wan_check.log; then + ping_test_passed=1 + wan_monitor "Ping test succeeded within $PING_WAIT secs." + exit 0 + else + sleep 1 + count_tries=$((count_tries + 1)) + wan_monitor "Ping failed [$count_tries]" + fi + done + + return $ping_test_passed +} + + + +if [ $(ping_test $PING_HOST) -gt 0 ]; then + wan_monitor "Internet was reachable. No need to restart WAN." +else + wan_monitor "Pings failed. Internet must be down." + restart_wan_interface +fi + +