neodotfiles/user/.config/rofi/scripts/rofi_wifi

100 lines
2.4 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)***
# - Description: A simple wifi rofi script
2023-06-20 14:29:23 +00:00
# - Dependencies: rofi, NetworkManager, io.elementary.capnet-assist
2022-09-13 01:06:55 +00:00
2023-06-20 14:29:23 +00:00
## MENU PROMPT ##
menu="rofi -dmenu -i -p"
2022-09-13 01:06:55 +00:00
## MAIN OPTIONS ##
2022-09-19 19:06:15 +00:00
option1=" Turn on WiFi"
option2=" Turn off WiFi"
2023-06-20 14:29:23 +00:00
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-06-20 14:29:23 +00:00
## GRAB WIFI INTERFACE ##
2022-09-13 01:06:55 +00:00
wlan=$(nmcli dev | grep wifi | sed 's/ \{2,\}/|/g' | cut -d '|' -f1 | head -1)
2023-06-20 14:29:23 +00:00
2022-09-13 01:06:55 +00:00
## TURN OFF WIFI FUNCTION ##
turnoff() {
nmcli radio wifi off
notify-send "WiFi has been turned off"
}
## TURN ON WIFI FUNCTION ##
turnon() {
nmcli radio wifi on
notify-send "WiFi has been turned on"
}
## DISCONNECT WIFI FUNCTION ##
disconnect() {
nmcli device disconnect "$wlan"
sleep 1
constate=$(nmcli dev | grep wifi | sed 's/ \{2,\}/|/g' | cut -d '|' -f3 | head -1)
if [ "$constate" = "disconnected" ]; then
notify-send "WiFi has been disconnected"
fi
}
## CONNECT FUNCTION ##
connect() {
notify-send "Scannig networks, please wait"
sleep 1
2023-06-20 14:29:23 +00:00
bssid=$(nmcli device wifi list | sed -n '1!p' | cut -b 9- | $menu " Select a Wifi Network  " | cut -d' ' -f1)
}
2022-09-13 01:06:55 +00:00
## SELECT PASSWORD FUNCTION ##
password() {
2023-06-20 14:29:23 +00:00
pass=$(echo " " | $menu " Enter Password  " -password)
}
2022-09-13 01:06:55 +00:00
## MAIN CONNECTION COMMAND ##
action() {
nmcli device wifi connect "$bssid" password "$pass" || nmcli device wifi connect "$bssid"
2023-06-20 14:29:23 +00:00
}
2022-09-13 01:06:55 +00:00
## CHECKING IF WIFI IS WORKING
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
}
## MAIN ACTION COMMANDS ##
2023-06-20 14:29:23 +00:00
cases=$(echo -e "$options" | $menu "  Wifi Settings " )
if [ -n "$cases" ]; then
case "$cases" in
$option1)
turnon;;
$option2)
turnoff;;
$option3)
disconnect;;
$option4)
connect;
if [ -n "$bssid" ]; then
password;
action;
check;
else
exit 0;
fi;;
$option5)
io.elementary.capnet-assist;;
$option6)
exit 0;
esac
else
exit 0;
fi