167 lines
4.2 KiB
Text
167 lines
4.2 KiB
Text
|
#!/usr/bin/env bash
|
||
|
|
||
|
# ***This script was made by Clay Gomera (Drake)***
|
||
|
# - Description: A simple wifi script for rofi/dmenu/wofi
|
||
|
# - Dependencies: {rofi||dmenu||wofi}, NetworkManager, io.elementary.capnet-assist
|
||
|
|
||
|
#######################
|
||
|
## 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"
|
||
|
|
||
|
#####
|
||
|
## These variables will store specific
|
||
|
## information about the wireless
|
||
|
## interface
|
||
|
#####
|
||
|
wlan=$(nmcli dev | grep wifi | sed 's/ \{2,\}/|/g' | cut -d '|' -f1 | head -1)
|
||
|
constate=$(nmcli dev | grep wifi | sed 's/ \{2,\}/|/g' | cut -d '|' -f3 | head -1)
|
||
|
currentwfi=$(nmcli dev | grep wifi | sed 's/ \{2,\}/|/g' | cut -d '|' -f4 | head -1)
|
||
|
|
||
|
#####
|
||
|
## This function uses nmcli to turn
|
||
|
## off wifi and then sends a
|
||
|
## notification
|
||
|
#####
|
||
|
## param: none
|
||
|
## return: void
|
||
|
#####
|
||
|
turnoff() {
|
||
|
nmcli radio wifi off && notify-send "WiFi has been turned off";
|
||
|
}
|
||
|
|
||
|
#####
|
||
|
## This function uses nmcli to turn
|
||
|
## on wifi and then sends a
|
||
|
## notification
|
||
|
#####
|
||
|
## param: none
|
||
|
## return: void
|
||
|
#####
|
||
|
turnon() {
|
||
|
nmcli radio wifi on && notify-send "WiFi has been turned on";
|
||
|
}
|
||
|
|
||
|
#####
|
||
|
## This function uses nmcli and the
|
||
|
## $wlan and $constate variables to
|
||
|
## disconnect from the wifi network
|
||
|
## and then sends a notification
|
||
|
#####
|
||
|
## param: none
|
||
|
## return: void
|
||
|
#####
|
||
|
disconnect() {
|
||
|
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
|
||
|
}
|
||
|
|
||
|
#####
|
||
|
## 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
|
||
|
#####
|
||
|
connect() {
|
||
|
notify-send "Scannig networks..." && nmcli dev wifi rescan;
|
||
|
bssid=$(nmcli device wifi list | sed -n '1!p' | cut -b 9- | $RUNNER -i -p " Select a Wifi Network" | cut -d' ' -f1);
|
||
|
}
|
||
|
|
||
|
#####
|
||
|
## This function will store the WiFi
|
||
|
## password in the $pass variable
|
||
|
#####
|
||
|
## param: none
|
||
|
## return: string
|
||
|
#####
|
||
|
password() {
|
||
|
# 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
|
||
|
pass=$($RUNNER -i -W 250 -L 1 -p "Enter Password " --password);
|
||
|
}
|
||
|
|
||
|
#####
|
||
|
## This function will actually connect
|
||
|
## to the chosen WiFi network using the
|
||
|
## $bssid and $pass variables
|
||
|
#####
|
||
|
## param: none
|
||
|
## return: void
|
||
|
#####
|
||
|
action() {
|
||
|
nmcli device wifi connect "$bssid" password "$pass" || nmcli device wifi connect "$bssid";
|
||
|
}
|
||
|
|
||
|
#####
|
||
|
## This function will check if the
|
||
|
## connection works
|
||
|
#####
|
||
|
## param: none
|
||
|
## return: void
|
||
|
#####
|
||
|
check() {
|
||
|
notify-send "Checking if connection was successful";
|
||
|
sleep 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 ##
|
||
|
##########
|
||
|
cases=$(echo -e "$options" | $RUNNER -i -L 8 -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;
|
||
|
if [ -n "$pass" ]; then # if the user typed a password
|
||
|
action;
|
||
|
if [ "$constate" = "connected" ]; then # if the connection was successful
|
||
|
check;
|
||
|
else
|
||
|
notify-send "Connection error"; # if not, connection error
|
||
|
exit 1;
|
||
|
fi
|
||
|
else
|
||
|
exit 1; # if not, exit the script
|
||
|
fi
|
||
|
else
|
||
|
exit 1; # if not, exit the script
|
||
|
fi
|
||
|
;;
|
||
|
"$option5")
|
||
|
io.elementary.capnet-assist;
|
||
|
;;
|
||
|
"$option6")
|
||
|
exit 0;
|
||
|
esac
|