Abdul Wajid
Posts: 24
Joined: Tue Dec 18, 2018 7:20 am

Auto Reconnect the connection when Failed[Solved]

Mon Jan 14, 2019 11:12 am

Hi, I am Wajid Here
I want my Ble to scan data from BLE and advertise it to the broker
I have established both the (Wifi and Ethernet) connection and it works fine.

Now I want to backup the connection. In case if connection lost then it should reconnect automatically
For this I am using

Code: Select all

#!/bin/bash

while true ; do
   if ifconfig wlan0 | grep -q "inet addr:" ; then
      sleep 60
   else
      echo "Network connection down! Attempting reconnection."
      ifup --force wlan0
      sleep 10
   fi
done
Source: viewtopic.php?t=16054#p165196
Problem : Even the connection is established the

Code: Select all

RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
RTNETLINK answers: File exists
Network connection down! Attempting reconnection.
^C
It means ifconfig wlan0 | grep -q "inet addr:" does not work..

Would You Guys help me about it OR Help me in finding another methor to check if connection is up or not

Regards,
Last edited by Abdul Wajid on Tue Jan 15, 2019 12:32 pm, edited 1 time in total.

pcmanbob
Posts: 4838
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Auto Reconnect the connection when Failed

Mon Jan 14, 2019 12:02 pm

You are following a 6 year old post things have changed since then, if you had bothered to run ifconfig wlan0 you would have seen that the output has changed it no longer has "inet addr:" at the start of the line, its now "inet" or "inet6"

Code: Select all

pi@zeropi:~ $ ifconfig wlan0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.158  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::ca40:8666:9dd5:6193  prefixlen 64  scopeid 0x20<link>
so try changing the code line to this

Code: Select all

 if ifconfig wlan0 | grep -q "inet" ; then
 
Last edited by pcmanbob on Tue Jan 15, 2019 1:32 pm, edited 1 time in total.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Abdul Wajid
Posts: 24
Joined: Tue Dec 18, 2018 7:20 am

Re: Auto Reconnect the connection when Failed[Solved]

Tue Jan 15, 2019 12:31 pm

Thanks for answering pcmanbob...
I solve it by the code below and it works fine...
It checks for both the ethernet and wifi for more information

https://samhobbs.co.uk/2013/11/fix-for- ... -pi?page=2

Code: Select all

#!/bin/bash

LOGFILE=/home/pi/network-monitor.log

ipe=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)

if [[ "$ipe" =~ ^192.168. ]];
then
        echo "$(date "+%m %d %Y %T") : Ethernet OK" >> $LOGFILE
else
        echo "$(date "+%m %d %Y %T") : Ethernet connection down! Attempting recconnection." >> $LOGFILE
        ifup --force eth0
        OUT=$? #save exit status of last command to decide what to do next
        if [ $OUT -eq 0 ];
        then
                STATE=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
                echo "$(date "+%m %d %Y %T") : Ethernet connection reset. Current State is "$STATE >> $LOGFILE
        else
        		echo "$(date "+%m %d %Y %T") : Failed to reset ethernet connection " >> $LOGFILE
        fi

fi


ipw=$(ip addr show wlan0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)


if [[ "$ipw" =~ ^192.168. ]];
then
        echo "$(date "+%m %d %Y %T") : Wifi OK" >> $LOGFILE
else
        echo "$(date "+%m %d %Y %T") : Wifi connection down! Attempting recconnection." >> $LOGFILE
        ifup --force wlan0
        OUT=$? #save exit status of last command to decide what to do next
        if [ $OUT -eq 0 ];
        then
                STATE=$(ip addr show wlan0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
                echo "$(date "+%m %d %Y %T") : Wifi connection reset. Current State is "$STATE >> $LOGFILE
        else
        		echo "$(date "+%m %d %Y %T") : Failed to reset wifi connection " >> $LOGFILE
        fi

fi

Return to “Beginners”