Created
September 16, 2025 16:04
-
-
Save chrismatthieu/4ab38edaa1b842515ed4df691922507c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -e # Exit immediately if a command exits with a non-zero status | |
| # --- Helper Functions --- | |
| # Function for timed confirmation, takes message and function name to run | |
| timed_confirm() { | |
| local message=$1 | |
| local function_to_run=$2 | |
| local timeout=5 # seconds | |
| local key_pressed=false | |
| local confirm="n" # Default to no if key is pressed but input is not y/Y | |
| # Print the message without a newline, add a space | |
| echo -n -e "\\n\\e[94m${message}\\e[0m " | |
| # Prompt user and show countdown on the same line | |
| for (( i=timeout; i>0; i-- )); do | |
| # Add blue color code here for the prompt line | |
| printf "\r\e[94m${message} Press any key to manually confirm/skip (automatically proceeding in %d seconds...)\e[0m " "$i" | |
| # Read with timeout of 1 second, non-blocking (-n 1), silent (-s) | |
| if read -t 1 -n 1 -s; then | |
| key_pressed=true | |
| break # Exit loop if key is pressed | |
| fi | |
| done | |
| # Clear the countdown line | |
| printf "\r%*s\r" "$(tput cols)" "" # Print spaces across the screen width, then return cursor | |
| # Re-print the original message cleanly after clearing the line | |
| echo -e "\\e[94m${message}\\e[0m" | |
| if $key_pressed; then | |
| # Clear potential buffered input from during countdown | |
| while read -t 0.1 -n 1 discard_char; do :; done | |
| # Add blue color code and change to (Y/n) | |
| read -rp $' | |
| \e[94mManually proceed with this setup step? (Y/n): \e[0m' confirm < /dev/tty | |
| # Default to Yes if Enter is pressed (confirm is empty) | |
| if [[ -z "$confirm" || "$confirm" =~ ^[Yy]$ ]]; then | |
| echo " Proceeding with manual confirmation..." | |
| "$function_to_run" | |
| else | |
| echo " Skipping this step..." | |
| fi | |
| else | |
| echo -e "\\e[94mTimeout reached, proceeding automatically...\\e[0m" | |
| "$function_to_run" | |
| fi | |
| } | |
| # =========================== | |
| # MAIN SETUP SCRIPT | |
| # =========================== | |
| # --- SSH Setup Block --- | |
| _setup_ssh() { | |
| echo -e "\\n--- Setting up SSH ---" | |
| echo "Installing and enabling SSH service..." | |
| sudo apt install -y ssh | |
| sudo systemctl enable ssh | |
| sudo systemctl start ssh | |
| echo "Checking SSH service status..." | |
| sudo SYSTEMD_PAGER='' systemctl status ssh | |
| } | |
| timed_confirm "Installing and configuring SSH..." _setup_ssh | |
| # --- Access Point Setup Block --- | |
| _setup_accesspoint() { | |
| echo "--- Running Access Point Setup ---" | |
| local SSID="realsense-bot" | |
| local PASSWORD="12345678" | |
| local CONNECTION_NAME="MyHotspot" | |
| local VIRTUAL_IFACE="wlan0_ap" | |
| local SETUP_SCRIPT="/usr/local/bin/setup_hotspot.sh" | |
| local SYSTEMD_SERVICE="/etc/systemd/system/hotspot.service" | |
| if [ -f "$SETUP_SCRIPT" ] || [ -f "$SYSTEMD_SERVICE" ]; then | |
| echo "Access point setup files detected ($SETUP_SCRIPT or $SYSTEMD_SERVICE exists)." | |
| echo "Assuming setup was already done. To force re-run, manually delete these files and disable/stop the hotspot.service." | |
| if ! systemctl is-active --quiet hotspot.service; then | |
| echo "Attempting to start existing hotspot service..." | |
| sudo systemctl start hotspot.service || echo "Failed to start existing service." | |
| fi | |
| echo "--- Skipping Access Point Setup Re-creation ---" | |
| return 0 | |
| fi | |
| echo "Creating the hotspot setup script at $SETUP_SCRIPT..." | |
| sudo tee "$SETUP_SCRIPT" > /dev/null << EOF_AP_SCRIPT | |
| #!/bin/bash | |
| set -e | |
| SSID="$SSID" | |
| PASSWORD="$PASSWORD" | |
| CONNECTION_NAME="$CONNECTION_NAME" | |
| VIRTUAL_IFACE="$VIRTUAL_IFACE" | |
| create_virtual_interface() { | |
| if ! iw dev | grep -q "$VIRTUAL_IFACE"; then | |
| echo "Creating virtual interface $VIRTUAL_IFACE..." | |
| sudo iw dev wlan0 interface add "$VIRTUAL_IFACE" type __ap || echo "Failed to add virtual interface. Is iw installed?" | |
| else | |
| echo "Virtual interface $VIRTUAL_IFACE already exists." | |
| fi | |
| } | |
| bring_up_interface() { | |
| echo "Bringing up interface $VIRTUAL_IFACE..." | |
| sudo ip link set "$VIRTUAL_IFACE" up || echo "Failed to bring up interface $VIRTUAL_IFACE" | |
| } | |
| configure_hotspot() { | |
| echo "Configuring NetworkManager hotspot..." | |
| if nmcli c show "$CONNECTION_NAME" > /dev/null 2>&1; then | |
| echo "Deleting existing connection '$CONNECTION_NAME'..." | |
| sudo nmcli connection delete "$CONNECTION_NAME" || echo "Failed to delete existing connection $CONNECTION_NAME" | |
| sleep 1 | |
| fi | |
| echo "Creating new hotspot connection '$CONNECTION_NAME'..." | |
| sudo nmcli connection add type wifi ifname "$VIRTUAL_IFACE" con-name "$CONNECTION_NAME" autoconnect yes ssid "$SSID" || echo "Failed to add connection $CONNECTION_NAME" | |
| sleep 1 | |
| sudo nmcli connection modify "$CONNECTION_NAME" 802-11-wireless.mode ap || echo "Failed: set mode ap" | |
| sudo nmcli connection modify "$CONNECTION_NAME" 802-11-wireless.band bg || echo "Failed: set band bg" | |
| sudo nmcli connection modify "$CONNECTION_NAME" ipv4.method shared || echo "Failed: set ipv4 shared" | |
| sudo nmcli connection modify "$CONNECTION_NAME" wifi-sec.key-mgmt wpa-psk || echo "Failed: set key-mgmt wpa-psk" | |
| sudo nmcli connection modify "$CONNECTION_NAME" wifi-sec.psk "$PASSWORD" || echo "Failed: set psk" | |
| sleep 1 | |
| echo "Activating hotspot connection '$CONNECTION_NAME'..." | |
| sudo nmcli connection up "$CONNECTION_NAME" || echo "Failed to bring up connection $CONNECTION_NAME" | |
| sleep 1 | |
| } | |
| configure_ssh() { | |
| echo "Ensuring SSH service is enabled and running..." | |
| if ! systemctl is-active ssh >/dev/null 2>&1; then | |
| sudo systemctl enable ssh || echo "Failed: enable ssh" | |
| sudo systemctl start ssh || echo "Failed: start ssh" | |
| else | |
| sudo systemctl restart ssh || echo "Failed: restart ssh" | |
| fi | |
| } | |
| create_virtual_interface | |
| bring_up_interface | |
| configure_hotspot | |
| configure_ssh | |
| echo "Access Point Setup script finished." | |
| EOF_AP_SCRIPT | |
| sudo chmod +x "$SETUP_SCRIPT" | |
| echo "Creating the systemd service at $SYSTEMD_SERVICE..." | |
| sudo tee "$SYSTEMD_SERVICE" > /dev/null << EOF_AP_SERVICE | |
| [Unit] | |
| Description=Persistent Wi-Fi Hotspot Setup (via setup_os.sh) | |
| After=network.target NetworkManager.service | |
| BindsTo=sys-subsystem-net-devices-wlan0.device | |
| [Service] | |
| Type=oneshot | |
| ExecStart=$SETUP_SCRIPT | |
| RemainAfterExit=yes | |
| [Install] | |
| WantedBy=multi-user.target | |
| EOF_AP_SERVICE | |
| echo "Reloading systemd daemon and enabling the hotspot service..." | |
| sudo systemctl daemon-reload | |
| sudo systemctl enable hotspot.service | |
| sudo systemctl start hotspot.service | |
| echo "Verifying the hotspot service status..." | |
| sudo systemctl status hotspot.service --no-pager -l || echo "Service status check failed." | |
| echo "--- Access Point Setup Complete ---" | |
| } | |
| timed_confirm "Setting up WiFi access point..." _setup_accesspoint | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment