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"
|
|
|
|
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
|
|
|
#####
|
|
|
|
## This variable will grab the wireless
|
|
|
|
## interface name
|
|
|
|
#####
|
2022-09-13 01:06:55 +00:00
|
|
|
wlan=$(nmcli dev | grep wifi | sed 's/ \{2,\}/|/g' | cut -d '|' -f1 | head -1)
|
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() {
|
|
|
|
nmcli radio wifi off
|
|
|
|
notify-send "WiFi has been turned off"
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
nmcli radio wifi on
|
|
|
|
notify-send "WiFi has been turned on"
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:36:18 +00:00
|
|
|
#####
|
|
|
|
## This function uses nmcli and the
|
|
|
|
## $wlan variable to disconnect
|
|
|
|
## from the wifi network and
|
|
|
|
## then sends a notification
|
|
|
|
#####
|
|
|
|
## param: none
|
|
|
|
## return: void
|
|
|
|
#####
|
2022-09-13 01:06:55 +00:00
|
|
|
disconnect() {
|
|
|
|
nmcli device disconnect "$wlan"
|
2023-09-04 17:36:18 +00:00
|
|
|
notify-send "WiFi has been disconnected"
|
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-09-04 17:36:18 +00:00
|
|
|
notify-send "Scannig WiFi networks, please wait"
|
|
|
|
nmcli dev wifi rescan
|
2022-09-13 01:06:55 +00:00
|
|
|
sleep 1
|
2023-06-25 03:24:12 +00:00
|
|
|
bssid=$(nmcli device wifi list | sed -n '1!p' | cut -b 9- | dmenu -i -l 10 -p "Select a Wifi Network " | cut -d' ' -f1)
|
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-09-04 17:36:18 +00:00
|
|
|
# TODO Create a condition to check if the chosen network is open or not, that
|
|
|
|
# way this will be skipped of it's open
|
|
|
|
# TODO What if the network is already saved? The user doesn't need to type the
|
|
|
|
# password again
|
2023-06-24 17:00:22 +00:00
|
|
|
pass=$(echo " " | dmenu -P -i -p "Enter Password ")
|
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-09-04 17:36:18 +00:00
|
|
|
# TODO Create a condition to check if the chosen network is open or not (check
|
|
|
|
# password funcion)
|
|
|
|
# TODO What if the network is already saved? The user doesn't need to type the
|
|
|
|
# password again
|
2022-09-13 01:06:55 +00:00
|
|
|
nmcli device wifi connect "$bssid" password "$pass" || nmcli device wifi connect "$bssid"
|
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 check if the
|
|
|
|
## connection works
|
|
|
|
#####
|
|
|
|
## param: none
|
|
|
|
## return: void
|
|
|
|
#####
|
2022-09-13 01:06:55 +00:00
|
|
|
check() {
|
|
|
|
notify-send "Checking if connection was successful"
|
|
|
|
sleep 1
|
|
|
|
currentwfi=$(nmcli dev | grep wifi | sed 's/ \{2,\}/|/g' | cut -d '|' -f4 | head -1)
|
|
|
|
if ping -q -c 2 -W 2 google.com >/dev/null; then
|
|
|
|
notify-send "You are now connected to $currentwfi and internet is working properly"
|
|
|
|
else
|
|
|
|
notify-send "Your internet is not working :("
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
"$option1")
|
|
|
|
turnon;;
|
|
|
|
"$option2")
|
|
|
|
turnoff;;
|
|
|
|
"$option3")
|
|
|
|
disconnect;;
|
|
|
|
"$option4")
|
|
|
|
connect;
|
|
|
|
if [ -n "$bssid" ]; then # if the user chooses a network
|
|
|
|
password;
|
|
|
|
action;
|
|
|
|
sleep 5;
|
|
|
|
check;
|
|
|
|
else
|
|
|
|
exit 0; # if not, exit the script
|
|
|
|
fi;;
|
|
|
|
"$option5")
|
|
|
|
"$BROWSER" http://networkcheck.kde.org;; # $BROWSER reffers to a global variable set in .xinitrc/.bash_profile
|
|
|
|
"$option6")
|
|
|
|
exit 0;
|
|
|
|
esac
|