neodotfiles/source/dmenu/scripts/dmenu_wifi

126 lines
3.1 KiB
Text
Raw Normal View History

2024-03-28 21:40:01 +00:00
#!/usr/bin/env bash
2024-08-13 13:07:56 +00:00
# *** Script by Clay Gomera (Drake) ***
# Description: A simple WiFi management script using dmenu and NetworkManager
# Dependencies: dmenu, NetworkManager, notify-send
2024-03-28 21:40:01 +00:00
2024-08-13 13:07:56 +00:00
# Main menu options
2024-03-28 21:40:01 +00:00
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"
2024-08-13 13:07:56 +00:00
# Retrieve wireless interface information
2024-03-28 21:40:01 +00:00
wifi_info=$(nmcli dev | awk '/wifi/ {print $1,$3; exit}')
2024-08-13 13:07:56 +00:00
read -r wlan state <<< "$wifi_info"
2024-03-28 21:40:01 +00:00
2024-08-13 13:07:56 +00:00
# Function to turn off WiFi
2024-03-28 21:40:01 +00:00
turnoff() {
2024-06-03 20:01:31 +00:00
nmcli radio wifi off
if [ $? -eq 0 ]; then
notify-send "WiFi has been turned off"
else
notify-send "Failed to turn off WiFi"
fi
2024-03-28 21:40:01 +00:00
}
2024-08-13 13:07:56 +00:00
# Function to turn on WiFi
2024-03-28 21:40:01 +00:00
turnon() {
2024-06-03 20:01:31 +00:00
nmcli radio wifi on
if [ $? -eq 0 ]; then
notify-send "WiFi has been turned on"
else
notify-send "Failed to turn on WiFi"
fi
2024-03-28 21:40:01 +00:00
}
2024-08-13 13:07:56 +00:00
# Function to disconnect WiFi
2024-03-28 21:40:01 +00:00
disconnect() {
2024-08-13 13:07:56 +00:00
if [ "$state" = "disconnected" ]; then
2024-03-28 21:40:01 +00:00
notify-send "WiFi is already disconnected"
2024-08-13 13:07:56 +00:00
elif [ "$state" = "connected" ]; then
2024-06-03 20:01:31 +00:00
nmcli device disconnect "$wlan"
if [ $? -eq 0 ]; then
notify-send "WiFi has been disconnected"
else
notify-send "Failed to disconnect WiFi"
fi
2024-03-28 21:40:01 +00:00
else
2024-06-03 20:01:31 +00:00
notify-send "Unknown WiFi state"
2024-03-28 21:40:01 +00:00
fi
}
2024-08-13 13:07:56 +00:00
# Function to select and connect to a WiFi network
2024-03-28 21:40:01 +00:00
connect() {
notify-send -t 5000 "Scanning networks..."
nmcli dev wifi rescan
2024-08-13 13:07:56 +00:00
local wifinet=$(nmcli -f BSSID,SSID,BARS,SECURITY dev wifi list | sed -n '1!p' | dmenu -i -l 10 -p " Select a Wifi Network")
2024-06-03 20:01:31 +00:00
if [ -z "$wifinet" ]; then
exit 0
fi
bssid=$(echo "$wifinet" | awk '{print $1}')
ssid=$(echo "$wifinet" | awk '{print $2}')
2024-03-28 21:40:01 +00:00
}
2024-08-13 13:07:56 +00:00
# Function to prompt for WiFi password
2024-03-28 21:40:01 +00:00
password() {
2024-06-03 20:01:31 +00:00
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=""
2024-03-28 21:40:01 +00:00
else
2024-06-03 20:01:31 +00:00
pass=$(dmenu -l 1 -P -p "Enter Password ")
if [ -z "$pass" ]; then
exit 0
2024-03-28 21:40:01 +00:00
fi
fi
}
2024-08-13 13:07:56 +00:00
# Function to connect to the selected WiFi network
2024-03-28 21:40:01 +00:00
action() {
2024-06-03 20:01:31 +00:00
if [ -n "$pass" ]; then
2024-03-28 21:40:01 +00:00
nmcli dev wifi connect "$bssid" password "$pass"
2024-06-03 20:01:31 +00:00
else
2024-03-28 21:40:01 +00:00
nmcli dev wifi connect "$bssid"
fi
2024-06-03 20:01:31 +00:00
if [ $? -eq 0 ]; then
notify-send "Connected to $ssid"
else
notify-send "Failed to connect to $ssid"
fi
2024-03-28 21:40:01 +00:00
}
2024-08-13 13:07:56 +00:00
# Show menu and handle user choice
case=$(echo -e "$options" | dmenu -l 6 -i -p " WiFi Settings")
if [ -z "$case" ]; then
2024-06-03 20:01:31 +00:00
exit 0
fi
2024-08-13 13:07:56 +00:00
case "$case" in
2024-03-28 21:40:01 +00:00
"$option1")
turnon
;;
"$option2")
turnoff
;;
"$option3")
disconnect
;;
"$option4")
2024-06-03 20:01:31 +00:00
if connect; then
if password; then
action
fi
2024-03-28 21:40:01 +00:00
fi
;;
"$option5")
2024-06-03 20:01:31 +00:00
${BROWSER:-xdg-open} http://networkcheck.kde.org
2024-03-28 21:40:01 +00:00
;;
"$option6")
exit 0
2024-06-03 20:01:31 +00:00
;;
2024-03-28 21:40:01 +00:00
esac