Skip to content

Instantly share code, notes, and snippets.

@Morrolan
Created October 21, 2025 19:39
Show Gist options
  • Select an option

  • Save Morrolan/d0e30d0e1ebd6869b7114f2666b76aad to your computer and use it in GitHub Desktop.

Select an option

Save Morrolan/d0e30d0e1ebd6869b7114f2666b76aad to your computer and use it in GitHub Desktop.
Wifi switcher script for Bjorn Cyberviking - ideal for use when connected via usb0.
#!/bin/bash
# Simple interactive Wi-Fi network switcher using nmcli
# Works great for Raspberry Pi headless setups via USB access
set -e
# Check nmcli exists
if ! command -v nmcli >/dev/null 2>&1; then
echo "Error: nmcli not found. Install with: sudo apt install network-manager"
exit 1
fi
# Ensure Wi-Fi radio is on
nmcli radio wifi on
echo "Scanning for Wi-Fi networks..."
echo
nmcli -t -f SSID,SIGNAL dev wifi list | awk -F: '{printf "%-30s %s%%\n", $1, $2}' | nl -w2 -s': '
# Get user choice
echo
read -rp "Enter the number of the Wi-Fi network to connect to: " choice
# Get SSID from the numbered list
SSID=$(nmcli -t -f SSID dev wifi list | sed -n "${choice}p")
if [[ -z "$SSID" ]]; then
echo "Invalid selection."
exit 1
fi
# Ask for password (hidden input)
read -rsp "Enter password for '$SSID': " PASSWORD
echo
# Delete any existing connection profile for this SSID
if nmcli connection show | grep -q "$SSID"; then
echo "Removing old connection profile for $SSID..."
nmcli connection delete "$SSID" >/dev/null 2>&1 || true
fi
echo "Connecting to $SSID..."
if nmcli dev wifi connect "$SSID" password "$PASSWORD"; then
echo "✅ Successfully connected to '$SSID'."
else
echo "❌ Failed to connect. Check the password or signal strength."
fi
echo
nmcli -t -f GENERAL.CONNECTION,GENERAL.DEVICE device show wlan0 2>/dev/null | grep -v '^$' || true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment