Update bin/access-point.sh

This commit is contained in:
Zakary Timson 2024-07-27 10:49:50 -04:00
parent fa02eeb7be
commit 818b68d23b

View File

@ -11,36 +11,46 @@ DHCP_END="10.10.10.254" # End of DHCP IP pool
# End of Configuration ================================================================================================ # End of Configuration ================================================================================================
SCRIPT="$(basename $0)" SCRIPT="$(basename $0)"
FORCE=false
QUIET=false QUIET=false
FAILOVER=""
show_help() { show_help() {
echo "Usage: $(basename $0) [OPTIONS]" echo "Usage: $(basename $0) [OPTIONS]"
echo "" echo ""
echo "Options:" echo "Options:"
echo " -f, --force Create AP regardless of WiFi status" echo " -f, --failover Automaticaly toggle AP when network connection is lost"
echo " -h, --help Show this help message" echo " --failover=false Disable automatic toggling of AP"
echo " -q, --quiet Run without output" echo " -h, --help Show this help message"
echo " --enable Enable automatic fallback AP" echo " -q, --quiet Run without output"
echo " --disable Disable automatic fallback AP" echo " --ssid <SSID> Override default SSID"
echo " --passwd <PASSWORD> Override default password"
}
is_active() {
[ "$(systemctl is-active hostapd)" == "active" ]
}
is_connected() {
[ -n "$(iwgetid -r)" ]
} }
enable_ap() { enable_ap() {
rw > /dev/null
log_message "Enabling AP..." log_message "Enabling AP..."
systemctl stop dhcpcd systemctl stop dhcpcd
systemctl stop wpa_supplicant systemctl stop wpa_supplicant
systemctl stop systemd-resolved systemctl stop systemd-resolved
ip link set wlan0 down ip link set wlan0 down
ip link set wlan0 up ip link add link wlan0 name ap0 type macvlan mode bridge
ip addr add "$DHCP_IP/24" dev wlan0 ip link set ap0 up
ip addr add "$DHCP_IP/24" dev ap0
# Configure and start hostapd # Configure and start hostapd
cat <<EOF > /etc/hostapd/hostapd.conf cat <<EOF > /etc/hostapd/hostapd.conf
interface=wlan0 interface=ap0
ssid=$SSID ssid=$SSID
hw_mode=g hw_mode=g
channel=7 channel=0
wpa=2 wpa=2
wpa_passphrase=$PASSWORD wpa_passphrase=$PASSWORD
wpa_key_mgmt=WPA-PSK wpa_key_mgmt=WPA-PSK
@ -52,7 +62,7 @@ EOF
# Configure and start dnsmasq # Configure and start dnsmasq
cat <<EOF > /etc/dnsmasq.conf cat <<EOF > /etc/dnsmasq.conf
interface=wlan0 interface=ap0
dhcp-range=$DHCP_START,$DHCP_END,255.255.255.0,24h dhcp-range=$DHCP_START,$DHCP_END,255.255.255.0,24h
EOF EOF
systemctl start dnsmasq systemctl start dnsmasq
@ -60,7 +70,9 @@ EOF
disable_ap() { disable_ap() {
log_message "Disabling AP..." log_message "Disabling AP..."
ip addr del "$DHCP_IP/24" dev wlan0 ip addr del "$DHCP_IP/24" dev ap0
ip link set ap0 down
ip link delete ap0
systemctl stop hostapd systemctl stop hostapd
systemctl stop dnsmasq systemctl stop dnsmasq
systemctl start systemd-resolved systemctl start systemd-resolved
@ -91,21 +103,15 @@ log_message() {
# Parse command line arguments # Parse command line arguments
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do
case $1 in case $1 in
-f|--force) FORCE=true ;; -f|--failover|--failover=true) FAILOVER=true ;;
-h|--help) show_help && exit ;; -h|--help) show_help && exit ;;
-q|--quiet) QUIET=true ;; -q|--quiet) QUIET=true ;;
--enable) enable_cron && exit ;; --ssid) shift && SSID="$1" ;;
--disable) disable_cron && disable_ap && exit ;; --passwd) shift && PASSWORD="$1" ;;
*) *)
if [ -z "$SSID" ]; then echo "Unknown option: $1"
SSID="$1" show_help
elif [ -z "$PASSWORD" ]; then exit 1
PASSWORD="$1"
else
echo "Unknown option: $1"
show_help
exit 1
fi
;; ;;
esac esac
shift shift
@ -118,13 +124,13 @@ if [ "$UID" != "0" ]; then
fi fi
# Check if connected to WiFi # Check if connected to WiFi
if [ "$FORCE" == "false" ] && [ -n "$(iwgetid -r)" ] ; then if [ "$FORCE" == "false" ] && is_connected; then
log_message "Already connected to WiFi" log_message "Already connected to WiFi"
# Disable the access point if it's running # Disable the access point if it's running
if [ "$(systemctl is-active hostapd)" == "active" ]; then if [ "$(systemctl is-active hostapd)" == "active" ]; then
disable_ap disable_ap
fi fi
elif [ "$(systemctl is-active hostapd)" != "active" ]; then elif ! is_active; then
if [ "$FORCE" == "true" ]; then disable_cron; fi if [ "$FORCE" == "true" ]; then disable_cron; fi
enable_ap enable_ap
fi fi