Skip to content

Instantly share code, notes, and snippets.

@douxxtech
Created November 16, 2025 16:26
Show Gist options
  • Select an option

  • Save douxxtech/3f0684eceb9e95bdf4edb91b4c7cd685 to your computer and use it in GitHub Desktop.

Select an option

Save douxxtech/3f0684eceb9e95bdf4edb91b4c7cd685 to your computer and use it in GitHub Desktop.
A simple bash tool to manage wifi connections in a single command
#!/bin/bash
# made by github.com/douxxtech
# Note that this was originally made for a rpi 0 w running rpi os buster lite.
RED='\033[0;31m'
GRN='\033[0;32m'
YEL='\033[1;33m'
NC='\033[0m'
log() {
local level="$1"
shift
local color=""
case "$level" in
INFO) color="$GRN" ;;
WARN) color="$YEL" ;;
ERROR) color="$RED" ;;
*) color="$NC" ;;
esac
printf "[%s] ${color}%-5s${NC} %s\n" "$(date +%T)" "$level" "$*" >&2
}
WLAN_IF="wlan0"
USB_IF="usb0"
TMP_CONF=""
SSID="$1"
PASS="$2"
usage() {
echo "Usage:"
echo " wificli <SSID> [password] Connect to Wi-Fi"
echo " wificli d Disconnect from Wi-Fi and restore USB routing"
exit 1
}
connect_wifi() {
if [ -z "$SSID" ]; then
usage
fi
log INFO "Bringing up $WLAN_IF"
ip link set "$WLAN_IF" up || { log ERROR "Failed to bring up $WLAN_IF"; exit 1; }
killall -q wpa_supplicant dhclient
rm -f "/var/run/wpa_supplicant/${WLAN_IF}"
TMP_CONF=$(mktemp)
if [ -z "$PASS" ]; then
log INFO "Generating config for open network"
cat > "$TMP_CONF" <<EOF
ctrl_interface=/var/run/wpa_supplicant
network={
ssid="$SSID"
key_mgmt=NONE
}
EOF
else
log INFO "Generating config for secured network"
wpa_passphrase "$SSID" "$PASS" > "$TMP_CONF"
fi
log INFO "Starting wpa_supplicant"
wpa_supplicant -B -i "$WLAN_IF" -c "$TMP_CONF" || {
log ERROR "Failed to start wpa_supplicant"
rm -f "$TMP_CONF"
exit 1
}
sleep 2
log INFO "Requesting IP via DHCP"
dhclient -v "$WLAN_IF"
sleep 5
IP=$(ip -4 addr show "$WLAN_IF" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
if [ -z "$IP" ]; then
log ERROR "No IP obtained from DHCP"
disconnect_wifi
exit 1
fi
log INFO "Got IP: $IP"
GW=$(ip route show default | grep "$WLAN_IF" | awk '{print $3}' | head -n1)
if [ -n "$GW" ]; then
log INFO "Setting default route via $GW"
ip route del default 2>/dev/null
ip route add default via "$GW" dev "$WLAN_IF"
else
log WARN "No default gateway found for $WLAN_IF"
fi
rm -f "$TMP_CONF"
log INFO "Connected to $SSID"
}
disconnect_wifi() {
log INFO "Disconnecting Wi-Fi"
killall -q dhclient wpa_supplicant
rm -f "/var/run/wpa_supplicant/${WLAN_IF}"
ip addr flush dev "$WLAN_IF"
ip link set "$WLAN_IF" down
log INFO "Restoring default route to $USB_IF"
GW_USB=$(ip route show default | grep "$USB_IF" | awk '{print $3}' | head -n1)
if [ -n "$GW_USB" ]; then
ip route del default 2>/dev/null
ip route add default via "$GW_USB" dev "$USB_IF"
log INFO "Default route restored to $USB_IF ($GW_USB)"
else
log WARN "Could not detect gateway for $USB_IF"
fi
log INFO "Wi-Fi disconnected"
}
if [ "$(id -u)" -ne 0 ]; then
log ERROR "Please run as root or with sudo"
exit 1
fi
case "$SSID" in
d)
disconnect_wifi
;;
"")
usage
;;
*)
connect_wifi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment