-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd-wifi
executable file
·43 lines (38 loc) · 1.69 KB
/
d-wifi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash
source d-var.conf
a1=" Disable Wi-Fi"
a2=" Enable Wi-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="$a1"
elif [[ "$connected" =~ "disabled" ]]; then
toggle="$a2"
fi
chosen_network=$(echo -e "$toggle\n$wifi_list" | uniq -u | $D_MENU "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" = "$a2" ]; then
nmcli radio wifi on
elif [ "$chosen_network" = "$a1" ]; 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=$(echo '' | $D_MENU -password "Password: " )
fi
nmcli device wifi connect "$chosen_id" password "$wifi_password" | grep "successfully" && notify-send "Connection Established" "$success_message"
fi
fi