#!/usr/bin/env bash # ***This script was made by Clay Gomera (Drake)*** # - Description: A simple wifi dmenu script # - Dependencies: dmenu, NetworkManager ## 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" ##### ## These variables will store specific information about the wireless interface ##### wifi_info=$(nmcli dev | awk '/wifi/ {print $1,$3; exit}') read -r wlan constate <<< "$wifi_info" ##### ## This function uses nmcli to turn off wifi and then sends a notification ##### 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 } ##### ## This function uses nmcli to turn on wifi and then sends a notification ##### 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 } ##### ## This function uses nmcli and the $wlan and $constate variables to disconnect ## from the wifi network and then sends a notification ##### disconnect() { if [ "$constate" = "disconnected" ]; then notify-send "WiFi is already disconnected" elif [ "$constate" = "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 } ##### ## This function uses nmcli to first scan for available networks and then the ## $bssid variable will store the SSID of the network that the user chooses ##### connect() { notify-send -t 5000 "Scanning networks..." nmcli dev wifi rescan 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}') } ##### ## This function will store the WiFi password in the $pass variable ##### 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 } ##### ## This function will actually connect to the chosen WiFi network using the ## $bssid and $pass variables ##### 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 } ########## ## main ## ########## cases=$(echo -e "$options" | dmenu -l 6 -i -p " Wifi Settings") if [ -z "$cases" ]; then exit 0 fi case "$cases" 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