2024-08-27 22:07:40 +07:00

49 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Load NetworkManager
network_status=$(ps aux | grep NetworkManager | grep root)
# Если служба не запущена
if [ "$network_status" = "" ]; then
# TODO: Hide password entry
rofi -dmenu -p "Root Password: " | sudo -S NetworkManager
fi
# Starts a scan of available broadcasting SSIDs
# nmcli dev wifi rescan
notify-send "Getting list of available Wi-Fi networks..."
wifi_list=$(nmcli --fields "SECURITY,SSID" device wifi list | sed 1d | sed 's/ */ /g' | sed -E "s/WPA*.?\S/ /g" | sed "s/^--/ /g" | sed "s/ //g" | sed "/--/d")
# Gives a list of known connections so we can parse it later
connected=$(nmcli -fields WIFI g)
if [[ "$connected" =~ "enabled" ]]; then
toggle="󱛅 Disable Wi-Fi"
elif [[ "$connected" =~ "disabled" ]]; then
toggle="󱚽 Enable Wi-Fi"
fi
chosen_network=$(echo -e "$toggle\n$wifi_list" | uniq -u | rofi -dmenu -theme ~/.config/bspwm/themes/bspwm_nord/rofi/menu.rasi -i -selected-row 1 -p "Wi-Fi SSID: " )
chosen_id=$(echo "${chosen_network:3}" | xargs)
# Parses the list of preconfigured connections to see if it already contains the chosen SSID. This speeds up the connection process
if [ "$chosen_network" = "" ]; then
exit
elif [ "$chosen_network" = "󱚽 Enable Wi-Fi" ]; then
nmcli radio wifi on
elif [ "$chosen_network" = "󱛅 Disable Wi-Fi" ]; then
nmcli radio wifi off
else
# Message to show when connection is activated successfully
success_message="You are now connected to the Wi-Fi network \"$chosen_id\"."
# Get known connections
saved_connections=$(nmcli -g NAME connection)
if [[ $(echo "$saved_connections" | grep -w "$chosen_id") = "$chosen_id" ]]; then
nmcli connection up id "$chosen_id" | grep "successfully" && notify-send "Connection Established" "$success_message"
else
if [[ "$chosen_network" =~ "" ]]; then
wifi_password=$(rofi -dmenu -p "Password: " )
fi
nmcli device wifi connect "$chosen_id" password "$wifi_password" | grep "successfully" && notify-send "Connection Established" "$success_message"
fi
fi