[solved] - Command interface uptime

Hi guys,

I have a particular situation that my backup internet interface has a lease time of 60 minutes. Off course i do have a script to bypass this somehow, but looking at it from a critical point of view, it could use some optimization i must say.

Currently my script is as follows.

Cronjob #1

  • every minute execute script “wireless.sh”. This script pings the DHCP-server of the router and if there is a reply, nothing needs to be changed. If there is no reply, then DHCP release, renew, interface down, up etc. etc.

Cronjob #2

  • However i had noticed in the past that although the 60 minutes had past and internet did not work anymore, pinging the DHCP-server still did work. So i made another script a bit the same, but in this case i did a ping command to 8.8.8.8 every 30 minutes.

Now one could say, well everything works as it should, but i noticed that that backup WAN interface had a very long uptime going to couple of hours even and sometimes still that WAN interface not working properly.

So now i though of this. give output to see the uptime of the interface, if it is higher than for example 58 minutes or seconds depending on the output, do x, y and z. The script is already done what i only need is the uptime in numbers only without any letters or whatever.

Using /sbin/ifstatus wan | grep uptime you can see the output, but with additional letters. Does anybody know maybe that i could with additional things to the command to ONLY display the integers?

I mean seriously i just don’t understand how come people don’t love Linux :stuck_out_tongue:

/sbin/ifstatus wan | grep -o 'uptime.*' | grep -o '[[:digit:]]*'

In this case i took the wan interface, in the output of ifstatus, it took only the line of “uptime” and it displaced only the numbers in that line.

Case solved. Here is the script.

#!/bin/bash


/sbin/ifstatus wan | grep -o 'uptime.*' | grep -o '[[:digit:]]*'

if [ "$?" -gt 3480 ]
then
        echo "host found"
else
        echo "it does not work"
fi

For testing purposes i used in this case -gt, this is “greater than”, for less, it is -ls (less than). 3480 is seconds…(58 minutes)

My other complete script is here if anybody is interested in it or tweak to their own situation enjoy.

#!/bin/bash

#This is part of a cronjob that executes this script every 19 minutes.
#check if the DHCP-lease time is less than 50 minutes (3000 seconds). If it is more than the output then release everything and ask for a new ipaddress

/sbin/ifstatus wan | grep -o 'uptime.*' | grep -o '[[:digit:]]*'

if [ "$?" -lt 3000 ]
then

else
        ifup wan
        ifconfig wlan2 down
        rm /var/run/wpa_supplicant/wlan2
        ifconfig wlan2 up
        killall wpa_supplicant #Kill the current wpa_supplicant which is running in the background
        /usr/sbin/wpa_supplicant -B -D nl80211 -i wlan2 -c /var/run/wpa_supplicant-wlan2.conf  #Create a new wpa_supplicant process

fi
1 Like

Today i was thinking about this solution, but i guess i forgot something. Although the lease time might not be over, but sometimes i still experienced loss of internet. So the checking if there is still internet connection is still important. I today made some changes to the script.

#!/bin/bash

#This is part of a cronjob that executes this script every 19 minutes.
#It checks if the DHCP-lease time is less than 50 minutes (3000 seconds). If it is more than the output than releases everything and asks for a new ipaddress. If the condition is met (everything is fine), it will do nothing.

######Command First condition#####
WANIF='wan' #Wan-interface you want to check the uptime.
UPTIME="$(/sbin/ifstatus $WANIF | grep -o 'uptime.*' | grep -o '[[:digit:]]'*)"

######Command Second condition####
IP='8.8.8.8' #ipaddress you want to ping
NIC='wlan2'  #networkinteface you want to use for the ping command

ping -I $NIC -c1 $IP 2>/dev/null 1>/dev/null
##########################

if [ "$UPTIME" -lt 3000 ] && [ "$?" = 0 ]
then
echo
else
	ifup $WANIF
	ifconfig wlan2 down
	rm /var/run/wpa_supplicant/wlan2
	ifconfig wlan2 up
	killall wpa_supplicant #Kill the current wpa_supplicant which is running in the background
	/usr/sbin/wpa_supplicant -B -D nl80211 -i wlan2 -c /var/run/wpa_supplicant-wlan2.conf  #Create a new wpa_supplicant process

fi

I couldn’t edit my previous comment, but now i THINK i have optimized it to the max so far. The cronjob will check every 19 minutes if the lease time has not gone over 54 minutes (3240 seconds) which off course the first 2 attempt it will not be the case. However with it, it will also check if there is even a internet connection. The 3rd time it checks it will see that the uptime has surpassed the 54 minutes (3240 seconds) because (3x19 = 57 minutes (3420 seconds). The lease time of the DHCP-server is 60 minutes, so every 57 minutes by default the script will reset everything.

#!/bin/bash

#This is part of a cronjob that executes this script every 19 minutes.
#It checks if the DHCP-lease time is less than 54 minutes (3240 seconds). If it is more than the output than releases everything and asks for a new ipaddress. If the condition is met (everything is fine), it will do nothing.

######Command First condition#####
WANIF='wan' #Wan-interface you want to check the uptime.
UPTIME="$(/sbin/ifstatus $WANIF | grep -o 'uptime.*' | grep -o '[[:digit:]]'*)"

######Command Second condition####
IP='8.8.8.8' #ipaddress you want to ping
NIC='wlan2'  #networkinteface you want to use for the ping command

ping -I $NIC -c1 $IP 2>/dev/null 1>/dev/null
##########################

if [ "$UPTIME" -lt 3240 ] && [ "$?" = 0 ]
then
echo
else
	ifup $WANIF
	ifconfig wlan2 down
	rm /var/run/wpa_supplicant/wlan2
	ifconfig wlan2 up
	killall wpa_supplicant #Kill the current wpa_supplicant which is running in the background
	/usr/sbin/wpa_supplicant -B -D nl80211 -i wlan2 -c /var/run/wpa_supplicant-wlan2.conf  #Create a new wpa_supplicant process

fi