neodotfiles/user/.config/suckless/dmenu/scripts/dmenu_wifi

151 lines
3.8 KiB
Text
Raw Normal View History

2022-09-13 01:06:55 +00:00
#!/usr/bin/env bash
# ***This script was made by Clay Gomera (Drake)***
2022-12-20 00:15:21 +00:00
# - Description: A simple wifi dmenu script
# - Dependencies: dmenu, NetworkManager
2022-09-13 01:06:55 +00:00
2023-09-04 17:36:18 +00:00
#######################
## Main manu options ##
#######################
2023-06-24 17:00:22 +00:00
option1=" Turn on WiFi"
option2=" Turn off WiFi"
option3="󱛅 Disconnect WiFi"
option4="󱛃 Connect WiFi"
option5="󱛆 Setup captive portal"
2023-10-03 14:40:20 +00:00
option6="󱎘 Exit"
2022-09-13 01:06:55 +00:00
options="$option1\n$option2\n$option3\n$option4\n$option5\n$option6"
2023-09-04 17:36:18 +00:00
#####
2023-10-03 14:40:20 +00:00
## These variables will store specific
## information about the wireless
## interface
2023-09-04 17:36:18 +00:00
#####
2023-10-03 14:40:20 +00:00
wifi_info=$(nmcli dev | awk '/wifi/ {print $1,$3; exit}')
read -r wlan constate <<< "$wifi_info"
2023-09-04 17:36:18 +00:00
#####
## This function uses nmcli to turn
## off wifi and then sends a
## notification
#####
## param: none
## return: void
#####
2022-09-13 01:06:55 +00:00
turnoff() {
2023-10-03 14:40:20 +00:00
nmcli radio wifi off && notify-send "WiFi has been turned off";
2022-09-13 01:06:55 +00:00
}
2023-09-04 17:36:18 +00:00
#####
## This function uses nmcli to turn
## on wifi and then sends a
## notification
#####
## param: none
## return: void
#####
2022-09-13 01:06:55 +00:00
turnon() {
2023-10-03 14:40:20 +00:00
nmcli radio wifi on && notify-send "WiFi has been turned on";
2022-09-13 01:06:55 +00:00
}
2023-09-04 17:36:18 +00:00
#####
## This function uses nmcli and the
2023-10-03 14:40:20 +00:00
## $wlan and $constate variables to
## disconnect from the wifi network
## and then sends a notification
2023-09-04 17:36:18 +00:00
#####
## param: none
## return: void
#####
2022-09-13 01:06:55 +00:00
disconnect() {
2023-10-03 14:40:20 +00:00
if [ "$constate" = "disconnected" ]; then
notify-send "WiFi is already disconnected";
elif [ "$constate" = "connected" ]; then
nmcli device disconnect "$wlan" && notify-send "Wifi has been disconnected";
else
exit 1;
fi
2022-09-13 01:06:55 +00:00
}
2023-09-04 17:36:18 +00:00
#####
## 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
#####
## param: none
## return: string
#####
2022-09-13 01:06:55 +00:00
connect() {
2023-10-03 14:40:20 +00:00
notify-send "Scannig 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");
bssid=$(echo "$wifinet" | cut -d' ' -f1)
ssid=$(echo "$wifinet" | cut -d' ' -f3)
2023-05-26 06:32:03 +00:00
}
2022-09-13 01:06:55 +00:00
2023-09-04 17:36:18 +00:00
#####
## This function will store the WiFi
## password in the $pass variable
#####
## param: none
## return: string
#####
2022-09-13 01:06:55 +00:00
password() {
2023-10-03 14:40:20 +00:00
if nmcli connection show | grep -q "$ssid"; then # check if the network is already saved
return 0; # no password is required
elif nmcli -f BSSID,SECURITY dev wifi list | sed -n '1!p' | grep "$bssid" | awk '{print $2}' | grep -q -- "--"; then # check if the network is open
return 0; # no password is required
else
pass=$(dmenu -i -l 1 -P -p "Enter Password " --password);
if [ -n "$pass" ]; then # if the user gave a password
return 0;
else
exit 1; # if not, exit the script
fi
fi
2023-05-26 06:32:03 +00:00
}
2022-09-13 01:06:55 +00:00
2023-09-04 17:36:18 +00:00
#####
## This function will actually connect
## to the chosen WiFi network using the
## $bssid and $pass variables
#####
## param: none
## return: void
#####
2022-09-13 01:06:55 +00:00
action() {
2023-10-03 14:40:20 +00:00
if [ -n "$pass" ]; then # we need to check again if the $pass variable exists
nmcli dev wifi connect "$bssid" password "$pass"
else # if not, that means that the password() function ended in one of the first two conditions, the network is saved or open
nmcli dev wifi connect "$bssid";
fi
2022-09-13 01:06:55 +00:00
}
2023-09-04 17:36:18 +00:00
##########
## main ##
##########
cases=$(echo -e "$options" | dmenu -l 6 -i -p " Wifi Settings" ) # main menu prompt
case "$cases" in
2023-10-03 14:40:20 +00:00
"$option1")
turnon;
;;
"$option2")
turnoff;
;;
"$option3")
disconnect;
;;
"$option4")
if connect; then # if the user chooses a network
password; # this function will exit the script if the user didn't put a password
action;
else
exit 1; # if not, exit the script
fi
;;
"$option5")
io.elementary.capnet-assist;
;;
"$option6")
exit 0;
2023-09-04 17:36:18 +00:00
esac