diff --git a/dmenuwifi b/dmenuwifi new file mode 100755 index 0000000..e2aa503 --- /dev/null +++ b/dmenuwifi @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +quick-notify "Getting list of available Wi-Fi networks..." +# Get a list of available wifi connections and morph it into a nice-looking list +wifi_list=$(nmcli --fields "SECURITY,SSID" device wifi list --rescan yes | sed 1d | sed 's/ */ /g' | sed -E "s/WPA*.?\S/ /g" | sed "s/^--/ /g" | sed "s/ //g" | sed "/--/d") + +connected=$(nmcli -fields WIFI g) +if [[ "$connected" =~ "enabled" ]]; then + toggle="睊 Disable Wi-Fi" +elif [[ "$connected" =~ "disabled" ]]; then + toggle="直 Enable Wi-Fi" +fi + +# Use dmenu to select wifi network +chosen_network=$(echo -e "$toggle\n$wifi_list" | uniq -u | dmenu -p "Wi-Fi SSID: " ) +# Get name of connection +chosen_id=$(echo "${chosen_network:3}" | xargs) + +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 saved 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" && quick-notify "Connection Established" "$success_message" + else + if [[ "$chosen_network" =~ "" ]]; then + wifi_password=$(dmenu -p "Password: " ) + fi + nmcli device wifi connect "$chosen_id" password "$wifi_password" | grep "successfully" && quick-notify "Connection Established" "$success_message" + fi +fi