-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwifi.sh
executable file
·91 lines (86 loc) · 3.49 KB
/
wifi.sh
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
#!/bin/bash
##################################################################
# Author: Kevin Reed (Dweeber)
#
# 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 /home/edison/src/edison_wifi
# o chmod +x /home/edison/src/edison_wifi/wifi.sh
# You may wish to modify line 39 to your desired location.
# o Add to crontab using sudo crontab -e
# 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 ...
#
#
# sudo crontab -e (to run cron from root)
#
# */5 * * * * ~/src/edison_wifi && ./wifi.sh $REMOTE_HOST
#The line below is also meant to run in cron and is supposed to look and see if your home network is available then switch to it if it is. Not sure it works
# */15 * * * * ( (wpa_cli status | grep $YOUR_HOME_NETWORK > /dev/null && echo already on $YOUR_HOME_NETWORK) || (wpa_cli scan > /dev/null && wpa_cli scan_results | egrep $YOUR_HOME_NETWORK > /dev/null && wpa_cli select_network $(wpa_cli list_networks | grep $YOUR_HOME_NETWORK | cut -f 1) && echo switched to $YOUR_HOME_NETWORK && sleep 15 && (for i in $(wpa_cli list_networks | grep DISABLED | cut -f 1); do wpa_cli enable_network $i > /dev/null; done) && echo and re-enabled other networks) ) 2>&1 | logger -t wifi-select
# REMOTE_HOST="google.com" # will be used to test network connectivity
# YOUR_HOME_NETOWRK=ssid_name #change ssid_name to your primary network you want to use. This will switch to that network if it is available.
#################################################################
# Settings
# Where and what you want to call the Lockfile
#lockfile=$(eval echo ~${SUDO_USER})'/src/edison_wifi/WiFi_Check.pid'
lockfile=$'/home/edison/src/edison_wifi/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='wlan0'
pingip=$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 addr:"
echo
# Check is complete, Remove Lock file and exit
#echo "process is complete, removing lockfile"
rm $lockfile
exit 0
##################################################################
# End of Script
##################################################################