#!/bin/bash # File name: wconnet, ~/Documents/wconnect_01.sh # # $? is ping result, 0 is good with ping reply # # filename='~/Documents/public/failurelog.txt' # define and create log file name # # define variables hh=0 # calculaed internet failure period hour hm=0 # calculaed internet failure period minute hs=0 # calculaed internet failure period second lhh=0 # calculaed LAN failure period hour lhm=0 # calculaed LAN failure period minute lhs=0 # calculaed LAN failure period second st=0 # variable for counting target failure, 3=all 3 targets failured count=0 count1=0 dt="" # Interent failure start date time dtd=0 # Internet failure period in second msd=$(date) # Monitoring start date time dts=0 # Internet failure start date time in second dtn="" # Internet failure stop date time dtns=0 # Internet failure stop date time in second lan=0 # LAN failure flag, 0=normal, 1=failure lanfs="" # LAN failure star date time lanfe="" # LAN failure stop date time lanss=0 # LAN failure start date time in second lanes=0 # LAN failure stop date time in second lanfd=0 # LAN failure date time difference, period usbe="" gateway="192.168.0.1" # Set default gatway IP address # # echo "Monitoring start date - " $msd >> /home/kksino/Documents/public/failurelog.txt # mark monitor start date and time # # make sure network interfaces are up # ifconfig enp4s0f2 down # Bring wired connection down ifconfig enx000ec669c763 down # Bring USB network interface dwon sleep 5 # wait 5 seconds ifconfig enp4s0f2 up # Bring wired connection up sleep 5 # wait 5 seconds ifconfig enx000ec669c763 up # Bring USB network interface up sleep 5 # wait 5 seconds # while : # Loop forever do read _ _ gateway _ < <(ip route list match 0/0) # get gateway IP ping -c1 $gateway # check LAN status if [ "$?" != 0 ] # LAN gateway not reply, LAN failure then if [ $lan == 0 ] # If last check was not failured then let lan=1 # 1st failur, set LAN failure flag lanfs=$(date) # get now date and time lanss=$(date --date="$lanfs" +%s) # convert now date and time to second else # Repeated failure, make sure network interfaces are up ifconfig enp4s0f2 down # Bring wired connection down ifconfig enx000ec669c763 down # Bring USB network interface dwon sleep 5 # wait 5 seconds ifconfig enp4s0f2 up # Bring wired connection up sleep 5 # wait 5 seconds ifconfig enx000ec669c763 up # Bring USB network interface up sleep 5 # wait 5 seconds fi sleep 3 # wait else # LAN OK if [ $lan == 1 ] # If LAN recover from failure then lanfe=$(date) # get now date and time lanes=$(date --date="$lanfe" +%s) # converet now date and time to second let lanfd=$lanes-$lanss # get date/time difference in second declare -i lhh="$lanfd / 3600" # calculate hour let lanfd=$(( $lanfd % 3600 )) # get balance after hour declare -i lhm="lanfd / 60" # calculate minute let lhs=$(( $lanfd % 60 )) # calculate second # # Write failure record to log file echo "LAN (" $gateway ") failure start at: " $lanfs -- period: $lhh" hour " $lhm" minute " $lhs" second " >> /home/kksino/Documents/public/failurelog.txt # # Reset variables let lan=0 lanfs="" lanfe="" let lanss=0 let lanes=0 let lanfd=0 sleep 5 # wait else # Not recovery from LAN failure, check Internet connection (targets) let st=3 ping -c1 8.8.8.8 # check target 1 if [ "$?" == 0 ] # if target replied then let st=$st-1 # set flag fi ping -c1 4.2.2.2 # check target 2 if [ "$?" == 0 ] # if target replied then let st=$st-1 # set flag fi ping -c1 208.67.222.222 # check target 3 if [ "$?" == 0 ] # if target replied then let st=$st-1 # set flag fi if [ "$st" == 3 ] # if all 3 targets failed reply then dt=$(date) # Target failure start date/Time dts=$(date --date="$dt" +%s) # Target failure start date/Time in second ifconfig enp4s0f2 down # Bring wired connection down ifconfig enx000ec669c763 down # Bring USB network interface down sleep 5 # wait 5 seconds ifconfig enp4s0f2 up # Bring wired connection up sleep 5 # wait 5 seconds ifconfig enx000ec669c763 up # Bring USB network interface up sleep 5 # wait 5 seconds else # Not all 3 targets down, if any target recover from failure if [ "$dt" != "" ] # If recover from Internet connection (targets) down then dtn=$(date) # Get now date/Time dtns=$(date --date="$dtn" +%s) # get now date/time in second let dtd=$dtns-$dts # get failure period in second declare -i hh="$dtd / 3600" # get failure hour let dtd=$(( $dtd % 3600 )) # get failure minute and second declare -i hm="dtd / 60" # get failure minute let hs=$(( $dtd % 60 )) # get failure second # # Write Internet connection failure record to log file echo "Target failure start at: " $dt -- period: $hh" hour " $hm" minute " $hs" second " >> /home/kksino/Documents/public/failurelog.txt # Reset variables dt="" dtn="" let dts=0 let dtns=0 fi fi fi sleep 10 # Wait, avoid ping action too frequent # fi # # # done