Fixed AP Script

This commit is contained in:
Zakary Timson 2024-07-27 18:55:37 +00:00
parent ff5ee6c0a2
commit 5750d5bba3

View File

@ -2,55 +2,62 @@
# Configuration ======================================================================================================= # Configuration =======================================================================================================
SSID="$(hostname)" # Access point SSID name SSID="$HOSTNAME" # Default SSID to device hostname
PASSWORD="guesswhatitis" # Access point password PASSWORD="guesswhatitis" # Default password if not specified
DHCP_IP="10.10.10.1" # Device IP on access point DHCP_IP="10.10.10.1" # Device IP on access point
DHCP_START="10.10.10.2" # Start of DHCP IP pool DHCP_START="10.10.10.2" # Start of DHCP IP pool
DHCP_END="10.10.10.254" # End of DHCP IP pool DHCP_END="10.10.10.254" # End of DHCP IP pool
# End of Configuration ================================================================================================ # End of Configuration ================================================================================================
SCRIPT="$(basename $0)" DISABLE=false
QUIET=false
FAILOVER="" FAILOVER=""
QUIET=false
SCRIPT="$(basename $0)"
show_help() { show_help() {
echo "Usage: $(basename $0) [OPTIONS]" cat <<EOF
echo "" Usage: $SCRIPT [OPTIONS]
echo "Options:"
echo " -f, --failover Automaticaly toggle AP when network connection is lost"
echo " --failover=false Disable automatic toggling of AP"
echo " -h, --help Show this help message"
echo " -q, --quiet Run without output"
echo " --ssid <SSID> Override default SSID"
echo " --passwd <PASSWORD> Override default password"
}
is_active() { Options:
[ "$(systemctl is-active hostapd)" == "active" ] -d, --disable Turn off access point
-f, --failover Automatically turn on/off on network disconnect/connect
--failover=false Disable automatic on/off on disconnect/connect
-h, --help Show this help message
-q, --quiet Run without output
--ssid SSID The SSID for the access point
--passwd PASSWORD The password for the access point
EOF
} }
is_connected() { is_connected() {
[ -n "$(iwgetid -r)" ] [ -n "$(iwgetid -r)" ]
} }
is_on() {
[ "$(systemctl is-active hostapd)" == "active" ]
}
enable_ap() { enable_ap() {
log_message "Enabling AP..." if is_on; then
log_message "Access point is already on"
exit
fi
log_message "Turning on access point: $SSID"
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 add link wlan0 name ap0 type macvlan mode bridge ip link set wlan0 up
ip link set ap0 up ip addr add "$DHCP_IP/24" dev wlan0
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=ap0 interface=wlan0
ssid=$SSID ssid=$SSID
hw_mode=g hw_mode=g
channel=0 channel=4
wpa=2 wpa=2
wpa_passphrase=$PASSWORD wpa_passphrase=$PASSWORD
wpa_key_mgmt=WPA-PSK wpa_key_mgmt=WPA-PSK
@ -62,17 +69,19 @@ EOF
# Configure and start dnsmasq # Configure and start dnsmasq
cat <<EOF > /etc/dnsmasq.conf cat <<EOF > /etc/dnsmasq.conf
interface=ap0 interface=wlan0
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
} }
disable_ap() { disable_ap() {
log_message "Disabling AP..." if ! is_on; then
ip addr del "$DHCP_IP/24" dev ap0 log_message "Access point is already off"
ip link set ap0 down exit
ip link delete ap0 fi
log_message "Turning off access point..."
ip addr del "$DHCP_IP/24" dev wlan0
systemctl stop hostapd systemctl stop hostapd
systemctl stop dnsmasq systemctl stop dnsmasq
systemctl start systemd-resolved systemctl start systemd-resolved
@ -85,13 +94,13 @@ enable_cron() {
exit 1 exit 1
fi fi
sed -i "/$SCRIPT/d" /etc/crontab sed -i "/$SCRIPT/d" /etc/crontab
echo "* * * * * root $(realpath $0) -q" >> /etc/crontab echo "* * * * * root $(realpath $0) --ssid '$SSID' --passwd '$PASSWORD' -q -f" >> /etc/crontab
echo "Automatic fallback AP enabled" echo "Access point failover enabled"
} }
disable_cron() { disable_cron() {
sed -i "/$SCRIPT/d" /etc/crontab sed -i "/$SCRIPT/d" /etc/crontab
echo "Automatic fallback AP disabled" echo "Access point failover disabled"
} }
log_message() { log_message() {
@ -103,11 +112,12 @@ 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|--failover|--failover=true) FAILOVER=true ;; -d|--disable) DISABLE=true ;;
-f|--failover) FAILOVER=true ;;
--failover=false) FAILOVER=false ;; --failover=false) FAILOVER=false ;;
-h|--help) show_help && exit ;; -h|--help) show_help && exit ;;
-q|--quiet) QUIET=true ;; -q|--quiet) QUIET=true ;;
--ssid) shift && SSID="$1" ;; --ssid) shift && SSID="$1" ;;
--passwd) shift && PASSWORD="$1" ;; --passwd) shift && PASSWORD="$1" ;;
*) *)
echo "Unknown option: $1" echo "Unknown option: $1"
@ -124,14 +134,23 @@ if [ "$UID" != "0" ]; then
exit 1 exit 1
fi fi
# Check if connected to WiFi if [ -n "$FAILOVER" ]; then
if [ "$FORCE" == "false" ] && is_connected; then if [ "$FAILOVER" == "false" ]; then
log_message "Already connected to WiFi" disable_cron
# Disable the access point if it's running exit
if [ "$(systemctl is-active hostapd)" == "active" ]; then
disable_ap
fi fi
elif ! is_active; then
if [ "$FORCE" == "true" ]; then disable_cron; fi if [ -z "$(grep "$SCRIPT" /etc/crontab)" ]; then
enable_cron
fi
if is_connected; then
DISABLE=true
fi
fi
if [ "$DISABLE" == "true" ]; then
disable_ap
else
enable_ap enable_ap
fi fi