2024-03-28 21:40:01 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# ***This script was made by Clay Gomera (Drake)***
|
|
|
|
# - Description: A simple wifi dmenu script
|
|
|
|
# - Dependencies: dmenu, NetworkManager
|
|
|
|
|
2024-06-03 20:01:31 +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-06-03 20:01:31 +00:00
|
|
|
## These variables will store specific information about the wireless interface
|
2024-03-28 21:40:01 +00:00
|
|
|
#####
|
|
|
|
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() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#####
|
|
|
|
## This function uses nmcli to turn on wifi and then sends a notification
|
|
|
|
#####
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#####
|
|
|
|
## 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
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#####
|
|
|
|
## 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")
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#####
|
|
|
|
## This function will store the WiFi password in the $pass variable
|
|
|
|
#####
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#####
|
|
|
|
## This function will actually connect to the chosen WiFi network using the
|
|
|
|
## $bssid and $pass variables
|
|
|
|
#####
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
##########
|
|
|
|
## main ##
|
|
|
|
##########
|
2024-06-03 20:01:31 +00:00
|
|
|
cases=$(echo -e "$options" | dmenu -l 6 -i -p " Wifi Settings")
|
|
|
|
if [ -z "$cases" ]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-03-28 21:40:01 +00:00
|
|
|
case "$cases" in
|
|
|
|
"$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
|