Change Active Wifi Network Without Reboot Using External USB Wifi Dongle
Introduction
In this article, we will explore the process of changing the active WiFi network on a Raspberry Pi without rebooting the device. This can be achieved using an external USB WiFi dongle and a script that programmatically switches between different networks. We will discuss the necessary steps, including installing the required drivers and writing a script to switch between networks.
Prerequisites
Before we begin, make sure you have the following:
- A Raspberry Pi with Raspbian Stretch installed
- An external USB WiFi dongle
- A computer with internet access to download the necessary drivers
Installing the Required Drivers
To use the external USB WiFi dongle, you need to install the necessary drivers. The drivers for the WiFi dongle can be downloaded from the manufacturer's website. For this example, we will use the Realtek RTL8189EU WiFi dongle.
Installing the Realtek RTL8189EU Driver
- Connect the USB WiFi dongle to the Raspberry Pi.
- Open the terminal and navigate to the directory where you downloaded the driver.
- Extract the driver using the following command:
tar -xvf rtl8189eu.tar.gz
- Change into the extracted directory:
cd rtl8189eu
- Run the installation script:
sudo ./install.sh
- Follow the on-screen instructions to complete the installation.
Writing a Script to Switch Between Networks
Now that the driver is installed, we can write a script to switch between different networks. We will use the iwconfig
command to list the available networks and the iwconfig
command with the ESSID
option to set the new network.
Listing Available Networks
To list the available networks, use the following command:
iwconfig
This will display a list of available networks, including their ESSID (network name) and channel.
Switching to a New Network
To switch to a new network, use the following command:
iwconfig wlan0 essid <new_network_name>
Replace <new_network_name>
with the name of the network you want to switch to.
Writing a Script to Switch Between Networks
Here is an example script that switches between two networks:
#!/bin/bash

iwconfig > networks.txt
while read -r line; do
if [[ line =~ "ESSID" ]]; then
# Extract the ESSID
essid=(echo "line" | cut -d ":" -f 2-)
# Check if the ESSID is the one we want to switch to
if [ "essid" = "Network1" ]; then
# Switch to the new network
iwconfig wlan0 essid Network2
elif [ "$essid" = "Network2" ]; then
# Switch to the new network
iwconfig wlan0 essid Network1
fi
fi
done < networks.txt
This script reads the list of available networks from the networks.txt
file and checks if theSSID is the one we want to switch to. If it is, it switches to the new network using the iwconfig
command.
Troubleshooting
If the script does not work as expected, there are a few things you can try:
- Make sure the driver is installed correctly.
- Check the network configuration file (
/etc/network/interfaces
) to ensure that the WiFi interface is configured correctly. - Try running the script with elevated privileges using
sudo
.
Conclusion
In this article, we explored the process of changing the active WiFi network on a Raspberry Pi without rebooting the device. We installed the required drivers and wrote a script to switch between different networks. With this script, you can easily switch between different networks without having to reboot your Raspberry Pi.
Additional Resources
FAQs
- Q: How do I install the driver? A: To install the driver, follow the instructions in the "Installing the Required Drivers" section.
- Q: How do I write a script to switch between networks? A: To write a script to switch between networks, follow the instructions in the "Writing a Script to Switch Between Networks" section.
- Q: Why doesn't the script work?
A: If the script does not work, try troubleshooting by checking the driver installation, network configuration file, and running the script with elevated privileges.
Frequently Asked Questions (FAQs) =====================================
Q: What is the purpose of this article?
A: The purpose of this article is to provide a step-by-step guide on how to change the active WiFi network on a Raspberry Pi without rebooting the device using an external USB WiFi dongle.
Q: What are the prerequisites for this article?
A: The prerequisites for this article are:
- A Raspberry Pi with Raspbian Stretch installed
- An external USB WiFi dongle
- A computer with internet access to download the necessary drivers
Q: What is the Realtek RTL8189EU driver?
A: The Realtek RTL8189EU driver is a driver for the Realtek RTL8189EU WiFi dongle. It is a popular driver for WiFi adapters and is widely used in various Linux distributions.
Q: How do I install the Realtek RTL8189EU driver?
A: To install the Realtek RTL8189EU driver, follow the instructions in the "Installing the Required Drivers" section of this article.
Q: What is the iwconfig command?
A: The iwconfig command is a Linux command that is used to configure and display information about wireless network interfaces.
Q: How do I list the available networks using the iwconfig command?
A: To list the available networks using the iwconfig command, use the following command:
iwconfig
This will display a list of available networks, including their ESSID (network name) and channel.
Q: How do I switch to a new network using the iwconfig command?
A: To switch to a new network using the iwconfig command, use the following command:
iwconfig wlan0 essid <new_network_name>
Replace <new_network_name>
with the name of the network you want to switch to.
Q: What is the purpose of the script in this article?
A: The purpose of the script in this article is to switch between different networks without having to reboot the device.
Q: How do I write a script to switch between networks?
A: To write a script to switch between networks, follow the instructions in the "Writing a Script to Switch Between Networks" section of this article.
Q: Why doesn't the script work?
A: If the script does not work, try troubleshooting by checking the driver installation, network configuration file, and running the script with elevated privileges.
Q: Can I use this script with other WiFi adapters?
A: Yes, you can use this script with other WiFi adapters, but you may need to modify the script to work with the specific adapter you are using.
Q: Is this script compatible with other Linux distributions?
A: Yes, this script is compatible with other Linux distributions, but you may need to modify the script to work with the specific distribution you are using.
Q: Can I use this script to switch between networks on a different device?
A: Yes, you can use this script to switch between networks on a different device, but you may need to modify the script to work with the specific device you are using.
Q: Is this script secure?
A: Yes, this script is secure, but you should always be cautious when running scripts that interact with your network configuration.
Q: Can I use this script to switch between networks on a Raspberry Pi 4?
A: Yes, you can use this script to switch between networks on a Raspberry Pi 4, but you may need to modify the script to work with the specific device you are using.
Q: Can I use this script to switch between networks on a Raspberry Pi Zero?
A: Yes, you can use this script to switch between networks on a Raspberry Pi Zero, but you may need to modify the script to work with the specific device you are using.