-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswitch-network.sh
executable file
·113 lines (105 loc) · 3.4 KB
/
switch-network.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
##################################################################
# A Project of TNET Services, Inc
# Original verion can be found at:
# https://github.com/dweeber/WiFi_Check
# This version is modified by:
# ExploWare http://raspberrypi.stackexchange.com/users/13296/exploware
#
# 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
#
#
##################################################################
##################################################################
# Settings
# Where and what you want to call the Lockfile
lockfile='/run/lock/WiFi_Check.pid'
# Which Interface do you want to check/fix
wlan='wlan0'
wired='eth0'
PATH=$PATH:/sbin
silent=false
if [[ "$1" == "silent" ]]; then
silent=true
fi
##################################################################
#echo
$silent || echo -n "Starting WiFi check for $wlan"
#echo
SRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source $SRC_DIR/network-functions.sh
logger "pwd = " `pwd`
logger "path = $PATH"
logger $SRC_DIR
# 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"
$silent || echo -n .
exit 1
else
# Old Lockfile, Remove it
#echo "Old lockfile, Removing Lockfile"
$silent || echo -n .
rm $lockfile
sleep .5
fi
else
$silent || echo -n .
fi
# If we get here, set a lock file using our current PID#
#echo "Setting Lockfile"
$silent || echo -n .
echo $$ > $lockfile
# We can perform check
#echo "Performing Network check for $wlan"
if iwconfig $wlan|grep -qe "ESSID:\".*\""; then
echo "ESSID found"
logger "ESSID found"
if ifconfig $wlan | grep -q "inet" ; then
logger "Wifi connection on $wlan is OK"
hotspot_down
else
$silent || echo "Network connection down! Attempting reconnection."
logger "Wifi connection on $wlan is DOWN, trying to reconnect... "
date
wlan_reconnect
if ifconfig $wlan | grep -q "inet" ; then
logger "Wifi connection on $wlan seems to be up and running again."
hotspot_down
else
logger "Wifi connection on $wlan is DOWN"
hotspot_up
fi
fi
else
$silent || logger "Network connection down!!!! Attempting reconnection."
logger "Wifi connection on $wlan is DOWN, trying to reconnect... "
date
wlan_reconnect
if ifconfig $wlan | grep -q "inet" ; then
logger Wifi connection on $wlan seems to be up and running again.
hotspot_down
else
logger Wifi connection on $wlan is DOWN, enabling the hotspot ...
hotspot_up
fi
fi
$silent || echo -n "Current Setting:"
$silent || iwconfig $wlan | grep -oe "ESSID:\".*\"";ifconfig $wlan | grep "inet"
logger `iwconfig $wlan | grep -oe "ESSID:\".*\"";ifconfig $wlan | grep "inet"`
# Check is complete, Remove Lock file and exit
#echo "process is complete, removing lockfile"
rm $lockfile
##################################################################
# End of Script
##################################################################