Skip to content

Commit 1c03b23

Browse files
committed
Update version of autohotspotN 0.97-N/HS-I with mods guysoft/HotSpotOS#6
1 parent 150cf02 commit 1c03b23

File tree

1 file changed

+83
-21
lines changed
  • src/modules/auto-hotspot/filesystem/root/usr/bin

1 file changed

+83
-21
lines changed

src/modules/auto-hotspot/filesystem/root/usr/bin/autohotspotN

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
#!/bin/bash
2-
#version 0.95-1-N/HS-I
2+
#version 0.97-N/HS-I modded
33
# Based on tutorial http://www.raspberryconnect.com/network/item/330-raspberry-pi-auto-wifi-hotspot-switch-internet
44
# Part of CustomPiOS auto-hotspot module
55

66
#You may share this script on the condition a reference to RaspberryConnect.com
77
#must be included in copies or derivatives of this script.
88

9-
#Network Wifi & Hotspot with Internet
10-
#A script to switch between a wifi network and an Internet routed Hotspot
9+
#Network Wifi & Hotspot with Network/Internet
10+
#A script to switch between a wifi network and an Network/Internet routed Hotspot
1111
#A Raspberry Pi with a network port required for Internet in hotspot mode.
1212
#Works at startup or with a seperate timer or manually without a reboot
1313
#Other setup required find out more at
1414
#http://www.raspberryconnect.com
1515

1616
wifidev="wlan0" #device name to use. Default is wlan0.
17+
ethdev="eth0" #Ethernet port to use with IP tables
1718
#use the command: iw dev ,to see wifi interface name
1819

