diff --git a/sbin/Autohotspot/ChangeLog-Autohotspot-Setup.txt b/sbin/Autohotspot/ChangeLog-Autohotspot-Setup.txt new file mode 100644 index 0000000..94c1ce7 --- /dev/null +++ b/sbin/Autohotspot/ChangeLog-Autohotspot-Setup.txt @@ -0,0 +1,3 @@ +Change log for autohotspot-setup.sh + +0.71 First release 19 Apr 2020 diff --git a/sbin/Autohotspot/ChangeLog-Autohotspot.txt b/sbin/Autohotspot/ChangeLog-Autohotspot.txt new file mode 100644 index 0000000..c18cd5f --- /dev/null +++ b/sbin/Autohotspot/ChangeLog-Autohotspot.txt @@ -0,0 +1,9 @@ +Change log for autohotspot and autohotspotN scripts + +version 0.95-61-N/HS-I & version 0.96-N/HS + +-Added error checking for status "Invalid exchange (-52) +-If the wpa_supplicant.conf file was created in windows then it can contain Cr characters. These caused the ssid not to match so the hotspot was generated. +SSID matching now filters the Cr. +-Script output of local SSID's and BSS's in list to be read easier. +-From Raspbian Buster hostapd is masked by default. If the Autohotspot setup has been done before the first system update, hostapd can get remasked. As this may happen in future updates the Autohotspot/N scripts check for Masked & enabled hostapd and dnsmasq services and disables/stops them before the script chooses network or hotspot. diff --git a/sbin/Autohotspot/ReadMe.txt b/sbin/Autohotspot/ReadMe.txt new file mode 100644 index 0000000..4701349 --- /dev/null +++ b/sbin/Autohotspot/ReadMe.txt @@ -0,0 +1,111 @@ +RaspberryConnect.com +This installer script, the autohotspot and autohotspotN scripts can be shared and modified but all references to RaspberryConnect.com must be kept in place. + +AutoHotspot Setups: +This script is for installing a Raspberry Pi WiFi setup where the Pi will connect to a previously configured Wifi network when the Pi is in range of the router or Automatically setup a Raspberry Pi Hotspot/access point when a known wifi network is not in range. +This can also be run manually or with a timer to switch without a reboot. + +This is available in two setups: +1: Internet access available for connected devices when a Ethernet cable is connected for the Raspberry Pi's 3A,3B,3B+ & 4. For Rapberry Pi's A,B, B+,& 2 if an usb Wifi adapter is used. +2: No internet access for connected devices. Designed for the Raspberry Pi Zero W or other Raspberry PI's where only a direct connection to the PI from a phone, tablet or Laptop is required. + +There is also setup for permanent hotspot with internet access for connected devices. + +For more information and for the manual setup's see: +https://www.raspberryconnect.com/projects/65-raspberrypi-hotspot-accesspoints/157-raspberry-pi-auto-wifi-hotspot-switch-internet +https://www.raspberryconnect.com/projects/65-raspberrypi-hotspot-accesspoints/158-raspberry-pi-auto-wifi-hotspot-switch-direct-connection +https://www.raspberryconnect.com/projects/65-raspberrypi-hotspot-accesspoints/168-raspberry-pi-hotspot-access-point-dhcpcd-method + +This script will install any of these three setups or allow you to change between setup types. +In addition the Hotspot SSID and Password can be changed, new WiFi networks can be added to the Raspberry PI while in Hotspot Mode. The Pi can be forced between Network mode and Hotspot mode without a reboot. Also there is an uninstaller. + +To use the installer: + +Download the AutoHotspot-Setup.tar.xz archive from +curl "https://www.raspberryconnect.com/images/hsinstaller/AutoHotspot-Setup.tar.gz" -o AutoHotspot-Setup.tar.gz + +Unarchive the file to the curent folder using the command +tar -xzvf AutoHotspot-Setup.tar.xz +If you are using the Desktop then right click on the AutoHotspot-Setup.tar.xz and select Extract Here + +open a terminal screen and navigate to the Autohotspot folder. If this is in your home directory then use +cd Autohotspot +if this is your desktop then use +cd ./Desktop/Autohotspot +Run the script with the command +sudo ./autohotspot-setup.sh +This script will fail if sudo is not used. + +You will presented with a menu with these options + + 1 = Install Autohotspot with Internet for Connected Devices + 2 = Install Autohotspot with No Internet for connected devices + 3 = Install a Permanent Hotspot with Internet for connected devices + 4 = Uninstall Autohotspot or Permanent Hotspot + 5 = Add or Change a WiFi network (SSID) + 6 = Autohotspot: Force to a Hotspot or Force to Network if SSID in Range + 7 = Change the Hotspots SSID and Password + 8 = Exit + +Option 1: Install Autohotspot with Internet for Connected Devices +Once installed and after a reboot the Raspberry Pi will connect to a router that has previously been connected to and is listed in /etc/wpa_supplicant/wpa_supplicant.conf. If no router is in range then it will generate a WiFi hotspot. +This will have an SSID of RPiHotspot and password of 1234567890 +Use option 7 to change the password and also the SSID if required +If an ethernet cable is connected to the Pi with access to the internet then it will allow devices connected to the hotspot access to the internet or local network. +Once a connection to the hotspot has been made you can access the Raspberry Pi via ssh & VNC with +ssh pi@192.168.50.5 +vnc: 192.168.50.5::5900 +for webservers use http://192.168.50.5/ + +Option 2: Install Autohotspot with No Internet for connected devices +This option is similar to option 1 but connected devices have no internet connection if an ethernet cable is connected. +This has been designed so you can access only the Pi from a Laptop, tablet or phone. +The hotspot SSID will be RPiHotspot with a password of 1234567890 +Once a connection to the hotspot has been made you can access the Raspberry Pi via ssh & VNC with +ssh pi@10.0.0.5 +vnc: 10.0.0.5::5900 +for webservers use http://10.0.0.5/ + +Otion 3: Install a Permanent Hotspot with Internet for connected devices +This is for a permanent WiFi hotspot with internet access for connected devices. +The Raspberry Pi will only have network or internet access when an ethernet cable is connected. +Once a connection to the hotspot has been made you can access the Raspberry Pi via ssh & VNC with +ssh pi@192.168.50.10 +vnc: 192.168.50.10::5900 +for webservers use http://192.168.50.10/ + +Additional setup is required if you wanted to use a second WiFi device to connect to the internet rather than a ethernet conection. This will be a future option. + +Option 4: Uninstall Autohotspot or Permanent Hotspot +This will disable the setup of any of the three setups and return the Raspberry Pi to default Wifi settings. +Hostapd & dnsmasq will not be uninstalled just disabled. + +Option 5: Add or Change a WiFi network (SSID) +If you are using either of the autohotspot setups in hotspot modes and wish to connect to a local WiFi network. You will be unable to scan for any networks as the desktop wifi option will be disabled, shown as red crosses. You can manually add the details to /etc/wpa_supplicant/wpa_supplicant.conf if you know them. +This option will allow you to scan for local WiFi networks and update the Pi. If you then reboot or use the Force... option ,see below. +This option only works for WiFi networks where only a password is required. If a username is required this will not work. (Future update) + +Option 6: Autohotspot: Force to a Hotspot or Force to Network if SSID in Range +This option is only for the Autohotspot setups. +If you are at home and connected to your home network but would like to use the hotspot. This option will force the pi to hotspot mode and will ignore your home network untill the next reboot. If you use this option again while in hotspot mode it will attempt to connect to a known network. This will go back to the hotspot if no valid WiFi network is found or there is a connection issue. + +Option 7: Change the Hotspots SSID and Password +By default the hotspot ssid is RPiHotSpot with a password of 1234567890. Use this option to change either or both SSID and Password. +You will be prompted to change both but if you make no entry and press enter the existing setting will be kept. +The password must be at least 8 characters. + +Option 8: Exit +Exit the script. + + +NFtables Warning. +This setup uses iptables for routing the Hotspots to the internet, options 1 & 3. From Raspbian 10 Buster, IPtables are depreciated in place of NFtables. By default NFtables will handle IPtable rules. +If you know you are using NFtables for a Firewall or other routing rules then don't use the internet routed hotspots and only use option 2, Autohotspot without internet. Otherwise there will be a conflict with your routing tables. +NFtables will be implemented shortly in the next main update of the installer and scripts. +Raspbian Stretch (9) & Jessie (8) only use IPtables. +You will be warned when you initally run the installer script if NFtables are active. + +/etc/network/interfaces file: +many older hotspot and network setup guides online add entries to the /etc/network/interfaces file. This file is depreciated in Raspbian and any entry in this file is not compatible with these setups. This installer backup and remove any entries found in this file. They will be restored if the uninstall option is used. + +RaspberryConnect.com diff --git a/sbin/Autohotspot/autohotspot-setup.sh b/sbin/Autohotspot/autohotspot-setup.sh new file mode 100755 index 0000000..b58d458 --- /dev/null +++ b/sbin/Autohotspot/autohotspot-setup.sh @@ -0,0 +1,693 @@ +#!/bin/bash +#RaspberryConnect.com +#This installer can be shared but all references to RaspberryConnect.com in this file +#and other files used by the installer should remain in place. + +#Installer version 0.73 (25 Apr 2020) +#Installer for AutoHotspot, AutohotspotN scripts and Static Hotspot setup. +#Autohotspot: a script that allows the Raspberry Pi to switch between Network Wifi and +#a hotspot either at bootup or with a timer without a reboot. + +#This installer script will alter network settings and may overwrite existing settings if allowed. +#/etc/hostapd/hostapd.conf (backup old), /etc/dnsmasq.conf (backup old), modifies /etc/dhcpcd.conf (modifies) +#/etc/sysctl.conf (modifies), /etc/network/interfaces (backup old & removes any network entries) +#Currently iptables are used, you will receive a warning if nftables active active. +#If you are using nftables please only use option 2, Autohotspot-Non Internet script until a future update. + +#Force Hotspot or Network Wifi option will only work if either autohotspot is installed and active. + + +#Check for Raspbian and version. +osver=($(cat /etc/issue)) +cpath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +opt="X" +vhostapd="N" vdnsmasq="N" autoH="N" +autoserv="N" iptble="N" nftble="N" + +if [ "${osver[0]}" != 'Raspbian' ]; then + echo "This AutoHotspot installer is only for the OS Raspbian on the Raspberry Pi" + exit 1 +elif [ "${osver[2]}" -ge 10 ]; then + echo 'Raspbian Version' "${osver[2]}" +elif [ "${osver[2]}" -lt 8 ];then + echo "The version of Raspbian is too old for the Autohotspot script" + echo "Version 8 'Jessie' is the minimum requirement" +fi + +check_installed() +{ + #check if required software is already installed + if dpkg -s "hostapd" | grep 'Status: install ok installed' >/dev/null 2>&1; then + vhostapd="Y" + fi + if dpkg -s "dnsmasq" | grep 'Status: install ok installed' >/dev/null 2>&1; then + vdnsmasq="Y" + fi + #Does an Autohotspot files exist + if ls /usr/bin/ | grep "autohotspot*" >/dev/null 2>&1 ; then + autoH="Y" + fi + if ls /etc/systemd/system/ | grep "autohotspot.service" >/dev/null 2>&1 ; then + autoserv="Y" + fi + if dpkg -s "iptables" >/dev/null 2>&1 ; then + iptble="Y" + fi + if dpkg -s "nftables" >/dev/null 2>&1 ; then + nftble="Y" + fi +} + +check_reqfiles() +{ + fstatus=0 + cd "${cpath}/config/" + if test -f "Checklist.md5" ;then + if ! md5sum -c --quiet Checklist.md5 ;then + echo "one or more of the required files in the config folder are missing or have been altered" + echo "please download the installer again from RaspberryConnect.com" + exit + fi + else + echo "The file Checklist.md5 is missing from Config folder" + echo "Please download the installer again" + echo "from RaspberryConnect.com" + exit + fi + +} + +check_wificountry() +{ + #echo "Checking WiFi country" + wpa=($(cat "/etc/wpa_supplicant/wpa_supplicant.conf" | grep "country=")) + if [ -z ${wpa: -2} ] || [[ ${wpa: -2} == *"="* ]];then + echo "The WiFi country has not been set. This is required for the hotspot setup." + echo "Please update Raspbian with the wifi country using the command 'sudo raspi-config' and choose the localisation menu" + echo "From the desktop this can be done in the menu Preferences - Raspberry Pi Configuration - Localisation" + echo "Once done please try again." + echo "" + echo "press a key to continue" + read + fi +} + + + +hostapd_config() +{ + echo "hostapd Config" + echo "Hostapd Status is " $vhostapd + if [ "$vhostapd" = "N" ]; then + echo "Hostapd not installed- now installing" + apt -q install hostapd + echo "Recheck install Status" + check_installed + if [ "$vhostapd" = "N" ]; then + echo "" + echo "" + echo "Hostapd failed to install. Check there is internet access" + echo "and try again" + echo "Press a key to continue" + read + menu + fi + fi + echo "Hostapd is installed" + if ! grep -F "RaspberryConnect.com" "/etc/hostapd/hostapd.conf" ;then + #not a autohotspot file, create backup + mv "/etc/hostapd/hostapd.conf" "/etc/hostapd/hostapd-RCbackup.conf" + fi + cp "$cpath/config/hostapd.conf" /etc/hostapd/hostapd.conf + if [ "${osver[2]}" -lt 10 ]; then + cp "$cpath/config/hostapd" /etc/default/hostapd + fi + if [ "$opt" = "AHN" ] || [ "$opt" = "AHD" ]; then + #For Autohotspots + echo "Unmask & Disable Hostapd" + if systemctl -all list-unit-files hostapd.service | grep "hostapd.service masked" ;then + systemctl unmask hostapd.service >/dev/null 2>&1 + fi + if systemctl -all list-unit-files hostapd.service | grep "hostapd.service enabled" ;then + systemctl disable hostapd.service >/dev/null 2>&1 + fi + elif [ "$opt" = "SHS" ]; then + #for Static Hotspot + echo "Unmask and enable hostapd" + if systemctl -all list-unit-files hostapd.service | grep "hostapd.service masked" ;then + systemctl unmask hostapd >/dev/null 2>&1 + fi + if systemctl -all list-unit-files hostapd.service | grep "hostapd.service disabled" ;then + systemctl enable hostapd >/dev/null 2>&1 + fi + elif [ "$opt" = "REM" ]; then + if [ -f "/etc/hostapd/hostapd-RCbackup.conf" ] ; then + mv "/etc/hostapd/hostapd-RCbackup.conf" "/etc/hostapd/hostapd.conf" + fi + fi + #check country code for hostapd.conf + wpa=($(cat "/etc/wpa_supplicant/wpa_supplicant.conf" | grep "country=")) + hapd=($(cat "/etc/hostapd/hostapd.conf" | grep "country_code=")) + if [[ ! ${wpa: -2} == ${hapd: -2} ]] ; then + echo "Changing Hostapd Wifi country to " ${wpa: -2} + sed -i -e "/country_code=/c\country_code=${wpa: -2}" /etc/hostapd/hostapd.conf + fi +} + +dnsmasq_config() +{ + echo "Dnsmasq Config" + if [ "$vdnsmasq" = "N" ]; then + apt -q install dnsmasq + check_installed + if [ "$vdnsmasq" = "N" ]; then + echo "" + echo "" + echo "dnsmasq failed to install. Check there is internet access" + echo "and try again" + echo "Press a key to continue" + read + menu + fi + fi + if [ -f "/etc/dnsmasq.conf" ] ; then + if ! grep -F "RaspberryConnect.com" "/etc/dnsmasq.conf" ;then + #not a autohotspot file, create backup + mv "/etc/dnsmasq.conf" "/etc/dnsmasq-RCbackup.conf" + fi + fi + if [ "$opt" = "AHN" ] ; then + echo "${cpath}/config/dnsmasqAHSN.conf" + cp "${cpath}/config/dnsmasqAHSN.conf" "/etc/dnsmasq.conf" + elif [ "$opt" = "AHD" ];then + cp "${cpath}/config/dnsmasqAHS.conf" "/etc/dnsmasq.conf" + elif [ "$opt" = "SHS" ] ;then + cp "${cpath}/config/dnsmasqSHS.conf" "/etc/dnsmasq.conf" + fi + if [ "$opt" = "AHN" ] || [ "$opt" = "AHD" ]; then + #For Autohotspots + echo "Unmask & Disable Dnsmasq" + if systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service masked" ;then + systemctl unmask dnsmasq >/dev/null 2>&1 + fi + if systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service enabled" ;then + systemctl disable dnsmasq >/dev/null 2>&1 + fi + elif [ "$opt" = "SHS" ]; then + #for Static Hotspot + echo "Unmask & Enable Dnsmasq" + if systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service masked" ;then + systemctl unmask dnsmasq >/dev/null 2>&1 + fi + if systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service disabled" ;then + systemctl enable dnsmasq >/dev/null 2>&1 + fi + fi + if [ "$opt" = "REM" ]; then + if [ -f "/etc/dnsmasq-RCbackup.conf" ] ; then + mv "/etc/dnsmasq-RCbackup.conf" "/etc/dnsmasq.conf" + fi + fi + +} +dhcpcd_config() +{ + if [ "$opt" = "AHN" ] || [ "$opt" = "AHD" ] ;then + grep -vxf "${cpath}/config/dhcpcd-remove.conf" "/etc/dhcpcd.conf" > "${cpath}/config/Ndhcpcd.conf" + cat "${cpath}/config/dhcpcd-autohs.conf" >> "${cpath}/config/Ndhcpcd.conf" + mv "${cpath}/config/Ndhcpcd.conf" "/etc/dhcpcd.conf" + elif [ "$opt" = "SHS" ]; then + grep -vxf "${cpath}/config/dhcpcd-remove.conf" "/etc/dhcpcd.conf" > "${cpath}/config/Ndhcpcd.conf" + cat "${cpath}/config/dhcpcd-SHSN.conf" >> "${cpath}/config/Ndhcpcd.conf" + mv "${cpath}/config/Ndhcpcd.conf" "/etc/dhcpcd.conf" + + fi +} + +sysctl() +{ + if [ "$opt" = "AHN" ] || [ "$opt" = "SHS" ] ;then + sed -i -e "/#net.ipv4.ip_forward=1/c\net.ipv4.ip_forward=1" /etc/sysctl.conf + elif [ "$opt" = "AHD" ] || [ "$opt" = "REM" ] ;then + sed -i -e "/net.ipv4.ip_forward=1/c\#net.ipv4.ip_forward=1" /etc/sysctl.conf + fi +} + +auto_service() +{ + if [ "$opt" = "AHN" ] ;then + cp "${cpath}/config/autohotspot-Net.service" "/etc/systemd/system/autohotspot.service" + systemctl daemon-reload + systemctl enable autohotspot + elif [ "$opt" = "AHD" ] ;then + cp "${cpath}/config/autohotspot-direct.service" "/etc/systemd/system/autohotspot.service" + systemctl daemon-reload + systemctl enable autohotspot + fi + if [ "$opt" = "REM" ] || [ "$opt" = "SHS" ]; then + if systemctl -all list-unit-files autohotspot.service | grep "autohotspot.service enabled" ;then + systemctl disable autohotspot.service + fi + if [ -f "/etc/systemd/system/autohotspot.service" ]; then + rm /etc/systemd/system/autohotspot.service + fi + fi + +} +hs_routing() +{ + if [ "$opt" = "SHS" ] ;then + if [ "$iptble" = "Y" ] ; then + if [ ! -f "/etc/systemd/system/hs-iptables.service" ];then + cp "${cpath}/config/hs-iptables.service" "/etc/systemd/system/hs-iptables.service" + fi + if systemctl -all list-unit-files hs-iptables.service | grep "hs-iptables.service enabled" ;then + systemctl daemon-reload + fi + if systemctl -all list-unit-files hs-iptables.service | grep "hs-iptables.service disabled" ;then + systemctl enable hs-iptables.service + fi + if [ ! -f "/etc/iptables-hs" ] ;then + cp "${cpath}/config/iptables-hs.txt" "/etc/iptables-hs" + chmod +x "/etc/iptables-hs" + fi + + elif [ "$nftble" = "Y" ] ; then + echo "future feature" + + fi + elif [ "$opt" = "REM" ] || [ "$opt" = "AHN" ] || [ "$opt" = "AHD" ] ; then + if systemctl is-active hs-iptables | grep -w "active" ;then + systemctl disable hs-iptables.service + fi + if test -f "/etc/systemd/system/hs-iptables.service" ; then + rm /etc/systemd/system/hs-iptables.service + fi + if test -f "/etc/iptables-hs" ; then + rm /etc/iptables-hs + fi + fi +} + +auto_script() +{ + if [ "$opt" = "AHN" ] ;then + cp "${cpath}/config/autohotspotN" "/usr/bin/autohotspotN" + chmod +x /usr/bin/autohotspotN + elif [ "$opt" = "AHD" ] ;then + cp "${cpath}/config/autohotspot-direct" "/usr/bin/autohotspot" + chmod +x /usr/bin/autohotspot + elif [ "$opt" = "REM" ] || [ "$opt" = "SHS" ] ;then + if [ -f "/usr/bin/autohotspotN" ]; then + rm /usr/bin/autohotspotN + fi + if [ -f "/usr/bin/autohotspot" ]; then + rm /usr/bin/autohotspot + fi + fi +} + +interface() +{ + #if interfaces file contains network settings + #backup and remove. + if grep -vxf "${cpath}/config/interfaces" "/etc/network/interfaces" ;then + mv "/etc/network/interfaces" "/etc/network/RCbackup-interfaces" + cp "${cpath}/config/interfaces" "/etc/network/interfaces" + fi + if [ "$opt" = "REM" ] ;then + if [ -f "/etc/network/RCbackup-interfaces" ] ;then + mv "/etc/network/RCbackup-interfaces" "/etc/network/interfaces" + fi + fi +} + +remove() +{ + if systemctl -all list-unit-files hostapd.service | grep "hostapd.service enabled" ;then + systemctl disable hostapd >/dev/null 2>&1 + fi + if systemctl -all list-unit-files dnsmasq.service | grep "dnsmasq.service enabled" ;then + systemctl disable dnsmasq >/dev/null 2>&1 + fi + auto_script #Remove Autohotspot Scripts + #Reset DHCPCD.conf + grep -vxf "${cpath}/config/dhcpcd-remove.conf" "/etc/dhcpcd.conf" > "${cpath}/config/Ndhcpcd.conf" + mv "${cpath}/config/Ndhcpcd.conf" "/etc/dhcpcd.conf" + hs_routing #remove routing for Static HS + sysctl #remove port forwarding + interface #restore backup of interfaces fle + auto_service #remove autohotspot.service +} + +Hotspotssid() +{ + #Change the Default Hotspot SSID and Password + if [ ! -f "/etc/hostapd/hostapd.conf" ] ;then + echo "A hotspot is not installed. No Password to change" + echo "press enter to continue" + read + menu + fi + HSssid=($(cat "/etc/hostapd/hostapd.conf" | grep '^ssid=')) + HSpass=($(cat "/etc/hostapd/hostapd.conf" | grep '^wpa_passphrase=')) + echo "Change the Hotspot's SSID and Password. press enter to keep existing settings" + echo "The current SSID is:" "${HSssid:5}" + echo "The current SSID Password is:" "${HSpass:15}" + echo "Enter the new Hotspots SSID:" + read ssname + echo "Enter the hotspots new password. Minimum 8 characters" + read sspwd + if [ ! -z $ssname ] ;then + echo "Changing Hotspot SSID to:" "$ssname" + sed -i -e "/^ssid=/c\ssid=$ssname" /etc/hostapd/hostapd.conf + else + echo "The Hotspot SSID is" ${HSssid: 5} + fi + if [ ! -z $sspwd ] && [ ${#sspwd} -ge 8 ] ;then + echo "Changing Hotspot Password to:" "$sspwd" + sed -i -e "/^wpa_passphrase=/c\wpa_passphrase=$sspwd" /etc/hostapd/hostapd.conf + else + echo "The Hotspot Password is:" ${HSpass: 15} + fi + echo "" + echo "The new setup will be available next time the hotspot is started" + echo "Press a key to continue" + read + menu +} + +setupssid() +{ + echo "Searching for local WiFi connection" + echo "Connect to a new WiFi network or change the password for an existing one in range" + echo "For Wifi networks where only a password is required." + echo "This will not work where a username and password is required" + ct=0; j=0 ; lp=0 + wfselect=() + + until [ $lp -eq 1 ] #wait for wifi if busy, usb wifi is slower. + do + IFS=$'\n:$\t' localwifi=($((iw dev wlan0 scan ap-force | egrep "SSID:") 2>&1)) >/dev/null 2>&1 + #if wifi device errors recheck + if (($j >= 5)); then #if busy 5 times exit to menu + echo "WiFi Device Unavailable, cannot scan for wifi devices at this time" + echo "press a key to continue" + menu + break + elif echo "${localwifi[1]}" | grep "No such device (-19)" >/dev/null 2>&1; then + echo "No Device found,trying again" + j=$((j + 1)) + sleep 2 + elif echo "${localwifi[1]}" | grep "Network is down (-100)" >/dev/null 2>&1 ; then + echo "Network Not available, trying again" + j=$((j + 1)) + sleep 2 + elif echo "${localwifi[1]}" | 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 "${localwifi[1]}" | grep "Invalid exchange (-52)" >/dev/null 2>&1 ; then + echo "Temporary unavailable, trying again" + j=$((j + 1)) + sleep 2 + elif echo "${localwifi[1]}" | grep -v "Device or resource busy (-16)" >/dev/null 2>&1 ; then + lp=1 + else #see if device not busy in 2 seconds + echo "WiFi Device unavailable checking again" + j=$((j + 1)) + sleep 2 + fi + done + + #Wifi Connections found - continue + for x in "${localwifi[@]}" + do + if [ $x != "SSID" ]; then + ct=$((ct + 1)) + echo "$ct ${x/ /}" + wfselect+=("${x/ /}") + fi + done + ct=$((ct + 1)) + echo "$ct To Cancel" + wfselect+=("Cancel") + if [ "${#wfselect[@]}" -eq 1 ] ;then + echo "Unable to detect local WiFi devices. Maybe there is a temporary issue with your WiFi" + echo "Try again in a minute" + echo "press a enter to continue" + read + menu + fi + + read wf + if [[ $wf =~ ^[0-9]+$ ]]; then + if [ $wf -ge 0 ] && [ $wf -le $ct ]; then + updatessid "${wfselect[$wf-1]}" + else + echo -e "\nNot a Valid entry" + setupssid + fi + else + echo -e "\nNot a Valid entry" + setupssid + fi +} + +updatessid() +{ + #check for blank in return + echo "$1" + echo "" + if [ "$1" = "Cancel" ] || [ "$1" = "" ] ; then + menu + exit + fi + + IFS="," 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']}") + ssids=($wpassid) + if [[ ! " ${ssids[@]} " =~ " $1 " ]]; then + echo "Add New Wifi Network" + echo "Selection SSID: $1" + echo "" + echo "Enter password for Wifi" + read ssidpw + echo -e "\nnetwork={\n\tssid=\x22$1\x22\n\tpsk=\x22$ssidpw\x22\n\tkey_mgmt=WPA-PSK\n}" >> /etc/wpa_supplicant/wpa_supplicant.conf + else + f=0 + echo "Change Password for Selected Wifi" + while IFS= read -r ln || [[ -n "$ln" ]] <&3; do + if [[ "$ln" == *"psk="* ]] && [ $f -eq 1 ] ;then + break + elif [[ "$ln" == *"$1"* ]] ; then + f=1 + fi + done < /etc/wpa_supplicant/wpa_supplicant.conf + echo "Change Wifi Network Password" + echo "Selected SSID: $1" + echo "" + echo "Enter password for Wifi" + read chgpw + newpsk=$'\tpsk=\x22'$chgpw$'\x22\n' + echo "The entry will be" $newpsk + echo "To be Replaced $ln" + sed -i '/'"$ln"'/c\'"$newpsk" /etc/wpa_supplicant/wpa_supplicant.conf + f=0 + fi +} + +forceswitch() +{ + if [ ! -f "/etc/systemd/system/autohotspot.service" ] ;then + echo "No Autohotspot script installed, unable to continue" + echo "press enter to continue" + read + menu + fi + Aserv=($(cat /etc/systemd/system/autohotspot.service | grep "ExecStart=")) + wi=($(cat ${Aserv: 10} | grep wifidev=)) + eth=($(cat ${Aserv: 10} | grep ethdev=)) + + wifidev=${wi[0]: 9:-1} #wifi device name from active autohotspot/N script + ethdev=${eth[0]: 8:-1} #Ethernet port to use with IP tables + +createAdHocNetwork_N() #for Internet routed Hotspot +{ + #receive IP as $1 + echo "Creating Hotspot with Internet" + ip link set dev "$wifidev" down + ip a add $1 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 +} + +createAdHocNetwork_D() #For non Internet Routed Hotspot +{ + echo "Creating Hotspot direct - no Internet" + ip link set dev "$wifidev" down + ip a add $1 brd + dev "$wifidev" + ip link set dev "$wifidev" up + dhcpcd -k "$wifidev" >/dev/null 2>&1 + systemctl start dnsmasq + systemctl start hostapd +} + +get_HS_IP() #get ip address from current active hotspot script +{ + #add check that the service is enabled, otherwise exit + Aserv=($(cat /etc/systemd/system/autohotspot.service | grep "ExecStart=")) #which hotspot is active? + if [ ${Aserv: -4} = "spot" ];then #Direct + ipline=($(cat /usr/bin/autohotspot | grep "ip a add")) + createAdHocNetwork_D "${ipline[3]}" + elif [ ${Aserv: -4} = "potN" ];then #Internet + ipline=($(cat /usr/bin/autohotspotN | grep "ip a add")) + createAdHocNetwork_N "${ipline[3]}" + else + echo "The Autohotspot is disabled or not installed" + echo "unable to force a switch." + echo "Press enter to continue" + read + menu + fi +} + +#Create Hotspot or connect to valid wifi networks +echo 0 > /proc/sys/net/ipv4/ip_forward #deactivate ip forwarding + +if systemctl status hostapd | grep "(running)" >/dev/null 2>&1 +then + echo "Hotspot already active" + echo "Switching to Network Wifi if it is available" + echo "this takes about 20 seconds to complete checks" + systemctl restart autohotspot.service + menu +elif { wpa_cli status | grep "$wifidev"; } >/dev/null 2>&1 +then + echo "Cleaning wifi files and Activating 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 + get_HS_IP + else #Neither the Hotspot or Network is active + get_HS_IP +fi +} + +display_HS_IP() #get ip address from current active hotspot script +{ + Aserv=($(cat /etc/systemd/system/autohotspot.service 2>/dev/null| grep "ExecStart=")) #which hotspot is active? + if [ ${Aserv: -4} = "spot" ] >/dev/null 2>&1 ;then #Direct + ipline=($(cat /usr/bin/autohotspot | grep "ip a add")) + echo "Hotspot IP Address for SSH and VNC: ${ipline[3]: :-3}" + elif [ ${Aserv: -4} = "potN" ] >/dev/null 2>&1 ;then #Internet + ipline=($(cat /usr/bin/autohotspotN | grep "ip a add")) + echo "Hotspot IP Address for SSH and VNC: ${ipline[3]: :-3}" + else #Static Hotspot default IP + echo "Hotspot IP Address for ssh and VNC: 192.168.50.10" + fi +} + +go() +{ + opt="$1" + #echo "Selected" "$opt" + #echo "Action options" + if [ "$opt" = "REM" ] ;then + remove + echo "Please reboot to complete the uninstall" + elif [ "$opt" = "SSI" ] ;then + setupssid + echo "the new ssid will be used next time the autohotspot script is " + echo "run at boot or manually otherwise use the Force to.... option" + echo "if the hotspot is active" + elif [ "$opt" = "FOR" ] ;then + forceswitch + elif [ "$opt" = "HSS" ] ;then + Hotspotssid + else + hostapd_config + dnsmasq_config + interface + sysctl + dhcpcd_config + auto_service + hs_routing + auto_script + echo "" + echo "The hotspot setup will be available after a reboot" + HSssid=($(cat "/etc/hostapd/hostapd.conf" | grep '^ssid=')) + HSpass=($(cat "/etc/hostapd/hostapd.conf" | grep '^wpa_passphrase=')) + echo "The Hotspots WiFi SSID name is: ${HSssid: 5}" + echo "The WiFi password is: ${HSpass: 15}" + display_HS_IP + fi + echo "Press any key to continue" + read + +} + +menu() +{ +#selection menu +clear +until [ "$select" = "8" ]; do + echo "Raspberryconnect.com Autohotspot installation and setup" + echo "For installation or switching between hotspot types" + echo " or uninstall the hotspot back to standard Pi wifi" + echo "" + echo "Autohotspot Net = connects to a known wifi network in range," + echo "otherwise creates a Raspberry Pi Hotspot with internet if an" + echo "Ethernet cable is connected, Wlan0, Eth0. Pi's 3,3+,4" + echo "" + echo "Autohotspot NO Net = as above but connected devices to the hotspot" + echo "will NOT get internet if an Ethernet cable is connected. Rpi Zero W" + echo "" + echo "Permanent Hotspot = permanent hotspot with net access for connected devices" + echo "" + echo " 1 = Install Autohotspot with Internet for Connected Devices" + echo " 2 = Install Autohotspot with No Internet for connected devices" + echo " 3 = Install a Permanent Hotspot with Internet for connected devices" + echo " 4 = Uninstall Autohotspot or Permanent Hotspot" + echo " 5 = Add or Change a WiFi network (SSID)" + echo " 6 = Autohotspot: Force to a Hotspot or Force to Network if SSID in Range" + echo " 7 = Change the Hotspots SSID and Password" + echo " 8 = Exit" + echo "" + echo -n "Select an Option:" + read select + case $select in + 1) clear ; go "AHN" ;; #Autohospot Internet + 2) clear ; go "AHD" ;; #Autohotspot Direct + 3) clear ; go "SHS" ;; #Static Hotspot + 4) clear ; go "REM" ;; #Remove Autohotspot or Static Hotspot + 5) clear ; go "SSI" ;; #Change/Add Wifi Network + 6) clear ; go "FOR" ;; #Force Hotspot <> Force Network + 7) clear ; go "HSS" ;; #Change Hotspot SSID and Password + 8) clear ; exit ;; + *) clear; echo "Please select again";; + esac +done +} + +check_installed #check system and status +if [ $nftble = "Y" ]; then + echo "The Internet Hotspots scripts use iptables. nftables is enabled on this system." + echo "It is not recommended you use iptables and nftables together." + echo "A nftables version will be available in the next update" + echo "" + echo "You can use the 'Autohotspot with NO Internet...' version of the script. Option 2 in the menus" + echo -n "Press a key to continue" + read +fi +check_reqfiles +check_installed +check_wificountry +menu #show menu diff --git a/sbin/Autohotspot/config/Checklist.md5 b/sbin/Autohotspot/config/Checklist.md5 new file mode 100644 index 0000000..652c8f2 --- /dev/null +++ b/sbin/Autohotspot/config/Checklist.md5 @@ -0,0 +1,16 @@ +ef41924efbed1bb6e0de487a2fe02f85 autohotspot-direct +b87b8b6fa19445d0e65045d8e271f456 autohotspot-direct.service +d7a25f23930a755404342194335451dd autohotspotN +bb7fd111c1baa11fdee18699041a2516 autohotspot-Net.service +557bfbf9d19f7d4a8d8fd19e40a435c5 dhcpcd-autohs.conf +696c6076b3475717f08059cc65080030 dhcpcd-remove.conf +26e78fb29affea8022a9c989f6475ca7 dhcpcd-SHS.conf +41ace870877f786773244a178f26cefe dhcpcd-SHSN.conf +8789bd5d1674e65d18c21c3bcffdfb0a dnsmasqAHS.conf +807885886aa6a40f30d4c9d480b80c84 dnsmasqAHSN.conf +25e3152c11d0860428bcb51b25e33441 dnsmasqSHS.conf +50d80f635b02b8ce58bfa8f260eba7e7 hostapd +46634b98cc27980c4130b9fa9d568de9 hostapd.conf +d0f09b7f89ef821edaef58e7b91b313a hs-iptables.service +c82e7473bb1490cf4702aaf7b669bb33 interfaces +fbb31822a27f4acca55acdb8f52d538c iptables-hs.txt diff --git a/sbin/Autohotspot/config/autohotspot-Net.service b/sbin/Autohotspot/config/autohotspot-Net.service new file mode 100644 index 0000000..6083e26 --- /dev/null +++ b/sbin/Autohotspot/config/autohotspot-Net.service @@ -0,0 +1,9 @@ +[Unit] +Description=Automatically generates a Hotspot when a valid SSID is not in range +After=multi-user.target +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/bin/autohotspotN +[Install] +WantedBy=multi-user.target diff --git a/sbin/Autohotspot/config/autohotspot-direct b/sbin/Autohotspot/config/autohotspot-direct new file mode 100644 index 0000000..76856e2 --- /dev/null +++ b/sbin/Autohotspot/config/autohotspot-direct @@ -0,0 +1,194 @@ +#!/bin/bash +#version 0.96-N/HS + +#You may share this script on the condition a reference to RaspberryConnect.com +#must be included in copies or derivatives of this script. + +#A script to switch between a wifi network and a non internet routed Hotspot +#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. +#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/,$//') +wpassid=$(echo "${wpassid//[$'\r\n']}") +IFS="," +ssids=($wpassid) +IFS=$IFSdef #reset back to defaults + + +#Note:If you only want to check for certain SSIDs +#Remove the # in in front of ssids=('mySSID1'.... below and put a # infront of all four lines above +# separated by a space, eg ('mySSID1' 'mySSID2') +#ssids=('mySSID1' 'mySSID2' 'mySSID3') + +#Enter the Routers Mac Addresses for hidden SSIDs, seperated by spaces ie +#( '11:22:33:44:55:66' 'aa:bb:cc:dd:ee:ff' ) +mac=() + +ssidsmac=("${ssids[@]}" "${mac[@]}") #combines ssid and MAC for checking + +createAdHocNetwork() +{ + echo "Creating Hotspot" + ip link set dev "$wifidev" down + ip a add 10.0.0.5/24 brd + dev "$wifidev" + ip link set dev "$wifidev" up + dhcpcd -k "$wifidev" >/dev/null 2>&1 + systemctl start dnsmasq + systemctl start hostapd +} + +KillHotspot() +{ + echo "Shutting Down Hotspot" + ip link set dev "$wifidev" down + systemctl stop hostapd + systemctl stop dnsmasq + 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 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.' + wpa_cli terminate "$wifidev" >/dev/null 2>&1 + createAdHocNetwork + 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() +{ +#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, usb wifi is slower. +do + ssidreply=$((iw dev "$wifidev" scan ap-force | egrep "^BSS|SSID:") 2>&1) >/dev/null 2>&1 + #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 "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 + 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 Network found, NoSSid issued" + echo "No SSid found, assessing WiFi status" + ssidChk='NoSSid' + fi +done +} + +NoDevice() +{ + #if no wifi device,ie usb wifi removed, activate wifi so when it is + #reconnected wifi to a router will be available + echo "No wifi device connected" + wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1 + exit 1 +} + +chksys +FindSSID + +#Create Hotspot or connect to valid wifi networks +if [ "$ssidChk" != "NoSSid" ] +then + 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 "Wifi already connected to a 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 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 #"No SSID, activating Hotspot" + createAdHocNetwork + fi +fi diff --git a/sbin/Autohotspot/config/autohotspot-direct.service b/sbin/Autohotspot/config/autohotspot-direct.service new file mode 100644 index 0000000..c121a94 --- /dev/null +++ b/sbin/Autohotspot/config/autohotspot-direct.service @@ -0,0 +1,9 @@ +[Unit] +Description=Automatically generates a Hotspot when a valid SSID is not in range +After=multi-user.target +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/bin/autohotspot +[Install] +WantedBy=multi-user.target diff --git a/sbin/Autohotspot/config/autohotspotN b/sbin/Autohotspot/config/autohotspotN new file mode 100644 index 0000000..755b08c --- /dev/null +++ b/sbin/Autohotspot/config/autohotspotN @@ -0,0 +1,204 @@ +#!/bin/bash +#version 0.96-N/HS-I + +#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 +#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/,$//') +wpassid=$(echo "${wpassid//[$'\r\n']}") +IFS="," +ssids=($wpassid) +IFS=$IFSdef #reset back to defaults + + +#Note:If you only want to check for certain SSIDs +#Remove the # in in front of ssids=('mySSID1'.... below and put a # infront of all four lines above +# separated by a space, eg ('mySSID1' 'mySSID2') +#ssids=('mySSID1' 'mySSID2' 'mySSID3') + +#Enter the Routers Mac Addresses for hidden SSIDs, seperated by spaces ie +#( '11:22:33:44:55:66' 'aa:bb:cc:dd:ee:ff' ) +mac=() + +ssidsmac=("${ssids[@]}" "${mac[@]}") #combines ssid and MAC for checking + +createAdHocNetwork() +{ + echo "Creating 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 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 ok" + 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.' + wpa_cli terminate "$wifidev" >/dev/null 2>&1 + createAdHocNetwork + 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() +{ +#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, usb wifi is slower. +do + ssidreply=$((iw dev "$wifidev" scan ap-force | egrep "^BSS|SSID:") 2>&1) >/dev/null 2>&1 + #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 "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 + 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 Network found, NoSSid issued" + echo "No SSid found, assessing WiFi status" + ssidChk='NoSSid' + fi +done +} + +NoDevice() +{ + #if no wifi device,ie usb wifi removed, activate wifi so when it is + #reconnected wifi to a router will be available + echo "No wifi device connected" + wpa_supplicant -B -i "$wifidev" -c /etc/wpa_supplicant/wpa_supplicant.conf >/dev/null 2>&1 + exit 1 +} + +chksys +FindSSID + +#Create Hotspot or connect to valid wifi networks +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 "Wifi already connected to a 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 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 #"No SSID, activating Hotspot" + createAdHocNetwork + fi +fi diff --git a/sbin/Autohotspot/config/dhcpcd-SHS.conf b/sbin/Autohotspot/config/dhcpcd-SHS.conf new file mode 100644 index 0000000..6a1ab85 --- /dev/null +++ b/sbin/Autohotspot/config/dhcpcd-SHS.conf @@ -0,0 +1,6 @@ +#RaspberryConnect.com +#Static Hotspot without Net +nohook wpa_supplicant +interface wlan0 +static ip_address=192.168.50.10/24 +static routers=192.168.50.1 diff --git a/sbin/Autohotspot/config/dhcpcd-SHSN.conf b/sbin/Autohotspot/config/dhcpcd-SHSN.conf new file mode 100644 index 0000000..94d5afe --- /dev/null +++ b/sbin/Autohotspot/config/dhcpcd-SHSN.conf @@ -0,0 +1,7 @@ +#RaspberryConnect.com +#Static Hotspot with Net +nohook wpa_supplicant +interface wlan0 +static ip_address=192.168.50.10/24 +static routers=192.168.50.1 +static domain_name_servers=8.8.8.8 \ No newline at end of file diff --git a/sbin/Autohotspot/config/dhcpcd-autohs.conf b/sbin/Autohotspot/config/dhcpcd-autohs.conf new file mode 100644 index 0000000..1890d6d --- /dev/null +++ b/sbin/Autohotspot/config/dhcpcd-autohs.conf @@ -0,0 +1,3 @@ +#RaspberryConnect.com +#Autohotspots +nohook wpa_supplicant \ No newline at end of file diff --git a/sbin/Autohotspot/config/dhcpcd-remove.conf b/sbin/Autohotspot/config/dhcpcd-remove.conf new file mode 100644 index 0000000..6ed1b3b --- /dev/null +++ b/sbin/Autohotspot/config/dhcpcd-remove.conf @@ -0,0 +1,9 @@ +#RaspberryConnect.com +#Autohotspots +nohook wpa_supplicant +#Static Hotspot without Net +interface wlan0 +static ip_address=192.168.50.10/24 +static routers=192.168.50.1 +#Static Hotspot with Net +static domain_name_servers=8.8.8.8 \ No newline at end of file diff --git a/sbin/Autohotspot/config/dnsmasqAHS.conf b/sbin/Autohotspot/config/dnsmasqAHS.conf new file mode 100644 index 0000000..6d1252d --- /dev/null +++ b/sbin/Autohotspot/config/dnsmasqAHS.conf @@ -0,0 +1,7 @@ +#RaspberryConnect.com +#AutoHotspot Config +interface=wlan0 +no-resolv +bind-interfaces +dhcp-range=10.0.0.50,10.0.0.100,12h + diff --git a/sbin/Autohotspot/config/dnsmasqAHSN.conf b/sbin/Autohotspot/config/dnsmasqAHSN.conf new file mode 100644 index 0000000..7b93ab5 --- /dev/null +++ b/sbin/Autohotspot/config/dnsmasqAHSN.conf @@ -0,0 +1,9 @@ +#RaspberryConnect.com Autohotspot +#AutoHotspotN config +interface=wlan0 +bind-dynamic +server=8.8.8.8 +domain-needed +bogus-priv +dhcp-range=192.168.50.150,192.168.50.200,72h + diff --git a/sbin/Autohotspot/config/dnsmasqSHS.conf b/sbin/Autohotspot/config/dnsmasqSHS.conf new file mode 100644 index 0000000..9021b3e --- /dev/null +++ b/sbin/Autohotspot/config/dnsmasqSHS.conf @@ -0,0 +1,8 @@ +#RaspberryConnect.com +#RPiHotspot config - Internet +interface=wlan0 +bind-dynamic +domain-needed +bogus-priv +server=8.8.8.8 +dhcp-range=192.168.50.150,192.168.50.200,255.255.255.0,12h diff --git a/sbin/Autohotspot/config/hostapd b/sbin/Autohotspot/config/hostapd new file mode 100644 index 0000000..978f3e0 --- /dev/null +++ b/sbin/Autohotspot/config/hostapd @@ -0,0 +1,23 @@ +# Defaults for hostapd initscript +# +# WARNING: The DAEMON_CONF setting has been deprecated and will be removed +# in future package releases. +# +# See /usr/share/doc/hostapd/README.Debian for information about alternative +# methods of managing hostapd. +# +# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration +# file and hostapd will be started during system boot. An example configuration +# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz +# +DAEMON_CONF="/etc/hostapd/hostapd.conf" + +# Additional daemon options to be appended to hostapd command:- +# -d show more debug messages (-dd for even more) +# -K include key data in debug messages +# -t include timestamps in some debug messages +# +# Note that -B (daemon mode) and -P (pidfile) options are automatically +# configured by the init.d script and must not be added to DAEMON_OPTS. +# +#DAEMON_OPTS="" diff --git a/sbin/Autohotspot/config/hostapd.conf b/sbin/Autohotspot/config/hostapd.conf new file mode 100644 index 0000000..3a14cd2 --- /dev/null +++ b/sbin/Autohotspot/config/hostapd.conf @@ -0,0 +1,22 @@ +#RaspberryConnect.com - Autohotspot +#2.4GHz setup wifi 80211 b,g,n +interface=wlan0 +driver=nl80211 +ssid=RPiHotspot +hw_mode=g +channel=8 +wmm_enabled=0 +macaddr_acl=0 +auth_algs=1 +ignore_broadcast_ssid=0 +wpa=2 +wpa_passphrase=1234567890 +wpa_key_mgmt=WPA-PSK +wpa_pairwise=CCMP TKIP +rsn_pairwise=CCMP + +#80211n - Change GB to your WiFi country code +country_code=GB +ieee80211n=1 +ieee80211d=1 + diff --git a/sbin/Autohotspot/config/hs-iptables.service b/sbin/Autohotspot/config/hs-iptables.service new file mode 100644 index 0000000..63325ff --- /dev/null +++ b/sbin/Autohotspot/config/hs-iptables.service @@ -0,0 +1,11 @@ +[Unit] +Description=Activate IPtables for Hotspot +After=network-pre.target +Before=network-online.target + +[Service] +Type=simple +ExecStart=/etc/iptables-hs + +[Install] +WantedBy=multi-user.target diff --git a/sbin/Autohotspot/config/interfaces b/sbin/Autohotspot/config/interfaces new file mode 100644 index 0000000..e46d1ee --- /dev/null +++ b/sbin/Autohotspot/config/interfaces @@ -0,0 +1,7 @@ +# interfaces(5) file used by ifup(8) and ifdown(8) + +# Please note that this file is written to be used with dhcpcd +# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf' + +# Include files from /etc/network/interfaces.d: +source-directory /etc/network/interfaces.d diff --git a/sbin/Autohotspot/config/iptables-hs.txt b/sbin/Autohotspot/config/iptables-hs.txt new file mode 100644 index 0000000..c4cdcea --- /dev/null +++ b/sbin/Autohotspot/config/iptables-hs.txt @@ -0,0 +1,5 @@ +#!/bin/bash +iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE +iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT +iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT + diff --git a/source/.vscode/launch.json b/source/.vscode/launch.json index 6a2248a..431fb9d 100644 --- a/source/.vscode/launch.json +++ b/source/.vscode/launch.json @@ -8,17 +8,16 @@ "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/footboard2PC", + "program": "${workspaceFolder}/footboard2PC.d", "args": [ "-s", - "FT4WIND6A", - "-m", - "-c", - "2000", + "FT4WINDSA", "-f", - "4096", + "2048", "-u", "2048", + "-p", + "80", "192.168.1.42", "DEBUG" ], diff --git a/source/enums.h b/source/enums.h index 8721328..c953c62 100644 --- a/source/enums.h +++ b/source/enums.h @@ -34,7 +34,8 @@ BEGIN_ENUM(ftdErrors) DECL_ENUM_ELEMENT(DEVICE_OPENED,Device already opened by other), DECL_ENUM_ELEMENT(NO_SUPPORTED_DEVICE,Device not supported or not configured for D2XX and 245FIFO use), DECL_ENUM_ELEMENT(BAD_BLOCK, Data Allocation failure: bad_block), - DECL_ENUM_ELEMENT(DEVICE_NOT_POWERED, Device not powered) + DECL_ENUM_ELEMENT(DEVICE_NOT_POWERED, Device not powered), + DECL_ENUM_ELEMENT(INVALID_SERIALNO_OR_DESC, Invalid serial number or description) } END_ENUM(ftdErrors) diff --git a/source/ftd.cpp b/source/ftd.cpp index f5c0954..0e43879 100644 --- a/source/ftd.cpp +++ b/source/ftd.cpp @@ -74,6 +74,8 @@ void ftd::open(const openMode &mode_,const string &deviceIdentificator) { break; } } + if(selectedDevice == NULL) + ftdThrow(INVALID_SERIALNO_OR_DESC); } void ftd::setFifoBuffer(const ulong &fifoBuffer) { if(selectedDevice!=NULL) { diff --git a/source/lastVersion b/source/lastVersion index 90651de..619f6e2 100644 --- a/source/lastVersion +++ b/source/lastVersion @@ -1 +1 @@ -linuxd +raspbiandlld