2023-09-08 18:43:08 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# ***This script was made by Clay Gomera (Drake)***
|
2024-06-05 21:02:32 +00:00
|
|
|
# - Description: A simple wifi script for fuzzel
|
|
|
|
# - Dependencies: fuzzel, NetworkManager
|
2023-09-08 18:43:08 +00:00
|
|
|
|
|
|
|
#######################
|
|
|
|
## Main manu options ##
|
|
|
|
#######################
|
|
|
|
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-19 01:20:35 +00:00
|
|
|
## These variables will store specific information about the wireless interface
|
2023-09-10 06:29:11 +00:00
|
|
|
wifi_info=$(nmcli dev | awk '/wifi/ {print $1,$3; exit}')
|
|
|
|
read -r wlan constate <<< "$wifi_info"
|
2023-09-08 18:43:08 +00:00
|
|
|
|
2024-06-19 01:20:35 +00:00
|
|
|
## This function uses nmcli to turn off wifi and then sends a notification
|
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
|
|
|
}
|
|
|
|
|
2024-06-19 01:20:35 +00:00
|
|
|
## This function uses nmcli to turn on wifi and then sends a notification
|
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
|
|
|
}
|
|
|
|
|
2024-06-19 01:20:35 +00:00
|
|
|
## This function uses nmcli and the $wlan and $constate variables to disconnect from the wifi network and then sends a notification
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-19 01:20:35 +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
|
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,SECURITY dev wifi list | sed -n '1!p' | $RUNNER -i -l 10 -p "[ Select a Wifi Network] ")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-06-19 01:20:35 +00:00
|
|
|
## This function will store the WiFi password in the $pass variable
|
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=""
|
2023-09-10 06:29:11 +00:00
|
|
|
else
|
2024-06-19 01:20:35 +00:00
|
|
|
pass=$($RUNNER -l 0 --password -p "[Enter Password ] " );
|
2024-06-05 21:02:32 +00:00
|
|
|
if [ -z "$pass" ]; then
|
|
|
|
exit 0
|
2023-09-10 06:29:11 +00:00
|
|
|
fi
|
|
|
|
fi
|
2023-09-08 18:43:08 +00:00
|
|
|
}
|
|
|
|
|
2024-06-19 01:20:35 +00:00
|
|
|
## This function will actually connect to the chosen WiFi network using the $bssid and $pass variables
|
2023-09-08 18:43:08 +00:00
|
|
|
action() {
|
2024-06-05 21:02:32 +00:00
|
|
|
if [ -n "$pass" ]; then
|
2023-09-10 06:29:11 +00:00
|
|
|
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 ##
|
|
|
|
##########
|
2024-03-07 15:05:41 +00:00
|
|
|
cases=$(echo -e "$options" | $RUNNER -i -l 6 -p "[ Wifi Settings] " ) # main menu prompt
|
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
|