scripts/wofi/wofi-wifi-menu.sh

87 lines
3.0 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2022-09-17 21:03:01 +00:00
# Modified from: https://github.com/fourstepper/wofi-wifi-menu
# Starts a scan of available broadcasting SSIDs
# nmcli dev wifi rescan
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
FIELDS=SSID,SECURITY
if [ -r "$DIR/config" ]; then
2023-01-06 19:41:31 +00:00
source "$DIR/config"
elif [ -r "$HOME/.dotfiles/wofi/wifi" ]; then
2023-01-06 19:41:31 +00:00
source "$HOME/.dotfiles/wofi/wifi"
else
2023-01-06 19:41:31 +00:00
echo "WARNING: config file not found! Using default values."
fi
LIST=$(nmcli --fields "$FIELDS" device wifi list | sed '/^--/d')
# 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)
CURRSSID=$(LANGUAGE=C nmcli -t -f active,ssid dev wifi | awk -F: '$1 ~ /^yes/ {print $2}')
if [[ ! -z $CURRSSID ]]; then
2023-01-06 19:41:31 +00:00
HIGHLINE=$(echo "$(echo "$LIST" | awk -F "[ ]{2,}" '{print $1}' | grep -Fxn -m 1 "$CURRSSID" | awk -F ":" '{print $1}') + 1" | bc )
fi
if [[ "$CONSTATE" =~ "enabled" ]]; then
2023-01-06 19:41:31 +00:00
TOGGLE="toggle off"
elif [[ "$CONSTATE" =~ "disabled" ]]; then
2023-01-06 19:41:31 +00:00
TOGGLE="toggle on"
fi
2022-09-17 21:02:46 +00:00
CHENTRY=$(echo -e "$TOGGLE\nmanual\n$LIST" | uniq -u | wofi -s $HOME/.dotfiles/wofi/style.css -i -d --prompt "Wi-Fi SSID: " -c $HOME/.dotfiles/wofi/sidebar)
if [[ $CHENTRY == "" ]]; then
2023-01-07 13:16:47 +00:00
exit
fi
#echo "$CHENTRY"
CHSSID=$(echo "$CHENTRY" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $1}')
#echo "$CHSSID"
# If the user inputs "manual" as their SSID in the start window, it will bring them to this screen
if [ "$CHENTRY" = "manual" ] ; then
2023-01-06 19:41:31 +00:00
# Manual entry of the SSID and password (if appplicable)
MSSID=$(echo "enter the SSID of the network (SSID,password)" | wofi -s $HOME/.dotfiles/wofi/style.css -d "Manual Entry: " -c $HOME/.dotfiles/wofi/sidebar)
# Separating the password from the entered string
MPASS=$(echo "$MSSID" | awk -F "," '{print $2}')
2023-01-06 19:41:31 +00:00
#echo "$MSSID"
#echo "$MPASS"
2023-01-06 19:41:31 +00:00
# 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
2023-01-06 19:41:31 +00:00
nmcli radio wifi on
elif [ "$CHENTRY" = "toggle off" ]; then
2023-01-06 19:41:31 +00:00
nmcli radio wifi off
else
2023-01-06 19:41:31 +00:00
# If the connection is already in use, then this will still be able to get the SSID
if [ "$CHSSID" = "*" ]; then
CHSSID=$(echo "$CHENTRY" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $3}')
fi
# Parses the list of preconfigured connections to see if it already contains the chosen SSID. This speeds up the connection process
if [[ $(echo "$KNOWNCON" | grep "$CHSSID") = "$CHSSID" ]]; then
nmcli con up "$CHSSID"
else
if [[ "$CHENTRY" =~ "WPA2" ]] || [[ "$CHENTRY" =~ "WEP" ]]; then
WIFIPASS=$(echo "if connection is stored, hit enter" | wofi -s $HOME/.dotfiles/wofi/style.css -P -d --prompt "password" -c $HOME/.dotfiles/wofi/sidebar)
fi
nmcli dev wifi con "$CHSSID" password "$WIFIPASS"
fi
fi