forked from openenergymonitor/emonpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi-check
executable file
·95 lines (90 loc) · 2.8 KB
/
wifi-check
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
##################################################################
# A Project of TNET Services, Inc
#
# Title: WiFi_Check
# Author: Kevin Reed (Dweeber)
# dweeber.dweebs@gmail.com
# Updated: R. Pusztai <rjpcomputing@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='/tmp/wifi-check.pid'
# Which Interface do you want to check/fix
wlan='wlan0'
##################################################################
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
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"
if ifconfig $wlan | grep -q "inet addr:" ; then
echo "Network is Okay"
else
# Only allow re-connection if the eth0 is not physically connected
# isWireConnected=`cat /sys/class/net/eth0/operstate`
# if [ "$isWireConnected" == "down" ]; then
# echo "Network connection down! Attempting reconnection."
ifdown $wlan
sleep 5
ifup --force $wlan
ifconfig $wlan | grep "inet addr"
# else
# echo "Network connection down, but not reconnecting because eth0 is connected and configuring device."
# fi
fi
echo
echo "Current Setting:"
ifconfig $wlan | grep "inet addr:"
echo
# Check is complete, Remove Lock file and exit
#echo "process is complete, removing lockfile"
rm $lockfile
exit 0
##################################################################
# End of Script
##################################################################