neodotfiles/config/rofi/scripts/rs_wifi

169 lines
4 KiB
Text
Raw Permalink Normal View History

2023-09-08 18:43:08 +00:00
#!/usr/bin/env bash
2024-06-23 22:50:38 +00:00
# rs_wifi - A simple WiFi menu script for rofi/dmenu/wofi
# Author: Clay Gomera (Drake)
# Dependencies: {rofi || dmenu || wofi}, NetworkManager, libnotify (notify-send)
2023-09-08 18:43:08 +00:00
############################
# Configuration Parameters #
############################
# Main menu options
2023-09-08 18:43:08 +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"
# WiFi interface information
wifi_info=$(nmcli dev | awk '/wifi/ {print $1,$3; exit}')
read -r wlan constate <<< "$wifi_info"
2023-09-08 18:43:08 +00:00
########################
# Function Definitions #
########################
# Check for missing dependencies
check_dependencies() {
local run_launcher_found=false
for launcher in rofi dmenu wofi; do
if command -v "$launcher" &> /dev/null; then
run_launcher_found=true
break
fi
done
if [ "$run_launcher_found" = false ]; then
echo "Missing dependency: one of rofi, dmenu, or wofi is required."
exit 1
fi
local missing_deps=()
for dep in nmcli notify-send; do
if ! command -v "$dep" &> /dev/null; then
missing_deps+=("$dep")
fi
done
if [ ${#missing_deps[@]} -ne 0 ]; then
echo "Missing dependencies: ${missing_deps[*]}"
exit 1
fi
}
# Turn off WiFi
2023-09-08 18:43:08 +00:00
turnoff() {
2024-06-05 21:02:32 +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
2023-09-08 18:43:08 +00:00
}
# Turn on WiFi
2023-09-08 18:43:08 +00:00
turnon() {
2024-06-05 21:02:32 +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
2023-09-08 18:43:08 +00:00
}
# Disconnect from WiFi
2023-09-08 18:43:08 +00:00
disconnect() {
if [ "$constate" = "disconnected" ]; then
2024-06-05 21:02:32 +00:00
notify-send "WiFi is already disconnected"
2023-09-08 18:43:08 +00:00
elif [ "$constate" = "connected" ]; then
2024-06-05 21:02:32 +00:00
nmcli device disconnect "$wlan"
if [ $? -eq 0 ]; then
notify-send "WiFi has been disconnected"
else
notify-send "Failed to disconnect WiFi"
fi
2023-09-08 18:43:08 +00:00
else
2024-06-05 21:02:32 +00:00
notify-send "Unknown WiFi state"
2023-09-08 18:43:08 +00:00
fi
}
# Connect to a WiFi network
2023-09-08 18:43:08 +00:00
connect() {
2024-06-05 21:02:32 +00:00
notify-send -t 5000 "Scanning networks..."
nmcli dev wifi rescan
wifinet=$(nmcli -f BSSID,SSID,BARS dev wifi list | sed -n '1!p' | $RUNNER -i -p " Select a Wifi Network  ")
2024-06-05 21:02:32 +00:00
if [ -z "$wifinet" ]; then
exit 0
fi
bssid=$(echo "$wifinet" | awk '{print $1}')
ssid=$(echo "$wifinet" | awk '{print $2}')
2023-09-08 18:43:08 +00:00
}
# Prompt for WiFi password
2023-09-08 18:43:08 +00:00
password() {
2024-06-05 21:02:32 +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=""
else
pass=$($RUNNER -password -p "Enter Password   ")
2024-06-05 21:02:32 +00:00
if [ -z "$pass" ]; then
exit 0
fi
fi
2023-09-08 18:43:08 +00:00
}
# Execute WiFi connection
2023-09-08 18:43:08 +00:00
action() {
2024-06-05 21:02:32 +00:00
if [ -n "$pass" ]; then
nmcli dev wifi connect "$bssid" password "$pass"
2024-06-05 21:02:32 +00:00
else
nmcli dev wifi connect "$bssid"
fi
if [ $? -eq 0 ]; then
notify-send "Connected to $ssid"
else
notify-send "Failed to connect to $ssid"
2023-09-08 18:43:08 +00:00
fi
}
####################
# Main Script Flow #
####################
# Check for dependencies
check_dependencies
# Display main menu and execute selected option
cases=$(echo -e "$options" | $RUNNER -i -p " Wifi Settings  ")
2024-06-05 21:02:32 +00:00
if [ -z "$cases" ]; then
exit 0
fi
2023-09-08 18:43:08 +00:00
case "$cases" in
"$option1")
2024-06-05 21:02:32 +00:00
turnon
2023-09-08 18:43:08 +00:00
;;
"$option2")
2024-06-05 21:02:32 +00:00
turnoff
2023-09-08 18:43:08 +00:00
;;
"$option3")
2024-06-05 21:02:32 +00:00
disconnect
2023-09-08 18:43:08 +00:00
;;
"$option4")
2024-06-05 21:02:32 +00:00
if connect; then
if password; then
action
fi
2023-09-08 18:43:08 +00:00
fi
;;
"$option5")
2024-06-05 21:02:32 +00:00
${BROWSER:-xdg-open} http://networkcheck.kde.org
2023-09-08 18:43:08 +00:00
;;
"$option6")
2024-06-05 21:02:32 +00:00
exit 0
;;
2023-09-08 18:43:08 +00:00
esac