Skip to content

Commit

Permalink
Update version of autohotspotN 0.97-N/HS-I with mods guysoft/HotSpotOS#6
Browse files Browse the repository at this point in the history
  • Loading branch information
guysoft committed Mar 17, 2022
1 parent 150cf02 commit 1c03b23
Showing 1 changed file with 83 additions and 21 deletions.
104 changes: 83 additions & 21 deletions src/modules/auto-hotspot/filesystem/root/usr/bin/autohotspotN
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#!/bin/bash
#version 0.95-1-N/HS-I
#version 0.97-N/HS-I modded
# Based on tutorial http://www.raspberryconnect.com/network/item/330-raspberry-pi-auto-wifi-hotspot-switch-internet
# Part of CustomPiOS auto-hotspot module

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

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

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

IFSdef=$IFS
cnt=0
#These 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/,$//' | sed 's/\n//g' | sed 's/\r//g')
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/,$//')
IFS=","
ssids=($wpassid)
IFS=$IFSdef #reset back to defaults
Expand All @@ -38,12 +39,24 @@ ssidsmac=("${ssids[@]}" "${mac[@]}") #combines ssid and MAC for checking

createAdHocNetwork()
{
echo "Autohotspot by RaspberryConnect.com"
echo "Creating Hotspot"
ip link set dev "$wifidev" down
ip a add 192.168.50.1/24 brd + dev "$wifidev"
ip link set dev "$wifidev" up
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o "$wifidev" -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i "$wifidev" -o eth0 -j ACCEPT
dhcpcd -k "$wifidev" >/dev/null 2>&1
if iptables 2>&1 | grep 'no command specified' ; then
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
elif nft 2>&1 | grep 'no command specified' ; then
nft add table inet ap
nft add chain inet ap rthrough { type nat hook postrouting priority 0 \; policy accept \; }
nft add rule inet ap rthrough oifname "$ethdev" masquerade
nft add chain inet ap fward { type filter hook forward priority 0 \; policy accept \; }
nft add rule inet ap fward iifname "$ethdev" oifname "$wifidev" ct state established,related accept
nft add rule inet ap fward iifname "$wifidev" oifname "$ethdev" accept
fi
systemctl start dnsmasq
systemctl start hostapd
echo 1 > /proc/sys/net/ipv4/ip_forward
Expand All @@ -52,22 +65,28 @@ createAdHocNetwork()

KillHotspot()
{
echo "Autohotspot by RaspberryConnect.com"
echo "Shutting Down Hotspot"
ip link set dev "$wifidev" down
systemctl stop hostapd
systemctl stop dnsmasq
iptables -D FORWARD -i eth0 -o "$wifidev" -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i "$wifidev" -o eth0 -j ACCEPT
if iptables 2>&1 | grep 'no command specified' ; then
iptables -D FORWARD -i "$ethdev" -o "$wifidev" -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -D FORWARD -i "$wifidev" -o "$ethdev" -j ACCEPT
elif nft 2>&1 | grep 'no command specified' ; then
nft delete table inet ap
fi
echo 0 > /proc/sys/net/ipv4/ip_forward
echo 0 > /proc/sys/net/ipv6/conf/all/forwarding
ip addr flush dev "$wifidev"
ip link set dev "$wifidev" up
dhcpcd -n "$wifidev" >/dev/null 2>&1
}

ChkWifiUp()
{
echo "Checking WiFi connection ok"
sleep 10 #give time for connection to be completed to router
sleep 20 #give 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 wifi (check your wifi settings, password etc)
echo 'Wifi failed to connect, falling back to Hotspot.'
Expand All @@ -76,6 +95,28 @@ ChkWifiUp()
fi
}

chksys()
{
#After some system updates hostapd gets masked using Raspbian Buster, and above. This checks and fixes
#the issue and also checks dnsmasq is ok so the hotspot can be generated.
#Check Hostapd is unmasked and disabled
if systemctl -all list-unit-files hostapd.service | grep "hostapd.service masked" >/dev/null 2>&1 ;then
systemctl unmask hostapd.service >/dev/null 2>&1
fi
if systemctl -all list-unit-files hostapd.service | grep "hostapd.service enabled" >/dev/null 2>&1 ;then
systemctl disable hostapd.service >/dev/null 2>&1
systemctl stop hostapd >/dev/null 2>&1
fi
#Check dnsmasq is disabled
if systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service masked" >/dev/null 2>&1 ;then
systemctl unmask dnsmasq >/dev/null 2>&1
fi
if systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service enabled" >/dev/null 2>&1 ;then
systemctl disable dnsmasq >/dev/null 2>&1
systemctl stop dnsmasq >/dev/null 2>&1
fi
}


FindSSID()
{
Expand All @@ -85,28 +126,49 @@ i=0; j=0
until [ $i -eq 1 ] #wait for wifi if busy, usb wifi is slower.
do
ssidreply=$((iw dev "$wifidev" scan ap-force | egrep "^BSS|SSID:") 2>&1) >/dev/null 2>&1
if echo "$ssidreply" | grep "No such device (-19)" >/dev/null 2>&1; then
#echo "SSid's in range: " $ssidreply
printf '%s\n' "${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 "resource busy (-16)" >/dev/null 2>&1 ;then
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 -v "resource busy (-16)" >/dev/null 2>&1 ; then
echo "Device Available, checking SSid Results"
i=1
elif (($j >= 5)); then #if busy 5 times goto hotspot
ssidreply=""
i=1
else #see if device not busy in 2 seconds
j=$((j = 1))
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
if (echo "$ssidreply" | grep -F -- "$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 Network found, NoSSid issued"
echo "No SSid found, assessing WiFi status"
ssidChk='NoSSid'
fi
done
Expand All @@ -121,10 +183,11 @@ NoDevice()
exit 1
}

chksys
FindSSID

#Create Hotspot or connect to valid wifi networks
if [ "$ssidChk" != "NoSSid" ]
if [ "$ssidChk" != "NoSSid" ]
then
echo 0 > /proc/sys/net/ipv4/ip_forward #deactivate ip forwarding
echo 0 > /proc/sys/net/ipv6/conf/all/forwarding
Expand Down Expand Up @@ -153,9 +216,8 @@ else #ssid or MAC address not in range
ip addr flush "$wifidev"
ip link set dev "$wifidev" down
rm -r /var/run/wpa_supplicant >/dev/null 2>&1
ip link set dev "$wifidev" up
createAdHocNetwork
else #"No SSID, activating Hotspot"
createAdHocNetwork
fi
fi
fi
fi

0 comments on commit 1c03b23

Please sign in to comment.