wpe/wpe
2025-09-11 13:40:59 +02:00

104 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Display information about the required options
usage() {
cat <<'EOF'
Usage: $(basename "$0") -i <interface>
Options:
-i, --iface Network interface name (e.g., wlan0, eth0). Required.
-h, --help Show this help and exit.
Example:
$(basename "$0") -i wlan0
EOF
}
iface="wlan0"
# Check if script is run as root
if [[ $EUID -ne 0 ]]; then
echo "[!] This script requires root permissions." >&2
exit 1
fi
# Translate long options to short for getopts
# Keeping the script POSIX-friendly without relying on GNU getopt
if [[ $# -gt 0 ]]; then
args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--iface) args+=("-i"); shift ;;
--help) args+=("-h"); shift ;;
*) args+=("$1"); shift ;;
esac
done
set -- "${args[@]}"
fi
# Parse short options
while getopts ":i:h" opt; do
case "$opt" in
i) iface="$OPTARG" ;;
h) usage; exit 0 ;;
\?) echo "Error: Invalid option -$OPTARG" >&2; usage; exit 1 ;;
:) echo "Error: Option -$OPTARG requires an argument." >&2; usage; exit 1 ;;
esac
done
# Require -i/--iface
if [[ -z "${iface}" ]]; then
echo "Error: A network interface must be pecified with -i/--iface." >&2
echo "Example: $(basename "$0") -i wlan0" >&2
exit 1
fi
# Validate the interface exists on the system
if ! ip link show "$iface" &>/dev/null; then
echo "Error: Interface '$iface' does not exist on this system." >&2
exit 1
fi
# Check if hostapd-wpe is installed
if ! command -v hostapd-wpe &>/dev/null; then
read -r -p "[!] hostapd-wpe is not installed. Install it now? (Y/n): " choice
choice=${choice:-Y}
if [[ $choice =~ ^[Yy]$ ]]; then
if ! apt update || ! apt install -y hostapd-wpe; then
echo "[!] Installation failed. Exiting."
exit 1
fi
else
echo "[!] hostapd-wpe is required. Exiting."
exit 1
fi
fi
echo "[+] Preparing $iface for hostapd-wpe..."
nmcli device set "$iface" managed no
ip link set "$iface" down
ip link set "$iface" up
# Check for hostapd-wpe.conf in the current directory
if [[ -f "hostapd-wpe.conf" ]]; then
if grep -q '^interface=' hostapd-wpe.conf; then
sed -i "s/^interface=.*/interface=$iface/" hostapd-wpe.conf
echo "[+] Updated existing interface definition in hostapd-wpe.conf."
else
echo "interface=$iface" >> hostapd-wpe.conf
echo "[+] Added missing interface definition to hostapd-wpe.conf."
fi
echo "[+] Found hostapd-wpe.conf in the current directory. Using it."
config_file="hostapd-wpe.conf"
else
echo "[!] hostapd-wpe.conf not found in the current directory. Using the default: /etc/hostapd-wpe/hostapd-wpe.conf"
config_file="/etc/hostapd-wpe/hostapd-wpe.conf"
fi
# Launch hostapd-wpe
if ! hostapd-wpe -k "$config_file"; then
echo "[!] Failed to start hostapd-wpe. Exiting."
exit 1
fi