scripts/rofi/rofi-wifi-menu.sh

83 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
FIELDS=SSID,SECURITY,ACTIVE
if [ -r "${HOME}/.dotfiles/rofi/wifi.rasi" ]; then
source "${HOME}/.dotfiles/rofi/wifi.rasi"
fi
CURRSSID=$(LANGUAGE=C nmcli -t -f active,bssid dev wifi | awk -F: '$1 ~ /^yes/ {print $2}')
LIST=$(nmcli --fields "$FIELDS" device wifi list | sed '/^--/d' | sed 's/no/ /' | sed 's/yes/* /')
# Gives a list of known connections so we can parse it later
KNOWNCON=$(nmcli connection show)
# Really janky way of telling if there is currently a connection
CONSTATE=$(nmcli -fields WIFI g)
if [[ "$CONSTATE" =~ "enabled" ]]; then
TOGGLE="Toggle Off"
elif [[ "$CONSTATE" =~ "disabled" ]]; then
TOGGLE="Toggle On"
fi
CHENTRY=$(echo -e "${TOGGLE}\nManual\n${LIST}" | rofi -dmenu -i -p "Wi-Fi SSID: " "" -width 45 -l 1 -theme ${HOME}/.dotfiles/rofi/sidebar.rasi)
# If the user inputs "manual" as their SSID in the start window, it will bring them to this screen
if [ "$CHENTRY" = "manual" ] ; then
#
# Manual entry of the SSID and password
MSSID=$(echo "enter the SSID of the network (SSID,password)" | rofi -dmenu -p "Manual Entry: " "" -width 45 -l 1 -theme ${HOME}/.dotfiles/rofi/sidebar.rasi)
MPASS=$(echo "$MSSID" | cut -d',' -f2-)
MSSID=$(echo "$MSSID" | cut -d',' -f1)
# If the user entered a manual password, then use the password nmcli command
if [ "$MPASS" = "" ]; then
nmcli dev wifi con "$MSSID"
else
nmcli dev wifi con "$MSSID" password "$MPASS"
fi
elif [ "$CHENTRY" = "Toggle On" ]; then
nmcli radio wifi on
elif [ "$CHENTRY" = "Toggle Off" ]; then
nmcli radio wifi off
else
# If the connection is already in use, then this will still be able to get the SSID
CHSSID=$(echo "$CHENTRY" | cut -d' ' -f1)
CURRSSID=$(echo "$CHENTRY" | grep '\*')
if [ -n "$CURRSSID" ]; then
nmcli con down "$CHSSID"
else
# Request password if required
if [[ "$CHENTRY" =~ "WPA2" ]] || [[ "$CHENTRY" =~ "WEP" ]]; then
# Automatically connect to known network
if grep -q "^$CHSSID" <<< "$KNOWNCON"; then
nmcli con up "$CHSSID"
else
WIFIPASS=$(echo "Password" | rofi -dmenu -p "Password: " "" -width 45 -l 1 -theme "${HOME}"/.dotfiles/rofi/sidebar.rasi)
# Attempt to connect with known password if none provided (should fail)
if [[ "$WIFIPASS" == "Password" ]]; then
nmcli dev wifi connect "$CHSSID"
# Connect with password
else
nmcli dev wifi con "$CHSSID" password "$WIFIPASS"
fi
fi
# Connect to unprotected wifi
else
RET=$(nmcli dev wifi connect "$CHSSID")
# Offer to open VPN menu
if [[ "$RET" =~ 'successfully activated' ]] && grep -qP "(wg|tun)0" <<< "$(ip addr)"; then
swaynag --message "This is public Wi-Fi and you are not using your VPN." -z "Open VPN settings?" "${HOME}/scripts/rofi/rofi-openvpn.sh"
fi
fi
fi
fi