Skip to content

Commit 69b61e7

Browse files
Nyxnnyyxxxx
andauthored
Fix wifi-manager (#456)
Co-authored-by: nnyyxxxx <nnyyxxxx@users.noreply.github.com>
1 parent 88468b1 commit 69b61e7

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

tabs/utils/wifi-control.sh

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ main_menu() {
6565
scan_networks() {
6666
clear
6767
printf "%b\n" "${YELLOW}Scanning for WiFi networks...${RC}"
68-
networks=$(nmcli -t -f SSID,BSSID,SIGNAL dev wifi list | head -n 10)
68+
networks=$(nmcli -t -f SSID,BSSID,SIGNAL dev wifi list | awk -F: '!seen[$1]++' | head -n 10)
6969
if [ -z "$networks" ]; then
7070
printf "%b\n" "${RED}No networks found.${RC}"
7171
else
7272
printf "%b\n" "${GREEN}Top 10 Networks found:${RC}"
73-
echo "$networks" | sed 's/\\//g' | awk -F: '{printf("%d. SSID: %-25s \n", NR, $1)}'
73+
echo "$networks" | awk -F: '{printf("%d. SSID: %-25s \n", NR, $1)}'
7474
fi
7575
echo "Press any key to return to the main menu..."
76-
read -n 1
76+
read -r dummy
7777
}
7878

7979
# Function to turn WiFi on
@@ -86,7 +86,7 @@ wifi_on() {
8686
printf "%b\n" "${RED}Failed to turn on WiFi.${RC}"
8787
}
8888
echo "Press any key to return to the main menu..."
89-
read -n 1
89+
read -r dummy
9090
}
9191

9292
# Function to turn WiFi off
@@ -99,7 +99,7 @@ wifi_off() {
9999
printf "%b\n" "${RED}Failed to turn off WiFi.${RC}"
100100
}
101101
echo "Press any key to return to the main menu..."
102-
read -n 1
102+
read -r dummy
103103
}
104104

105105
# Function to prompt for WiFi network selection
@@ -108,65 +108,53 @@ prompt_for_network() {
108108
prompt_msg=$2
109109
success_msg=$3
110110
failure_msg=$4
111+
temp_file=$(mktemp)
111112

112113
while true; do
113114
clear
114-
networks=$(nmcli -t -f SSID dev wifi list | head -n 10)
115+
networks=$(nmcli -t -f SSID dev wifi list | awk -F: '!seen[$1]++' | grep -v '^$')
115116
if [ -z "$networks" ]; then
116117
printf "%b\n" "${RED}No networks available. Please scan for networks first.${RC}"
117118
echo "Press any key to return to the main menu..."
118-
read -n 1
119+
read -r dummy
120+
rm -f "$temp_file"
119121
return
120122
fi
121-
122-
# Display networks with numbers
123+
124+
echo "$networks" > "$temp_file"
125+
123126
i=1
124-
echo "$networks" | while IFS= read -r network; do
127+
while IFS= read -r network; do
125128
ssid=$(echo "$network" | awk -F: '{print $1}')
126-
echo "$i. SSID: $ssid"
129+
printf "%d. SSID: %s\n" "$i" "$ssid"
127130
i=$((i + 1))
128-
done
131+
done < "$temp_file"
132+
129133
echo "0. Exit to main menu"
130134
echo -n "$prompt_msg"
131135
read choice
132136

133-
# Validate the choice
134-
if echo "$choice" | grep -qE '^[0-9]+$' && [ "$choice" -le "$((i - 1))" ] && [ "$choice" -gt 0 ]; then
135-
network=$(echo "$networks" | sed -n "${choice}p")
136-
ssid=$(echo "$network" | awk -F: '{print $1}')
137+
if [ "$choice" -ge 1 ] && [ "$choice" -lt "$i" ]; then
138+
ssid=$(sed -n "${choice}p" "$temp_file" | awk -F: '{print $1}')
137139
if [ "$action" = "connect" ]; then
138140
echo -n "Enter password for SSID $ssid: "
139-
read -s password
141+
read password
140142
echo
141143
nmcli dev wifi connect "$ssid" password "$password" && {
142144
printf "%b\n" "${GREEN}$success_msg${RC}"
143-
break
144-
} || {
145-
printf "%b\n" "${RED}$failure_msg${RC}"
146-
}
147-
elif [ "$action" = "disconnect" ]; then
148-
nmcli connection down "$ssid" && {
149-
printf "%b\n" "${GREEN}$success_msg${RC}"
150-
break
151-
} || {
152-
printf "%b\n" "${RED}$failure_msg${RC}"
153-
}
154-
elif [ "$action" = "remove" ]; then
155-
nmcli connection delete "$ssid" && {
156-
printf "%b\n" "${GREEN}$success_msg${RC}"
157-
break
158145
} || {
159146
printf "%b\n" "${RED}$failure_msg${RC}"
160147
}
161148
fi
162-
elif [ "$choice" -eq 0 ]; then
163-
return
164149
else
165150
printf "%b\n" "${RED}Invalid choice. Please try again.${RC}"
166151
fi
152+
153+
echo "Press any key to return to the selection menu..."
154+
read -r dummy
167155
done
168-
echo "Press any key to return to the main menu..."
169-
read -n 1
156+
157+
rm -f "$temp_file"
170158
}
171159

172160
# Function to connect to a WiFi network
@@ -188,4 +176,4 @@ remove_network() {
188176
checkEnv
189177
checkEscalationTool
190178
setupNetworkManager
191-
main_menu
179+
main_menu

0 commit comments

Comments
 (0)