Skip to content

net wifi check

Raido Pahtma edited this page Aug 13, 2016 · 4 revisions

Get the package

net-check has been packaged - https://github.com/proactivity-lab/net-check

Network connection check and recovery

The following script tries to ping the specified gateway (GATEWAY variable, www.ee in the example) through the specified interface (INTERFACE variable, wlan0 in the example) and restarts the interface if the ping does not succeed. For the example, the script is configured to run every 5 minutes using a crontab. It can be useful for recovering a RaspberryPi from an error with a buggy Wifi driver or fixing network issues in general.

root@machine:~# nano /root/net_check.sh

#!/bin/bash

# http://technotes.nofailkale.com/2014/03/how-to-fix-raspberry-pi-wi-fi.html

SCRIPT_NAME=`basename $0`
LOGGER="/usr/bin/logger"
PING="/bin/ping"
INTERFACE="wlan0"
GATEWAY="www.ee"

function is_gateway_reachable
{
  $PING -w 1 -c 1 -I $INTERFACE $GATEWAY &>/dev/null
  return $?
}

if ! is_gateway_reachable
then
  sleep 20
  if ! is_gateway_reachable
  then
    $LOGGER -s -p cron.error -t $SCRIPT_NAME -i "Could not reach $GATEWAY. Restarting interface $INTERFACE."
    /sbin/ifdown --force $INTERFACE
    $LOGGER -s -p cron.error -t $SCRIPT_NAME -i "ifdown of $INTERFACE returned: $?"
    sleep 5
    /sbin/ifup --force $INTERFACE
    $LOGGER -s -p cron.error -t $SCRIPT_NAME -i "ifup of $INTERFACE returned: $?"
  fi
else
  $LOGGER -s -p cron.info -t $SCRIPT_NAME -i "Network interface $INTERFACE operational."
fi

root@machine:~# crontab -e

# m h  dom mon dow   command
*/5 *  *   *   *     /root/net_check.sh > /dev/null 2>&1

Clone this wiki locally

⌂ 换网址