dmenu-scripts/dmenu-wifi

39 lines
1.6 KiB
Plaintext
Raw Normal View History

2022-03-26 20:11:10 +01:00
#!/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
2022-04-05 16:59:13 +02:00
toggle="睊 Disable Wi-Fi"
2022-03-26 20:11:10 +01:00
elif [[ "$connected" =~ "disabled" ]]; then
2022-04-05 16:59:13 +02:00
toggle="直 Enable Wi-Fi"
2022-03-26 20:11:10 +01:00
fi
# Use dmenu to select wifi network
2022-04-22 12:48:51 +02:00
chosen_network=$(printf '%s\n%s' "$toggle" "$wifi_list" | uniq -u | dmenu -p "Wi-Fi SSID: " )
2022-03-26 20:11:10 +01:00
# Get name of connection
2022-04-22 12:48:51 +02:00
chosen_id=$(printf '%s\n' "${chosen_network:3}" | xargs)
2022-03-26 20:11:10 +01:00
if [ "$chosen_network" = "" ]; then
2022-04-05 16:59:13 +02:00
exit
2022-03-26 20:11:10 +01:00
elif [ "$chosen_network" = "直 Enable Wi-Fi" ]; then
2022-04-05 16:59:13 +02:00
nmcli radio wifi on
2022-03-26 20:11:10 +01:00
elif [ "$chosen_network" = "睊 Disable Wi-Fi" ]; then
2022-04-05 16:59:13 +02:00
nmcli radio wifi off
2022-03-26 20:11:10 +01:00
else
2022-04-05 16:59:13 +02:00
# 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)
2022-04-22 12:48:51 +02:00
if [[ $(printf '%s\n' "$saved_connections" | grep -w "$chosen_id") = "$chosen_id" ]]; then
2022-04-05 16:59:13 +02:00
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
2022-03-26 20:11:10 +01:00
fi