-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrofi-wifi
executable file
·196 lines (185 loc) · 5.67 KB
/
rofi-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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# #!/usr/bin/env bash
#
# # *** Script by Clay Gomera (Drake) ***
# # Description: A simple WiFi management script using dmenu and NetworkManager
# # Dependencies: dmenu, NetworkManager, notify-send
# # Modified by me to use rofi
#
# # Main menu options
# option1=" Turn on WiFi"
# option2=" Turn off WiFi"
# option3=" Disconnect WiFi"
# option4=" Connect WiFi"
# option5=" Setup captive portal"
# option6=" Exit"
# options="$option1\n$option2\n$option3\n$option4\n$option5\n$option6"
#
# # Retrieve wireless interface information
# wifi_info=$(nmcli dev | awk '/wifi/ {print $1,$3; exit}')
# read -r wlan state <<<"$wifi_info"
#
# # Function to turn off WiFi
# turnoff() {
# nmcli radio wifi off
# if [ $? -eq 0 ]; then
# notify-send "WiFi has been turned off"
# else
# notify-send "Failed to turn off WiFi"
# fi
# }
#
# # Function to turn on WiFi
# turnon() {
# nmcli radio wifi on
# if [ $? -eq 0 ]; then
# notify-send "WiFi has been turned on"
# else
# notify-send "Failed to turn on WiFi"
# fi
# }
#
# # Function to disconnect WiFi
# disconnect() {
# if [ "$state" = "disconnected" ]; then
# notify-send "WiFi is already disconnected"
# elif [ "$state" = "connected" ]; then
# nmcli device disconnect "$wlan"
# if [ $? -eq 0 ]; then
# notify-send "WiFi has been disconnected"
# else
# notify-send "Failed to disconnect WiFi"
# fi
# else
# notify-send "Unknown WiFi state"
# fi
# }
#
# # Function to select and connect to a WiFi network
# connect() {
# notify-send -t 5000 "Scanning networks..."
# nmcli dev wifi rescan
# local wifinet
# wifinet=$(nmcli -f SSID,BARS,SECURITY dev wifi list | sed -n '1!p' | sed -E "s/WPA*.?\S/ /g" | sed "s/^--/ /g" | sed "s/ //g" | sed "/--/d" | rofi -dmenu -i -config "$HOME/.config/rofi/regular.rasi" -p " Wifi Network")
# if [ -z "$wifinet" ]; then
# exit 0
# fi
# bssid=$(echo "$wifinet" | awk '{print $1}')
# ssid=$(echo "$wifinet" | awk '{print $2}')
# }
#
# # Function to prompt for WiFi password
# password() {
#
# if nmcli connection show | awk -v ssid="$ssid" '$1 == ssid {found=1} END {exit !found}'; then
# pass=""
# elif nmcli -f BSSID,SECURITY dev wifi list | grep -w "$bssid" | awk '{print $2}' | grep -q -- "--"; then
# pass=""
# else
# pass=$(rofi -dmenu -l 1 -config "$HOME/.config/rofi/regular.rasi" -p " Enter Password")
# if [ -z "$pass" ]; then
# exit 0
# fi
# fi
# }
#
# # Function to connect to the selected WiFi network
# action() {
# if [ -n "$pass" ]; then
# nmcli dev wifi connect "$bssid" password "$pass"
# else
# nmcli dev wifi connect "$bssid"
# fi
# if [ $? -eq 0 ]; then
# notify-send "Connected to $ssid"
# else
# notify-send "Failed to connect to $ssid"
# fi
# }
#
# # Show menu and handle user choice
# case=$(echo -e "$options" | rofi -dmenu -l 6 -i -config "$HOME/.config/rofi/regular.rasi" -p " WiFi Settings")
# if [ -z "$case" ]; then
# exit 0
# fi
#
# case "$case" in
# "$option1")
# turnon
# ;;
# "$option2")
# turnoff
# ;;
# "$option3")
# disconnect
# ;;
# "$option4")
# if connect; then
# if password; then
# action
# fi
# fi
# ;;
# "$option5")
# ${BROWSER:-xdg-open} http://networkcheck.kde.org
# ;;
# "$option6")
# exit 0
# ;;
# esac
#!/usr/bin/env bash
connect_wifi() {
notify-send "Fetching available Wi-Fi networks ..." -t 3000 -r 9991 -u normal
current=$(nmcli -t -f active,ssid dev wifi | awk -F: '/^yes/ {print $2}')
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")
if [ -n "$current" ]; then
wifi_list=$(echo "$wifi_list" | grep -v "$current")
wifi_list=" $current [Connected]\n$wifi_list"
fi
choice=$(echo -e "$toggle\n$wifi_list" | uniq -u | rofi -dmenu -i -p " Wifi" -selected-row 1 -config "$HOME/.config/rofi/regular.rasi")
if [ -z "$choice" ]; then
exit
fi
ssid=$(echo "$choice" | awk '{$1=""; print $0}' | sed 's/^ *//' | sed 's/ \[connected\]//')
if [[ "$choice" = "$toggle" ]]; then
nmcli radio wifi off
notify-send "Wi-Fi Disabled" -r 9991 -u normal -t 5000
exit
fi
if [[ "$ssid" = "$current" ]]; then
notify-send "Already connected to $ssid" -r 9991 -u normal -t 5000
exit
else
saved=$(nmcli -g NAME connection)
if [[ $(echo "$saved" | grep -w "$ssid") = "$ssid" ]]; then
nmcli connection up id "$ssid" | grep "successfully" && notify-send "Connection Established" "Connected to $ssid" -t 5000 -r 9991 && exit
fi
if [[ "$choice" =~ "" ]]; then
password=$(rofi -dmenu -p "Password: " -l 0 -config "$HOME/.config/rofi/regular.rasi")
fi
if nmcli device wifi connect "$ssid" password "$password" | grep "successfully"; then
notify-send "Connection Established" "Connected to $ssid" -t 5000 -r 9991
else
notify-send "Connection Failed" "Could not connect to $ssid" -t 5000 -r 9991
fi
fi
}
status=$(nmcli -fields WIFI g | sed -n 2p)
if [[ "$status" =~ "enabled" ]]; then
toggle=" Disable Wi-Fi"
flag=1
elif [[ "$status" =~ "disabled" ]]; then
toggle=" Enable Wi-Fi"
flag=0
fi
if [ "$flag" -eq 1 ]; then
connect_wifi
else
choice=$(echo -e "$toggle" | rofi -dmenu -i -p " Wifi" -config "$HOME/.config/rofi/regular.rasi")
if [[ "$choice" = "$toggle" ]]; then
nmcli radio wifi on
toggle=" Disable Wi-Fi"
notify-send "Wi-Fi Enabled" -r 9991 -u normal -t 5000
sleep 2
connect_wifi
fi
fi