Sonntag, 15. März 2015

Raspberry PI auto reconnect des WLAN Interface

Mein Raspberry kann das WLAN-Interface nach dem starten meines Roters nicht automatisch verbinden. Die Idee ist nun ein Cron-Job zu erzeigen der alle 5 min. prüft ob der Router noch erreichbar ist.
Von hier http://harizanov.com/2013/04/auto-reconnect-wifi-on-raspberry-pi/ habe ich das folgende Script. 
#!/bin/bash
##################################################################
# A Project of TNET Services, Inc
#
# Title: WiFi_Check
# Author: Kevin Reed (Dweeber)
# dweeber.dweebs@gmail.com
# Project: Raspberry Pi Stuff
#
# Copyright: Copyright (c) 2012 Kevin Reed <kreed@tnet.com>
# https://github.com/dweeber/WiFi_Check
#
# Purpose:
#
# Script checks to see if WiFi has a network IP and if not
# restart WiFi
#
# Uses a lock file which prevents the script from running more
# than one at a time. If lockfile is old, it removes it
#
# Instructions:
#`
# o Install where you want to run it from like /usr/local/bin
# o chmod 0755 /usr/local/bin/WiFi_Check
# o Add to crontab
#
# Run Every 5 mins - Seems like ever min is over kill unless
# this is a very common problem. If once a min change */5 to *
# once every 2 mins */5 to */2 ...
#
# */5 * * * * /usr/local/bin/WiFi_Check
#
##################################################################
# Settings
# Where and what you want to call the Lockfile
lockfile='/var/run/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='ra0'
pingip='192.168.2.1'
##################################################################
echo
echo "Starting WiFi check for $wlan"
date
echo
# Check to see if there is a lock file
if [ -e $lockfile ]; then
# A lockfile exists... Lets check to see if it is still valid
pid=`cat $lockfile`
if kill -0 &>1 > /dev/null $pid; then
# Still Valid... lets let it be...
#echo "Process still running, Lockfile valid"
exit 1
else
# Old Lockfile, Remove it
#echo "Old lockfile, Removing Lockfile"
rm $lockfile
fi
fi
# If we get here, set a lock file using our current PID#
#echo "Setting Lockfile"
echo $ > $lockfile
# We can perform check
echo "Performing Network check for $wlan"
/bin/ping -c 2 -I $wlan $pingip > /dev/null 2> /dev/null
if [ $? -ge 1 ] ; then
echo "Network connection down! Attempting reconnection."
/sbin/ifdown $wlan
/bin/sleep 5
/sbin/ifup --force $wlan
else
echo "Network is Okay"
fi
echo
echo "Current Setting:"
/sbin/ifconfig $wlan | grep "inet Adresse:"
echo
# Check is complete, Remove Lock file and exit
#echo "process is complete, removing lockfile"
rm $lockfile
exit 0
##################################################################
# End of Script
##################################################################

In meinem Fall musste ich wlan0 durch ra0 ersetzten. Mit chmod +x das script ausführbar machen.
Das Script wird dann mit sudo crontab -e -u root eingetragen. Standardmäßig ist das Logging von cron nicht aktiviert. Das lässt sich mit sudo nano /etc/rsyslog.conf durch entfernen des # vor cron.* ändern. Nun wird in der Datei /var/log/cron.log nach einem sudo /etc/init.d/rsyslog restart geloggt.
Aktuell schreibe ich mit /5 * * * * /usr/local/bin/WiFi_Check.sh >> /home/pi/WiFi_Check.log in eine log Datei. Sowohl das Cron log als auch das Script log habe ich nach erfolgreichem Testen ausgeschaltet.
Das Script läuft nun schon seit Wochen ohne jedes Problem.

Keine Kommentare:

Kommentar veröffentlichen