PiKVM/bin/access-point.sh

163 lines
3.7 KiB
Bash
Raw Normal View History

2024-07-26 21:52:58 -04:00
#!/bin/bash
# Configuration =======================================================================================================
2024-07-27 15:08:04 -04:00
SSID="$HOSTNAME" # Defaults SSID to hostname
2024-07-26 21:52:58 -04:00
DHCP_START="10.10.10.2" # Start of DHCP IP pool
DHCP_END="10.10.10.254" # End of DHCP IP pool
# End of Configuration ================================================================================================
2024-07-27 14:55:37 -04:00
DISABLE=false
2024-07-27 10:49:50 -04:00
FAILOVER=""
2024-07-27 15:08:04 -04:00
PASSWORD=""
2024-07-27 14:55:37 -04:00
QUIET=false
SCRIPT="$(basename $0)"
2024-07-26 21:52:58 -04:00
show_help() {
2024-07-27 14:55:37 -04:00
cat <<EOF
Usage: $SCRIPT [OPTIONS]
Options:
-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
2024-07-27 10:49:50 -04:00
}
2024-07-27 14:55:37 -04:00
is_connected() {
[ -n "$(iwgetid -r)" ]
2024-07-27 10:49:50 -04:00
}
2024-07-27 14:55:37 -04:00
is_on() {
[ "$(systemctl is-active hostapd)" == "active" ]
2024-07-26 21:52:58 -04:00
}
enable_ap() {
2024-07-27 14:55:37 -04:00
if is_on; then
log_message "Access point is already on"
exit
fi
log_message "Turning on access point: $SSID"
2024-07-26 21:52:58 -04:00
systemctl stop dhcpcd
systemctl stop wpa_supplicant
systemctl stop systemd-resolved
ip link set wlan0 down
2024-07-27 14:55:37 -04:00
ip link set wlan0 up
ip addr add "$DHCP_IP/24" dev wlan0
2024-07-26 21:52:58 -04:00
# Configure and start hostapd
cat <<EOF > /etc/hostapd/hostapd.conf
2024-07-27 14:55:37 -04:00
interface=wlan0
2024-07-26 21:52:58 -04:00
ssid=$SSID
hw_mode=g
2024-07-27 14:55:37 -04:00
channel=4
2024-07-26 21:52:58 -04:00
wpa=2
wpa_passphrase=$PASSWORD
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
EOF
systemctl unmask hostapd
systemctl start hostapd
# Configure and start dnsmasq
cat <<EOF > /etc/dnsmasq.conf
2024-07-27 14:55:37 -04:00
interface=wlan0
2024-07-26 21:52:58 -04:00
dhcp-range=$DHCP_START,$DHCP_END,255.255.255.0,24h
EOF
systemctl start dnsmasq
}
disable_ap() {
2024-07-27 14:55:37 -04:00
if ! is_on; then
log_message "Access point is already off"
exit
fi
log_message "Turning off access point..."
ip addr del "$DHCP_IP/24" dev wlan0
2024-07-26 21:52:58 -04:00
systemctl stop hostapd
systemctl stop dnsmasq
systemctl start systemd-resolved
systemctl restart dhcpcd
}
enable_cron() {
if [ -z "$(which crontab)" ]; then
echo "Error: Please install crontab"
exit 1
fi
sed -i "/$SCRIPT/d" /etc/crontab
2024-07-27 14:55:37 -04:00
echo "* * * * * root $(realpath $0) --ssid '$SSID' --passwd '$PASSWORD' -q -f" >> /etc/crontab
echo "Access point failover enabled"
2024-07-26 21:52:58 -04:00
}
disable_cron() {
sed -i "/$SCRIPT/d" /etc/crontab
2024-07-27 14:55:37 -04:00
echo "Access point failover disabled"
2024-07-26 21:52:58 -04:00
}
log_message() {
if [ "$QUIET" != "true" ]; then
echo "$1"
fi
}
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
2024-07-27 14:55:37 -04:00
-d|--disable) DISABLE=true ;;
-f|--failover) FAILOVER=true ;;
2024-07-27 10:50:55 -04:00
--failover=false) FAILOVER=false ;;
2024-07-26 21:52:58 -04:00
-h|--help) show_help && exit ;;
-q|--quiet) QUIET=true ;;
2024-07-27 14:55:37 -04:00
--ssid) shift && SSID="$1" ;;
2024-07-27 10:49:50 -04:00
--passwd) shift && PASSWORD="$1" ;;
2024-07-26 21:52:58 -04:00
*)
2024-07-27 10:49:50 -04:00
echo "Unknown option: $1"
show_help
exit 1
2024-07-26 21:52:58 -04:00
;;
esac
shift
done
# Check for permissions
if [ "$UID" != "0" ]; then
echo "Error: Please run as root"
exit 1
fi
2024-07-27 14:55:37 -04:00
if [ -n "$FAILOVER" ]; then
if [ "$FAILOVER" == "false" ]; then
disable_cron
exit
fi
2024-07-27 16:16:47 -04:00
if [ -z "$PASSWORD" ]; then
echo "Error: Password required"
show_help
exit 1
2024-07-26 21:52:58 -04:00
fi
2024-07-27 16:16:47 -04:00
enable_cron
2024-07-27 14:55:37 -04:00
if is_connected; then
DISABLE=true
fi
fi
if [ "$DISABLE" == "true" ]; then
disable_ap
else
2024-07-27 15:05:08 -04:00
if [ -z "$PASSWORD" ]; then
echo "Error: Password required"
show_help
exit 1
fi
2024-07-26 21:52:58 -04:00
enable_ap
2024-07-27 10:49:50 -04:00
fi