#!/usr/bin/env bash # *** Script by Clay Gomera (Drake) *** # Description: A simple WiFi management script using dmenu and NetworkManager # Dependencies: dmenu, NetworkManager, notify-send # Main menu options option1=" Turn on WiFi" option2=" Turn off WiFi" option3="󱛅 Disconnect WiFi" option4="󱛃 Connect WiFi" option5="󱛆 Setup captive portal" option6="󱎘 Exit" options="$option1\n$option2\n$option3\n$option4\n$option5\n$option6" # Retrieve wireless interface information wifi_info=$(nmcli dev | awk '/wifi/ {print $1,$3; exit}') read -r wlan state <<< "$wifi_info" # Function to turn off WiFi turnoff() { nmcli radio wifi off if [ $? -eq 0 ]; then notify-send "WiFi has been turned off" else notify-send "Failed to turn off WiFi" fi } # Function to turn on WiFi turnon() { nmcli radio wifi on if [ $? -eq 0 ]; then notify-send "WiFi has been turned on" else notify-send "Failed to turn on WiFi" fi } # Function to disconnect WiFi disconnect() { if [ "$state" = "disconnected" ]; then notify-send "WiFi is already disconnected" elif [ "$state" = "connected" ]; then nmcli device disconnect "$wlan" if [ $? -eq 0 ]; then notify-send "WiFi has been disconnected" else notify-send "Failed to disconnect WiFi" fi else notify-send "Unknown WiFi state" fi } # Function to select and connect to a WiFi network connect() { notify-send -t 5000 "Scanning networks..." nmcli dev wifi rescan local wifinet=$(nmcli -f BSSID,SSID,BARS,SECURITY dev wifi list | sed -n '1!p' | dmenu -i -l 10 -p " Select a Wifi Network") if [ -z "$wifinet" ]; then exit 0 fi bssid=$(echo "$wifinet" | awk '{print $1}') ssid=$(echo "$wifinet" | awk '{print $2}') } # Function to prompt for WiFi password password() { if nmcli connection show | awk -v ssid="$ssid" '$1 == ssid {found=1} END {exit !found}'; then pass="" elif nmcli -f BSSID,SECURITY dev wifi list | grep -w "$bssid" | awk '{print $2}' | grep -q -- "--"; then pass="" else pass=$(dmenu -l 1 -P -p "Enter Password ") if [ -z "$pass" ]; then exit 0 fi fi } # Function to connect to the selected WiFi network action() { if [ -n "$pass" ]; then nmcli dev wifi connect "$bssid" password "$pass" else nmcli dev wifi connect "$bssid" fi if [ $? -eq 0 ]; then notify-send "Connected to $ssid" else notify-send "Failed to connect to $ssid" fi } # Show menu and handle user choice case=$(echo -e "$options" | dmenu -l 6 -i -p " WiFi Settings") if [ -z "$case" ]; then exit 0 fi case "$case" in "$option1") turnon ;; "$option2") turnoff ;; "$option3") disconnect ;; "$option4") if connect; then if password; then action fi fi ;; "$option5") ${BROWSER:-xdg-open} http://networkcheck.kde.org ;; "$option6") exit 0 ;; esac