forked from openenergymonitor/emonpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi-check
executable file
·109 lines (100 loc) · 3.11 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
##################################################################
# A Project of TNET Services, Inc
#
# Title: WiFi_Check
# Author: Kevin Reed (Dweeber)
# Updated: R. Pusztai <[email protected]>
# Project: Raspberry Pi Stuff
#
# Copyright: Copyright (c) 2012 Kevin Reed <[email protected]>
# 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'
##################################################################
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
echo
echo "Starting WiFi check for wlan0"
date
# 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
# only run wifi-check script if wifi network has been configured
if grep -q "network" /etc/wpa_supplicant/wpa_supplicant.conf
then
# We can perform check
echo "Performing network check for wlan0"
wlanstate="$(ifconfig wlan0| sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p')"
echo $wlanstate
if [ ! -z "$wlanstate" ]; then
echo "wlan0 network is up"
else
# Only allow re-connection if the eth0 is not physically connected
# isWireConnected=`cat /sys/class/net/eth0/operstate`
# if [ "$isWireConnected" == "down" ]; then
echo "wlan0 down! Attempting reconnection."
echo "fconfig wlan0 down"
ifconfig wlan0 down
ifdown wlan0
sleep 5
echo "ifconfig wlan0 up"
ifup wlan0
ifconfig wlan0 up
# else
# echo "Network connection down, but not reconnecting because eth0 is connected and configuring device."
# fi
sleep 10
echo
echo "IP addresses"
hostname -I
date
fi
else
echo "WiFi not configured"
fi
echo
# Check is complete, Remove Lock file and exit
echo "process is complete, removing lockfile"
rm $lockfile
exit 0
##################################################################
# End of Script
##################################################################