What's new

VPNMON VPNMON-R2 v2.0 -Jul 10, 2022- Monitor your VPN connection's Health (Thread locked/closed)

  • 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!

Correct, it never gets to the "WAN Down" screen of red text saying "reboot or reconnect your modem" (can't quite remember exact wording), it just sits at the normal monitoring loop screen trying to detect WAN but never going further ...
okay I am looking into the code now to see if I can figure out what is happening. It doesn't look like any variables are amiss , maybe it is something with the conditional arguments. I will keep @Viktor Jaep posted if I find anything.
 
okay I am looking into the code now to see if I can figure out what is happening. It doesn't look like any variables are amiss , maybe it is something with the conditional arguments. I will keep @Viktor Jaep posted if I find anything.
Why must wan1_state_t equal 2 to break out of the loop? If you don’t use Dual WAN, why would wan1 even be relevant?
 
To be clear, I would have || instead of && because we are saying "or" instead of requiring in addition to.

Code:
checkwan () {

  # Using Google's 8.8.8.8 server to test for WAN connectivity over verified SSL Handshake
  Connectivity=1
  wandownbreakertrip=0
  vpnresettripped=0
  WAN_ELAPSED_TIME=0
  testssl=8.8.8.8

  # Show that we're testing the WAN connection
  if [ $1 == "Loop" ]
  then
    printf "${CGreen}\r[Checking WAN Connectivity]..."
  elif [ $1 = "Reset" ]
  then
    printf "${CGreen}\rChecking WAN Connectivity..."
  fi

  while [ $Connectivity == "1" ]; do

    # Start a timer to see how long this takes to add to the TX/RX Calculations
    WAN_START_TIME=$(date +%s)

    # Check the actual WAN State from NVRAM before running connectivity test
    if [ "$(nvram get wan0_state_t)" -ne 2 ] && [ "$(nvram get wan1_state_t)" -ne 2 ]
      then

        # The WAN is most likely down, and keep looping through until NVRAM reports that it's back up
        wandownbreakertrip=1

        while [ $wandownbreakertrip == "1" ]; do

          # Continue to test for WAN connectivity while in this loop. If it comes back up, break out of the loop and reset VPN
          if [ "$(nvram get wan0_state_t)" -ne 2 ] || [ "$(nvram get wan1_state_t)" -ne 2 ]
            then
              # Continue to loop and retest the WAN every 15 seconds
              echo -e "$(date) - VPNMON-R2 ----------> ERROR: WAN DOWN" >> $LOGFILE
              # clear screen
              clear && clear
                echo -e "${CRed}-----------------> ERROR: WAN DOWN <-----------------"
                echo ""
                echo -e "${CRed}VPNMON-R2 is unable to detect a stable WAN connection."
                echo -e "Trying to verify connection every $SPIN seconds...${CClear}\n"
                echo ""
                echo -e "${CRed}Please check with your ISP, or reset your modem to "
                echo -e "${CRed}re-establish a connection.${CClear}\n"
                spinner
                wandownbreakertrip=1
            else
              wandownbreakertrip=2
          fi
        done

      else

        # If the WAN was down, and now it has just reset, then run a VPN Reset, and try to establish a new VPN connection
        if [ $wandownbreakertrip == "2" ]
          then
            wandownbreakertrip=0
            vpnresettripped=1
            echo ""
            echo -e "${CGReen}WAN Connection Detected... waiting 30 seconds for connection to stabilize."
            echo -e "$(date) - VPNMON-R2 - WAN Connection Re-established -- Resetting VPN" >> $LOGFILE
            SPIN=30
            spinner
            clear && clear
            vpnreset
        fi

        # Else test the connection on active WAN using 443 and verifying a handshake... if this fails, then the WAN connection is most likely down... or Google is down ;)
        if nc -w1 $testssl 443 && echo |openssl s_client -connect $testssl:443 2>&1 |awk 'handshake && $1 == "Verification" { if ($2=="OK") exit; exit 1 } $1 $2 == "SSLhandshake" { handshake = 1 }'
          then

            if [ $1 == "Loop" ]
            then
              printf "${CGreen}\r[Checking WAN Connectivity]...ACTIVE"
              sleep 1
              printf "\33[2K\r"
            elif [ $1 = "Reset" ]
            then
              printf "${CGreen}\rChecking WAN Connectivity...ACTIVE"
              sleep 1
              echo -e "\n"
            fi

            WAN_END_TIME=$(date +%s)
            WAN_ELAPSED_TIME=$(( WAN_END_TIME - WAN_START_TIME ))

            return

          else
            wandownbreakertrip=1
        fi
    fi
  done

}

In the grand scheme of things && would require both wan1 and wan2 to be up and running, while || would accept either of the two as long as one was running?

Nice catch @dave14305
 
Last edited:
So both states must != 2 to indicate WAN down, but only one of them needs to = 2 to indicate WAN up? Is that the logic?
 
So both states must != 2 to indicate WAN down, but only one of them needs to = 2 to indicate WAN up? Is that the logic?
Sounds like it. I only use 1 wan and the other reports 2.


Code:
@RT-AX88U-C7C0:/tmp/home/root# nvram get wan1_state_t
0
@RT-AX88U-C7C0:/tmp/home/root# nvram get wan1_state_t
0
@RT-AX88U-C7C0:/tmp/home/root# nvram get wan0_state_t
2
 
Code:
checkwan () {


  # Using Google's 8.8.8.8 server to test for WAN connectivity over verified SSL Handshake

  Connectivity=1

  wandownbreakertrip=0

  vpnresettripped=0

  WAN_ELAPSED_TIME=0

  testssl=8.8.8.8


  # Show that we're testing the WAN connection

  if [ $1 == "Loop" ]

  then

    printf "${CGreen}\r[Checking WAN Connectivity]..."

  elif [ $1 = "Reset" ]

  then

    printf "${CGreen}\rChecking WAN Connectivity..."

  fi


  while [ $Connectivity == "1" ]; do


    # Start a timer to see how long this takes to add to the TX/RX Calculations

    WAN_START_TIME=$(date +%s)


    # Check the actual WAN State from NVRAM before running connectivity test

    if [ "$(nvram get wan0_state_t)" -ne 2 ] && [ "$(nvram get wan1_state_t)" -ne 2 ]

      then


        # The WAN is most likely down, and keep looping through until NVRAM reports that it's back up

        wandownbreakertrip=1


        while [ $wandownbreakertrip == "1" ]; do


          # Continue to test for WAN connectivity while in this loop. If it comes back up, break out of the loop and reset VPN

          if [ "$(nvram get wan0_state_t)" -ne 2 ] || [ "$(nvram get wan1_state_t)" -ne 2 ]

            then

              # Continue to loop and retest the WAN every 15 seconds

              echo -e "$(date) - VPNMON-R2 ----------> ERROR: WAN DOWN" >> $LOGFILE

              # clear screen

              clear && clear

                echo -e "${CRed}-----------------> ERROR: WAN DOWN <-----------------"

                echo ""

                echo -e "${CRed}VPNMON-R2 is unable to detect a stable WAN connection."

                echo -e "Trying to verify connection every $SPIN seconds...${CClear}\n"

                echo ""

                echo -e "${CRed}Please check with your ISP, or reset your modem to "

                echo -e "${CRed}re-establish a connection.${CClear}\n"

                spinner

                wandownbreakertrip=1

            else

              wandownbreakertrip=2

          fi

        done


      else


        # If the WAN was down, and now it has just reset, then run a VPN Reset, and try to establish a new VPN connection

        if [ $wandownbreakertrip == "2" ]

          then

            wandownbreakertrip=0

            vpnresettripped=1

            echo ""

            echo -e "${CGReen}WAN Connection Detected... waiting 30 seconds for connection to stabilize."

            echo -e "$(date) - VPNMON-R2 - WAN Connection Re-established -- Resetting VPN" >> $LOGFILE

            SPIN=30

            spinner

            clear && clear

            vpnreset

        fi


        # Else test the connection on active WAN using 443 and verifying a handshake... if this fails, then the WAN connection is most likely down... or Google is down ;)

        if nc -w1 $testssl 443 && echo |openssl s_client -connect $testssl:443 2>&1 |awk 'handshake && $1 == "Verification" { if ($2=="OK") exit; exit 1 } $1 $2 == "SSLhandshake" { handshake = 1 }'

          then


            if [ $1 == "Loop" ]

            then

              printf "${CGreen}\r[Checking WAN Connectivity]...ACTIVE"

              sleep 1

              printf "\33[2K\r"

            elif [ $1 = "Reset" ]

            then

              printf "${CGreen}\rChecking WAN Connectivity...ACTIVE"

              sleep 1

              echo -e "\n"

            fi


            WAN_END_TIME=$(date +%s)

            WAN_ELAPSED_TIME=$(( WAN_END_TIME - WAN_START_TIME ))


            return


          else

            wandownbreakertrip=1

        fi

    fi

  done


}


So the first if condition would be && and the second would be accepting or ||
 
Code:
checkwan () {


  # Using Google's 8.8.8.8 server to test for WAN connectivity over verified SSL Handshake

  Connectivity=1

  wandownbreakertrip=0

  vpnresettripped=0

  WAN_ELAPSED_TIME=0

  testssl=8.8.8.8


  # Show that we're testing the WAN connection

  if [ $1 == "Loop" ]

  then

    printf "${CGreen}\r[Checking WAN Connectivity]..."

  elif [ $1 = "Reset" ]

  then

    printf "${CGreen}\rChecking WAN Connectivity..."

  fi


  while [ $Connectivity == "1" ]; do


    # Start a timer to see how long this takes to add to the TX/RX Calculations

    WAN_START_TIME=$(date +%s)


    # Check the actual WAN State from NVRAM before running connectivity test

    if [ "$(nvram get wan0_state_t)" -ne 2 ] && [ "$(nvram get wan1_state_t)" -ne 2 ]

      then


        # The WAN is most likely down, and keep looping through until NVRAM reports that it's back up

        wandownbreakertrip=1


        while [ $wandownbreakertrip == "1" ]; do


          # Continue to test for WAN connectivity while in this loop. If it comes back up, break out of the loop and reset VPN

          if [ "$(nvram get wan0_state_t)" -ne 2 ] || [ "$(nvram get wan1_state_t)" -ne 2 ]

            then

              # Continue to loop and retest the WAN every 15 seconds

              echo -e "$(date) - VPNMON-R2 ----------> ERROR: WAN DOWN" >> $LOGFILE

              # clear screen

              clear && clear

                echo -e "${CRed}-----------------> ERROR: WAN DOWN <-----------------"

                echo ""

                echo -e "${CRed}VPNMON-R2 is unable to detect a stable WAN connection."

                echo -e "Trying to verify connection every $SPIN seconds...${CClear}\n"

                echo ""

                echo -e "${CRed}Please check with your ISP, or reset your modem to "

                echo -e "${CRed}re-establish a connection.${CClear}\n"

                spinner

                wandownbreakertrip=1

            else

              wandownbreakertrip=2

          fi

        done


      else


        # If the WAN was down, and now it has just reset, then run a VPN Reset, and try to establish a new VPN connection

        if [ $wandownbreakertrip == "2" ]

          then

            wandownbreakertrip=0

            vpnresettripped=1

            echo ""

            echo -e "${CGReen}WAN Connection Detected... waiting 30 seconds for connection to stabilize."

            echo -e "$(date) - VPNMON-R2 - WAN Connection Re-established -- Resetting VPN" >> $LOGFILE

            SPIN=30

            spinner

            clear && clear

            vpnreset

        fi


        # Else test the connection on active WAN using 443 and verifying a handshake... if this fails, then the WAN connection is most likely down... or Google is down ;)

        if nc -w1 $testssl 443 && echo |openssl s_client -connect $testssl:443 2>&1 |awk 'handshake && $1 == "Verification" { if ($2=="OK") exit; exit 1 } $1 $2 == "SSLhandshake" { handshake = 1 }'

          then


            if [ $1 == "Loop" ]

            then

              printf "${CGreen}\r[Checking WAN Connectivity]...ACTIVE"

              sleep 1

              printf "\33[2K\r"

            elif [ $1 = "Reset" ]

            then

              printf "${CGreen}\rChecking WAN Connectivity...ACTIVE"

              sleep 1

              echo -e "\n"

            fi


            WAN_END_TIME=$(date +%s)

            WAN_ELAPSED_TIME=$(( WAN_END_TIME - WAN_START_TIME ))


            return


          else

            wandownbreakertrip=1

        fi

    fi

  done


}


So the first if condition would be && and the second would be accepting or ||
But do you need to invert the logic from -ne 2 to -eq 2 and swap the if and else clauses so that if either wan state = 2, then you trip the breaker. In your snippet, if either are not 2, we will stay in the loop indefinitely.
 
As @dave14305

Has pointed out for the first check we want to confirm both Wans are simultaneously down. On the second we can break the chain on realizing one of them is functional.

I was led to believe that && means "AND IF", in order to handle a scenario of someone having dual WANs. One or the other would break that chain. I only have 1 WAN. WAN2 always indicates 0. It seems to work?
 
But do you need to invert the logic from -ne 2 to -eq 2 and swap the if and else clauses so that if either wan state = 2, then you trip the breaker. In your snippet, if either are not 2, we will stay in the loop indefinitely.
correct, so wouldn't && be appropriate then and maybe we are over looking at it?
 
What it is saying is that we require both to not be equal to 2 in order to declare the WAN down when && is used.

You have to use && to consider a fallback wan scenario.
 
I was led to believe that && means "AND IF", in order to handle a scenario of someone having dual WANs. One or the other would break that chain. I only have 1 WAN. WAN2 always indicates 0. It seems to work?
I think you need to do double order check.

Code:
checkwan () {

  # Using Google's 8.8.8.8 server to test for WAN connectivity over verified SSL Handshake
  Connectivity=1
  wandownbreakertrip=0
  vpnresettripped=0
  WAN_ELAPSED_TIME=0
  testssl=8.8.8.8

  # Show that we're testing the WAN connection
  if [ $1 == "Loop" ]
  then
    printf "${CGreen}\r[Checking WAN Connectivity]..."
  elif [ $1 = "Reset" ]
  then
    printf "${CGreen}\rChecking WAN Connectivity..."
  fi

  while [ $Connectivity == "1" ]; do

    # Start a timer to see how long this takes to add to the TX/RX Calculations
    WAN_START_TIME=$(date +%s)

    # Check the actual WAN State from NVRAM before running connectivity test
    if { [ "$(nvram get wan0_state_t)" -ne 2 ] && [ "$(nvram get wan1_state_t)" -ne 2 ] } || { [ "$(nvram get wan1_state_t)" -ne 2 ] && [ "$(nvram get wan0_state_t)" -ne 2 ]; }
      then

        # The WAN is most likely down, and keep looping through until NVRAM reports that it's back up
        wandownbreakertrip=1

        while [ $wandownbreakertrip == "1" ]; do

          # Continue to test for WAN connectivity while in this loop. If it comes back up, break out of the loop and reset VPN
          if { [ "$(nvram get wan0_state_t)" -ne 2 ] && [ "$(nvram get wan1_state_t)" -ne 2 ] } || { [ "$(nvram get wan1_state_t)" -ne 2 ] && [ "$(nvram get wan0_state_t)" -ne 2 ]; }
            then
              # Continue to loop and retest the WAN every 15 seconds
              echo -e "$(date) - VPNMON-R2 ----------> ERROR: WAN DOWN" >> $LOGFILE
              # clear screen
              clear && clear
                echo -e "${CRed}-----------------> ERROR: WAN DOWN <-----------------"
                echo ""
                echo -e "${CRed}VPNMON-R2 is unable to detect a stable WAN connection."
                echo -e "Trying to verify connection every $SPIN seconds...${CClear}\n"
                echo ""
                echo -e "${CRed}Please check with your ISP, or reset your modem to "
                echo -e "${CRed}re-establish a connection.${CClear}\n"
                spinner
                wandownbreakertrip=1
            else
              wandownbreakertrip=2
          fi
        done

      else

        # If the WAN was down, and now it has just reset, then run a VPN Reset, and try to establish a new VPN connection
        if [ $wandownbreakertrip == "2" ]
          then
            wandownbreakertrip=0
            vpnresettripped=1
            echo ""
            echo -e "${CGReen}WAN Connection Detected... waiting 30 seconds for connection to stabilize."
            echo -e "$(date) - VPNMON-R2 - WAN Connection Re-established -- Resetting VPN" >> $LOGFILE
            SPIN=30
            spinner
            clear && clear
            vpnreset
        fi

        # Else test the connection on active WAN using 443 and verifying a handshake... if this fails, then the WAN connection is most likely down... or Google is down
        if nc -w1 $testssl 443 && echo |openssl s_client -connect $testssl:443 2>&1 |awk 'handshake && $1 == "Verification" { if ($2=="OK") exit; exit 1 } $1 $2 == "SSLhandshake" { handshake = 1 }'
          then

            if [ $1 == "Loop" ]
            then
              printf "${CGreen}\r[Checking WAN Connectivity]...ACTIVE"
              sleep 1
              printf "\33[2K\r"
            elif [ $1 = "Reset" ]
            then
              printf "${CGreen}\rChecking WAN Connectivity...ACTIVE"
              sleep 1
              echo -e "\n"
            fi

            WAN_END_TIME=$(date +%s)
            WAN_ELAPSED_TIME=$(( WAN_END_TIME - WAN_START_TIME ))

            return

          else
            wandownbreakertrip=1
        fi
    fi
  done

}
 
@Viktor Jaep , You need to consider both sides of the coin.


Code:
if { [ "$(nvram get wan0_state_t)" -ne 2 ] && [ "$(nvram get wan1_state_t)" -ne 2 ] } || { [ "$(nvram get wan1_state_t)" -ne 2 ] && [ "$(nvram get wan0_state_t)" -ne 2 ]; }
 

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