neodotfiles/.shell-scripts/rofi-wifi-menu.sh

51 lines
1.8 KiB
Bash
Raw Normal View History

2021-12-31 23:56:40 +00:00
#!/usr/bin/env bash
notify-send "Getting list of available Wi-Fi networks..."
2022-01-09 02:53:03 +00:00
nmcli -t d wifi rescan
2021-12-31 23:56:40 +00:00
# Get a list of available wifi connections and morph it into a nice-looking list
2022-01-09 02:53:03 +00:00
LIST=$(nmcli --fields SSID,SECURITY,BARS device wifi list | sed '/^--/d' | sed 1d | sed -E "s/WPA*.?\S/~~/g" | sed "s/~~ ~~/~~/g;s/802\.1X//g;s/--/~~/g;s/ *~/~/g;s/~ */~/g;s/_/ /g" | column -t -s '~')
2021-12-31 23:56:40 +00:00
2022-01-09 02:53:03 +00:00
# get current connection status
CONSTATE=$(nmcli -fields WIFI g)
if [[ "$CONSTATE" =~ "enabled" ]]; then
TOGGLE="Disable WiFi 睊"
elif [[ "$CONSTATE" =~ "disabled" ]]; then
TOGGLE="Enable WiFi 直"
2021-12-31 23:56:40 +00:00
fi
# Use rofi to select wifi network
2022-01-09 02:53:03 +00:00
CHENTRY=$(echo -e "$TOGGLE\n$LIST" | uniq -u | rofi -dmenu -selected-row 1 -p "Wi-Fi SSID: ")
2021-12-31 23:56:40 +00:00
# Get name of connection
2022-01-09 02:53:03 +00:00
CHSSID=$(echo "$CHENTRY" | sed 's/\s\{2,\}/\|/g' | awk -F "|" '{print $1}')
2021-12-31 23:56:40 +00:00
2022-01-09 02:53:03 +00:00
if [ "$CHENTRY" = "" ]; then
exit
elif [ "$CHENTRY" = "Enable WiFi 直" ]; then
2021-12-31 23:56:40 +00:00
nmcli radio wifi on
2022-01-09 02:53:03 +00:00
elif [ "$CHENTRY" = "Disable WiFi 睊" ]; then
2021-12-31 23:56:40 +00:00
nmcli radio wifi off
else
2022-01-09 02:53:03 +00:00
# get list of known connections
KNOWNCON=$(nmcli connection show)
# 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"
2021-12-31 23:56:40 +00:00
else
2022-01-09 02:53:03 +00:00
if [[ "$CHENTRY" =~ "" ]]; then
WIFIPASS=$(echo " Press Enter if network is saved" | rofi -dmenu -p " WiFi Password: " -lines 1 )
fi
if nmcli dev wifi con "$CHSSID" password "$WIFIPASS"
then
notify-send 'Connection successful'
else
notify-send 'Connection failed'
2021-12-31 23:56:40 +00:00
fi
fi
fi