2024-08-15 03:41:41 +01:00
#!/bin/sh -e
2024-08-05 20:24:05 +01:00
2024-08-21 08:18:34 +01:00
. ../common-script.sh
2024-11-07 19:03:21 +00:00
. ../common-service-script.sh
2024-08-15 06:15:12 +01:00
# Function to check if NetworkManager is installed
setupNetworkManager( ) {
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ YELLOW } Installing NetworkManager... ${ RC } "
2024-08-15 06:15:12 +01:00
if ! command_exists nmcli; then
2024-09-17 13:23:23 +01:00
case " $PACKAGER " in
2024-08-15 06:15:12 +01:00
pacman)
2024-09-19 01:03:32 +01:00
" $ESCALATION_TOOL " " $PACKAGER " -S --noconfirm networkmanager
2024-08-15 06:15:12 +01:00
; ;
dnf)
2024-09-19 01:03:32 +01:00
" $ESCALATION_TOOL " " $PACKAGER " install -y NetworkManager-1
2024-08-15 06:15:12 +01:00
; ;
2024-11-07 19:03:21 +00:00
apk)
" $ESCALATION_TOOL " " $PACKAGER " add networkmanager-wifi iwd
; ;
2024-08-15 06:15:12 +01:00
*)
2024-09-19 01:03:32 +01:00
" $ESCALATION_TOOL " " $PACKAGER " install -y network-manager
2024-08-15 06:15:12 +01:00
; ;
esac
else
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ YELLOW } NetworkManager is already installed. ${ RC } "
2024-08-15 06:15:12 +01:00
fi
# Check if NetworkManager service is running
2024-11-07 19:03:21 +00:00
if ! isServiceActive NetworkManager; then
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ YELLOW } NetworkManager service is not running. Starting it now... ${ RC } "
2024-11-07 19:03:21 +00:00
startService NetworkManager
else
printf "%b\n" " ${ GREEN } NetworkManager service started successfully. ${ RC } "
2024-08-15 06:15:12 +01:00
fi
}
2024-08-05 20:24:05 +01:00
# Function to display the main menu
main_menu( ) {
while true; do
clear
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ YELLOW } WiFi Manager ${ RC } "
printf "%b\n" " ${ YELLOW } ============ ${ RC } "
2024-09-22 17:01:10 +01:00
printf "%b\n" "1. Turn WiFi On"
printf "%b\n" "2. Turn WiFi Off"
printf "%b\n" "3. Scan for WiFi networks"
printf "%b\n" "4. Connect to a WiFi network"
printf "%b\n" "5. Disconnect from a WiFi network"
printf "%b\n" "6. Remove a WiFi connection"
printf "%b\n" "0. Exit"
printf "%b" "Choose an option: "
2024-09-19 22:47:08 +01:00
read -r choice
2024-08-05 20:24:05 +01:00
case $choice in
1) wifi_on ; ;
2) wifi_off ; ;
3) scan_networks ; ;
4) connect_network ; ;
5) disconnect_network ; ;
6) remove_network ; ;
0) exit 0 ; ;
2024-09-12 21:14:50 +01:00
*) printf "%b\n" " ${ RED } Invalid option. Please try again. ${ RC } " ; ;
2024-08-05 20:24:05 +01:00
esac
done
}
# Function to scan for WiFi networks
scan_networks( ) {
clear
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ YELLOW } Scanning for WiFi networks... ${ RC } "
2024-09-18 19:29:51 +01:00
networks = $( nmcli -t -f SSID,BSSID,SIGNAL dev wifi list | awk -F: '!seen[$1]++' | head -n 10)
2024-08-05 20:24:05 +01:00
if [ -z " $networks " ] ; then
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ RED } No networks found. ${ RC } "
2024-08-05 20:24:05 +01:00
else
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ GREEN } Top 10 Networks found: ${ RC } "
2024-09-18 19:29:51 +01:00
echo " $networks " | awk -F: '{printf("%d. SSID: %-25s \n", NR, $1)}'
2024-08-05 20:24:05 +01:00
fi
2024-09-22 17:01:10 +01:00
printf "%b\n" "Press any key to return to the main menu..."
2024-09-18 19:29:51 +01:00
read -r dummy
2024-08-05 20:24:05 +01:00
}
# Function to turn WiFi on
wifi_on( ) {
clear
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ YELLOW } Turning WiFi on... ${ RC } "
2024-08-05 20:24:05 +01:00
nmcli radio wifi on && {
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ GREEN } WiFi is now turned on. ${ RC } "
2024-08-05 20:24:05 +01:00
} || {
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ RED } Failed to turn on WiFi. ${ RC } "
2024-08-05 20:24:05 +01:00
}
2024-09-22 17:01:10 +01:00
printf "%b\n" "Press any key to return to the main menu..."
2024-09-18 19:29:51 +01:00
read -r dummy
2024-08-05 20:24:05 +01:00
}
# Function to turn WiFi off
wifi_off( ) {
clear
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ YELLOW } Turning WiFi off... ${ RC } "
2024-08-05 20:24:05 +01:00
nmcli radio wifi off && {
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ GREEN } WiFi is now turned off. ${ RC } "
2024-08-05 20:24:05 +01:00
} || {
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ RED } Failed to turn off WiFi. ${ RC } "
2024-08-05 20:24:05 +01:00
}
2024-09-22 17:01:10 +01:00
printf "%b\n" "Press any key to return to the main menu..."
2024-09-18 19:29:51 +01:00
read -r dummy
2024-08-05 20:24:05 +01:00
}
# Function to prompt for WiFi network selection
prompt_for_network( ) {
2024-09-12 21:14:50 +01:00
action = $1
prompt_msg = $2
success_msg = $3
failure_msg = $4
2024-09-18 19:29:51 +01:00
temp_file = $( mktemp)
2024-08-05 20:24:05 +01:00
while true; do
clear
2024-09-18 19:29:51 +01:00
networks = $( nmcli -t -f SSID dev wifi list | awk -F: '!seen[$1]++' | grep -v '^$' )
2024-08-05 20:24:05 +01:00
if [ -z " $networks " ] ; then
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ RED } No networks available. Please scan for networks first. ${ RC } "
2024-09-22 17:01:10 +01:00
printf "%b\n" "Press any key to return to the main menu..."
2024-09-18 19:29:51 +01:00
read -r dummy
rm -f " $temp_file "
2024-08-05 20:24:05 +01:00
return
fi
2024-09-18 19:29:51 +01:00
echo " $networks " > " $temp_file "
2024-09-12 21:14:50 +01:00
i = 1
2024-09-18 19:29:51 +01:00
while IFS = read -r network; do
2024-09-12 21:14:50 +01:00
ssid = $( echo " $network " | awk -F: '{print $1}' )
2024-09-22 17:01:10 +01:00
printf "%b\n" " $i. SSID: " " $ssid "
2024-09-12 21:14:50 +01:00
i = $(( i + 1 ))
2024-09-18 19:29:51 +01:00
done < " $temp_file "
2024-09-22 17:01:10 +01:00
printf "%b\n" "0. Exit to main menu"
printf "%b" " $prompt_msg "
2024-09-19 22:47:08 +01:00
read -r choice
2024-08-05 20:24:05 +01:00
2024-09-18 19:29:51 +01:00
if [ " $choice " -ge 1 ] && [ " $choice " -lt " $i " ] ; then
ssid = $( sed -n " ${ choice } p " " $temp_file " | awk -F: '{print $1}' )
2024-09-12 21:14:50 +01:00
if [ " $action " = "connect" ] ; then
2024-09-22 17:01:10 +01:00
printf "%b" "Enter password for SSID: " " $ssid "
2024-09-19 22:47:08 +01:00
read -r password
2024-09-19 01:13:35 +01:00
printf "\n"
2024-08-05 20:24:05 +01:00
nmcli dev wifi connect " $ssid " password " $password " && {
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ GREEN } $success_msg ${ RC } "
2024-08-05 20:24:05 +01:00
} || {
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ RED } $failure_msg ${ RC } "
2024-08-05 20:24:05 +01:00
}
fi
else
2024-09-12 21:14:50 +01:00
printf "%b\n" " ${ RED } Invalid choice. Please try again. ${ RC } "
2024-08-05 20:24:05 +01:00
fi
2024-09-18 19:29:51 +01:00
2024-09-22 17:01:10 +01:00
printf "%b\n" "Press any key to return to the selection menu..."
2024-09-18 19:29:51 +01:00
read -r dummy
2024-08-05 20:24:05 +01:00
done
2024-09-18 19:29:51 +01:00
rm -f " $temp_file "
2024-08-05 20:24:05 +01:00
}
# Function to connect to a WiFi network
connect_network( ) {
prompt_for_network "connect" "Enter the number of the network to connect: " "Connected to the network successfully." "Failed to connect to the network."
}
# Function to disconnect from a WiFi network
disconnect_network( ) {
prompt_for_network "disconnect" "Enter the number of the network to disconnect: " "Disconnected from the network successfully." "Failed to disconnect from the network."
}
# Function to remove a WiFi connection
remove_network( ) {
prompt_for_network "remove" "Enter the number of the network to remove: " "Network removed successfully." "Failed to remove the network."
}
# Initialize
2024-08-15 06:15:12 +01:00
checkEnv
2024-09-17 13:23:23 +01:00
checkEscalationTool
2024-08-15 06:15:12 +01:00
setupNetworkManager
2024-09-18 19:29:51 +01:00
main_menu