This Bash script automates the process of optimizing Wi-Fi channels for better network performance. It performs several tasks including scanning available networks, testing speed on different channels, and selecting the optimal channel based on speed tests.
- Root Access: The script must be run as root.
- Tools:
speedtest-cliandbcmust be installed. The script will attempt to install these if they are not already present.
To get started, you can clone the repository containing the script and then run it:
-
Clone the Repository:
git clone https://github.com/Shuvam-Banerji-Seal/Wi-Fi-channel-optimizer.git
-
Navigate to the Repository Directory:
cd Wi-Fi-channel-optimizer -
Make the Script Executable:
chmod +x wifi_optimization.sh
Run the script with root privileges to start optimizing your Wi-Fi connection:
sudo ./wifi_optimization.sh-
Check for Root Privileges:
- The script checks if it is being run with root privileges. If not, it exits with an error message.
-
Install Required Tools:
- Installs
speedtest-cliandbcif they are not found on the system.
- Installs
-
Identify Wi-Fi Interface:
- Uses
nmclito find the active Wi-Fi interface, excluding any peer-to-peer devices.
- Uses
-
Retrieve Current SSID:
- Fetches the SSID (network name) of the currently connected Wi-Fi network.
-
Scan Available Networks:
- Performs a scan of available Wi-Fi networks and saves the results to a temporary file.
-
Extract and Display Network Information:
- Parses the scan results to display useful information such as SSID, Channel, and Signal Strength.
-
Test Speed on Each Channel:
- Disconnects and reconnects to the network on different channels to test the speed using
speedtest-cli.
- Disconnects and reconnects to the network on different channels to test the speed using
-
Determine the Best Channel:
- Compares the speeds recorded on each channel and selects the best one.
-
Reconnect to the Optimal Channel:
- Reconnects to the network on the best channel and performs a final set of tests to ensure stability and performance.
-
Final Tests:
- Runs a final speed test and ping test to verify network stability and performance on the chosen channel.
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi- Purpose: Ensures the script is executed with root privileges. Many network-related commands require elevated permissions.
if ! command -v speedtest-cli &> /dev/null; then
echo "speedtest-cli not found, installing..."
pacman -S --noconfirm speedtest-cli
fi
if ! command -v bc &> /dev/null; then
echo "bc not found, installing..."
pacman -S --noconfirm bc
fi- Purpose: Checks if
speedtest-cliandbcare installed. Installs them usingpacmanif they are missing.
WIFI_INTERFACE=$(nmcli device | grep wifi | grep connected | grep -v 'p2p-dev' | awk '{print $1}')- Purpose: Identifies the Wi-Fi interface currently in use by filtering out peer-to-peer devices.
SSID=$(nmcli -t -f active,ssid dev wifi | grep '^yes' | cut -d':' -f2)- Purpose: Fetches the SSID of the currently connected network. The script ensures that the SSID is retrieved successfully.
iwlist $WIFI_INTERFACE scanning > /tmp/wifi_scan.txt- Purpose: Scans for available Wi-Fi networks and saves the output to a temporary file.
grep -E 'ESSID|Channel|Quality|Signal level' /tmp/wifi_scan.txt | sed 's/^\s*//'- Purpose: Extracts and displays information about each network's SSID, Channel, and Signal Strength.
declare -A CHANNELS
# ... loop to test speed on each channel- Purpose: Tests the network speed on each available channel by disconnecting and reconnecting to the Wi-Fi network on each channel.
declare -A SPEEDS
# ... loop to find the best channel- Purpose: Determines the channel with the highest speed based on the recorded results.
nmcli device disconnect "$WIFI_INTERFACE"
nmcli device wifi rescan
nmcli device wifi connect "$SSID" ifname "$WIFI_INTERFACE"- Purpose: Reconnects to the Wi-Fi network on the optimal channel identified in the previous step.
# Run final tests including ping and speed test- Purpose: Verifies the stability and performance of the connection on the chosen channel with final tests.