-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautohotspot
executable file
·161 lines (149 loc) · 5.43 KB
/
autohotspot
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
# Based on autohotspot script version 0.95-41-N/HS-I from RaspberryConnect.com
# See full details at https://www.raspberryconnect.com/projects/65-raspberrypi-hotspot-accesspoints/157-raspberry-pi-auto-wifi-hotspot-switch-internet
wifidev="wlan0" # Wifi device name
ethdev="eth0" # Ethernet device to use with IP tables (IP forwarding for bridged-wifi-to-ethernet setup)
IFSdef=$IFS
cnt=0
# The next four lines capture the wifi networks the RPi is setup to use
wpassid=$(awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' ORS=',' | sed 's/\"/''/g' | sed 's/,$//')
wpassid=$(echo "${wpassid//[$'\r\n']}")
IFS=","
ssids=($wpassid)
IFS=$IFSdef # Reset back to defaults
mac=()
ssidsmac=("${ssids[@]}" "${mac[@]}") # Combines SSID and MAC for checking
createAdHocNetwork()
{
echo "Creating local hotspot"
ip link set dev "$wifidev" down
ip a add 192.168.50.5/24 brd + dev "$wifidev"
ip link set dev "$wifidev" up
dhcpcd -k "$wifidev" >/dev/null 2>&1
iptables -t nat -A POSTROUTING -o "$ethdev" -j MASQUERADE
iptables -A FORWARD -i "$ethdev" -o "$wifidev" -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i "$wifidev" -o "$ethdev" -j ACCEPT
systemctl start dnsmasq
systemctl start hostapd
echo 1 > /proc/sys/net/ipv4/ip_forward
}
KillHotspot()
{
echo "Shutting down local hotspot"
ip link set dev "$wifidev" down
systemctl stop hostapd
systemctl stop dnsmasq
iptables -D FORWARD -i "$ethdev" -o "$wifidev" -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i "$wifidev" -o "$ethdev" -j ACCEPT
echo 0 > /proc/sys/net/ipv4/ip_forward
ip addr flush dev "$wifidev"
ip link set dev "$wifidev" up
dhcpcd -n "$wifidev" >/dev/null 2>&1
}
ChkWifiUp()
{
echo "Checking wifi connection"
sleep 20 # Give enough time for connection to be completed to router
if ! wpa_cli -i "$wifidev" status | grep 'ip_address' >/dev/null 2>&1
then # Failed to connect to a known wifi network (known wifi network not in range)
echo 'Wifi failed to connect, falling back to local hotspot'
wpa_cli terminate "$wifidev" >/dev/null 2>&1
createAdHocNetwork
fi
}
FindSSID()
{
# Check to see what SSID's and MAC addresses are in range
ssidChk=('NoSSid')
i=0; j=0
until [ $i -eq 1 ] # Wait for wifi if busy
do
ssidreply=$((iw dev "$wifidev" scan ap-force | egrep "^BSS|SSID:") 2>&1) >/dev/null 2>&1
echo "SSid's in range: " $ssidreply
echo "Device Available Check try " $j
if (($j >= 10)); then #if busy 10 times goto hotspot
echo "Device busy or unavailable 10 times, going to Hotspot"
ssidreply=""
i=1
elif echo "$ssidreply" | grep "No such device (-19)" >/dev/null 2>&1; then
echo "No Device Reported, try " $j
NoDevice
elif echo "$ssidreply" | grep "Network is down (-100)" >/dev/null 2>&1 ; then
echo "Network Not available, trying again" $j
j=$((j + 1))
sleep 2
elif echo "$ssidreply" | grep "Read-only file system (-30)" >/dev/null 2>&1 ; then
echo "Temporary Read only file system, trying again"
j=$((j + 1))
sleep 2
elif echo "$ssidreply" | grep "Invalid exchange (-52)" >/dev/null 2>&1 ; then
echo "Temporary unavailable, trying again"
j=$((j + 1))
sleep 2
elif ! echo "$ssidreply" | grep "resource busy (-16)" >/dev/null 2>&1 ; then
echo "Device Available, checking SSid Results"
i=1
else #see if device not busy in 2 seconds
echo "Device unavailable checking again, try " $j
j=$((j + 1))
sleep 2
fi
done
for ssid in "${ssidsmac[@]}"
do
if (echo "$ssidreply" | grep "$ssid") >/dev/null 2>&1
then
# Valid SSID found, passing to script
echo "Valid SSID detected, assesing wifi status"
ssidChk=$ssid
return 0
else
# No valid SSID found
echo "No SSID found, assessing wifi status"
ssidChk='NoSSid'
fi
done
}
NoDevice()
{
# If no wifi device, activate wifi so it will be available when it is reconnected
echo "No wifi device connected"
wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1
exit 1
}
FindSSID
# Create local hotspot or connect to valid wifi network
if [ "$ssidChk" != "NoSSid" ]
then
echo 0 > /proc/sys/net/ipv4/ip_forward # Deactivate IP forwarding
if systemctl status hostapd | grep "(running)" >/dev/null 2>&1
then # Hotspot running and SSID in range
KillHotspot
echo "Hotspot deactivated, bringing wifi up"
wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1
ChkWifiUp
elif { wpa_cli -i "$wifidev" status | grep 'ip_address'; } >/dev/null 2>&1
then # Already connected
echo "Already connected to an existing network"
else # SSID exists and no hotspot running --> connect to wifi network
echo "Connecting to the wifi network"
wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1
ChkWifiUp
fi
else # SSID or MAC address not in range
if systemctl status hostapd | grep "(running)" >/dev/null 2>&1
then
echo "Hostspot already active"
elif { wpa_cli status | grep "$wifidev"; } >/dev/null 2>&1
then
echo "Cleaning wifi files and activating local hotspot"
wpa_cli terminate >/dev/null 2>&1
ip addr flush "$wifidev"
ip link set dev "$wifidev" down
rm -r /var/run/wpa_supplicant >/dev/null 2>&1
createAdHocNetwork
else
echo "No valid SSID found, activating local hotspot"
createAdHocNetwork
fi
fi