Monitor wlan0 Interface (Wireless) to avoid disconnection from Raspberry Pi

I had implemented my raspberry pi to use wireless network, unfortunately the connection might after a while disconnect, the only option that I have found was to run a script that check the connection and re-connect if network is down.

 

#!/bin/bash


# CHECKING IF THE LOG FILE IS PRESENT
LOG="./network_monitor.log"

if [ ! -f ${LOG} ]; then

        echo "FILE LOG NOT FOUND"
        touch ${LOG}
fi

NOW=$(date +"%d_%m_%Y_%T")

# INTERFACE
INET="wlan0"

# ADDRESS TO TEST NETWORK -  THIS COULD BE EXTERNAL TARGET
PING_IP="192.168.1.1"

# NETWORK CHECK AND RESET THE CARD IF NEEDED

/bin/ping -c 3 -I ${INET} ${PING_IP} > /dev/null 2> /dev/null

if [ $? -ge 1 ]; then

        echo "${NOW} NETWORK IS DOWN. RESETTING IT" >> ${LOG}
        /sbin/ifdown ${INET}
        sleep 5
        /sbin/ifup --force ${INET}
else
        echo "${NOW} NETWORK IS FINE. NOTHING TO DO. EXITING." >> ${LOG}

fi


Leave a Reply

Your email address will not be published. Required fields are marked *