1920
IFSdef=$IFS
2021
cnt=0
2122
#These four lines capture the wifi networks the RPi is setup to use
22-
wpassid=$(awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' ORS=',' | sed 's/\"/''/g' | sed 's/,$//' | sed 's/\n//g' | sed 's/\r//g')
23+
wpassid=$(awk '/ssid="/{ print $0 }' /etc/wpa_supplicant/wpa_supplicant.conf | awk -F'ssid=' '{ print $2 }' | sed 's/\r//g'| awk 'BEGIN{ORS=","} {print}' | sed 's/\"/''/g' | sed 's/,$//')
2324
IFS=","
2425
ssids=($wpassid)
2526
IFS=$IFSdef #reset back to defaults
@@ -38,12 +39,24 @@ ssidsmac=("${ssids[@]}" "${mac[@]}") #combines ssid and MAC for checking
3839

3940
createAdHocNetwork()
4041
{
42+
echo "Autohotspot by RaspberryConnect.com"
43+
echo "Creating Hotspot"
4144
ip link set dev "$wifidev" down
4245
ip a add 192.168.50.1/24 brd + dev "$wifidev"
4346
ip link set dev "$wifidev" up
44-
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
45-
iptables -A FORWARD -i eth0 -o "$wifidev" -m state --state RELATED,ESTABLISHED -j ACCEPT
46-
iptables -A FORWARD -i "$wifidev" -o eth0 -j ACCEPT
47+
dhcpcd -k "$wifidev" >/dev/null 2>&1
48+
if iptables 2>&1 | grep 'no command specified' ; then
49+
iptables -t nat -A POSTROUTING -o "$ethdev" -j MASQUERADE
50+
iptables -A FORWARD -i "$ethdev" -o "$wifidev" -m state --state RELATED,ESTABLISHED -j ACCEPT
51+
iptables -A FORWARD -i "$wifidev" -o "$ethdev" -j ACCEPT
52+
elif nft 2>&1 | grep 'no command specified' ; then
53+
nft add table inet ap
54+
nft add chain inet ap rthrough { type nat hook postrouting priority 0 \; policy accept \; }
55+
nft add rule inet ap rthrough oifname "$ethdev" masquerade
56+
nft add chain inet ap fward { type filter hook forward priority 0 \; policy accept \; }
57+
nft add rule inet ap fward iifname "$ethdev" oifname "$wifidev" ct state established,related accept
58+
nft add rule inet ap fward iifname "$wifidev" oifname "$ethdev" accept
59+
fi
4760
systemctl start dnsmasq
4861
systemctl start hostapd
4962
echo 1 > /proc/sys/net/ipv4/ip_forward
@@ -52,22 +65,28 @@ createAdHocNetwork()
5265

5366
KillHotspot()
5467
{
68+
echo "Autohotspot by RaspberryConnect.com"
5569
echo "Shutting Down Hotspot"
5670
ip link set dev "$wifidev" down
5771
systemctl stop hostapd
5872
systemctl stop dnsmasq
59-
iptables -D FORWARD -i eth0 -o "$wifidev" -m state --state RELATED,ESTABLISHED -j ACCEPT
60-
iptables -D FORWARD -i "$wifidev" -o eth0 -j ACCEPT
73+
if iptables 2>&1 | grep 'no command specified' ; then
74+
iptables -D FORWARD -i "$ethdev" -o "$wifidev" -m state --state RELATED,ESTABLISHED -j ACCEPT
75+
iptables -D FORWARD -i "$wifidev" -o "$ethdev" -j ACCEPT
76+
elif nft 2>&1 | grep 'no command specified' ; then
77+
nft delete table inet ap
78+
fi
6179
echo 0 > /proc/sys/net/ipv4/ip_forward
6280
echo 0 > /proc/sys/net/ipv6/conf/all/forwarding
6381
ip addr flush dev "$wifidev"
6482
ip link set dev "$wifidev" up
83+
dhcpcd -n "$wifidev" >/dev/null 2>&1
6584
}
6685

6786
ChkWifiUp()
6887
{
6988
echo "Checking WiFi connection ok"
70-
sleep 10 #give time for connection to be completed to router
89+
sleep 20 #give time for connection to be completed to router
7190
if ! wpa_cli -i "$wifidev" status | grep 'ip_address' >/dev/null 2>&1
7291
then #Failed to connect to wifi (check your wifi settings, password etc)
7392
echo 'Wifi failed to connect, falling back to Hotspot.'
@@ -76,6 +95,28 @@ ChkWifiUp()
7695
fi
7796
}
7897

98+
chksys()
99+
{
100+
#After some system updates hostapd gets masked using Raspbian Buster, and above. This checks and fixes
101+
#the issue and also checks dnsmasq is ok so the hotspot can be generated.
102+
#Check Hostapd is unmasked and disabled
103+
if systemctl -all list-unit-files hostapd.service | grep "hostapd.service masked" >/dev/null 2>&1 ;then
104+
systemctl unmask hostapd.service >/dev/null 2>&1
105+
fi
106+
if systemctl -all list-unit-files hostapd.service | grep "hostapd.service enabled" >/dev/null 2>&1 ;then
107+
systemctl disable hostapd.service >/dev/null 2>&1
108+
systemctl stop hostapd >/dev/null 2>&1
109+
fi
110+
#Check dnsmasq is disabled
111+
if systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service masked" >/dev/null 2>&1 ;then
112+
systemctl unmask dnsmasq >/dev/null 2>&1
113+
fi
114+
if systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service enabled" >/dev/null 2>&1 ;then
115+
systemctl disable dnsmasq >/dev/null 2>&1
116+
systemctl stop dnsmasq >/dev/null 2>&1
117+
fi
118+
}
119+
79120

80121
FindSSID()
81122
{
@@ -85,28 +126,49 @@ i=0; j=0
85126
until [ $i -eq 1 ] #wait for wifi if busy, usb wifi is slower.
86127
do
87128
ssidreply=$((iw dev "$wifidev" scan ap-force | egrep "^BSS|SSID:") 2>&1) >/dev/null 2>&1
88-
if echo "$ssidreply" | grep "No such device (-19)" >/dev/null 2>&1; then
129+
#echo "SSid's in range: " $ssidreply
130+
printf '%s\n' "${ssidreply[@]}"
131+
echo "Device Available Check try " $j
132+
if (($j >= 10)); then #if busy 10 times goto hotspot
133+
echo "Device busy or unavailable 10 times, going to Hotspot"
134+
ssidreply=""
135+
i=1
136+
elif echo "$ssidreply" | grep "No such device (-19)" >/dev/null 2>&1; then
137+
echo "No Device Reported, try " $j
89138
NoDevice
90-
elif ! echo "$ssidreply" | grep "resource busy (-16)" >/dev/null 2>&1 ;then
139+
elif echo "$ssidreply" | grep "Network is down (-100)" >/dev/null 2>&1 ; then
140+
echo "Network Not available, trying again" $j
141+
j=$((j + 1))
142+
sleep 2
143+
elif echo "$ssidreply" | grep "Read-only file system (-30)" >/dev/null 2>&1 ; then
144+
echo "Temporary Read only file system, trying again"
145+
j=$((j + 1))
146+
sleep 2
147+
elif echo "$ssidreply" | grep "Invalid exchange (-52)" >/dev/null 2>&1 ; then
148+
echo "Temporary unavailable, trying again"
149+
j=$((j + 1))
150+
sleep 2
151+
elif echo "$ssidreply" | grep -v "resource busy (-16)" >/dev/null 2>&1 ; then
152+
echo "Device Available, checking SSid Results"
91153
i=1
92-
elif (($j >= 5)); then #if busy 5 times goto hotspot
93-
ssidreply=""
94-
i=1
95154
else #see if device not busy in 2 seconds
96-
j=$((j = 1))
155+
echo "Device unavailable checking again, try " $j
156+
j=$((j + 1))
97157
sleep 2
98158
fi
99159
done
100160

101161
for ssid in "${ssidsmac[@]}"
102162
do
103-
if (echo "$ssidreply" | grep "$ssid") >/dev/null 2>&1
163+
if (echo "$ssidreply" | grep -F -- "$ssid") >/dev/null 2>&1
104164
then
105165
#Valid SSid found, passing to script
166+
echo "Valid SSID Detected, assesing Wifi status"
106167
ssidChk=$ssid
107168
return 0
108169
else
109170
#No Network found, NoSSid issued"
171+
echo "No SSid found, assessing WiFi status"
110172
ssidChk='NoSSid'
111173
fi
112174
done
@@ -121,10 +183,11 @@ NoDevice()
121183
exit 1
122184
}
123185

186+
chksys
124187
FindSSID
125188

126189
#Create Hotspot or connect to valid wifi networks
127-
if [ "$ssidChk" != "NoSSid" ]
190+
if [ "$ssidChk" != "NoSSid" ]
128191
then
129192
echo 0 > /proc/sys/net/ipv4/ip_forward #deactivate ip forwarding
130193
echo 0 > /proc/sys/net/ipv6/conf/all/forwarding
@@ -153,9 +216,8 @@ else #ssid or MAC address not in range
153216
ip addr flush "$wifidev"
154217
ip link set dev "$wifidev" down
155218
rm -r /var/run/wpa_supplicant >/dev/null 2>&1
156-
ip link set dev "$wifidev" up
157219
createAdHocNetwork
158220
else #"No SSID, activating Hotspot"
159221
createAdHocNetwork
160-
fi
161-
fi
222+
fi
223+
fi

0 commit comments

Comments
 (0)