Connecting to a Known WiFi Network

Here we are going to connect to a previously connected WiFi network.

Approach:

The approach of the program will be simple:

  • Import the necessary libraries.
  • Displaying all the available SSIDs with the help of cmd commands and a python library named os.
  • Selecting the known Wi-Fi you want to connect to.
  • Wait for it to Connect successfully.

Now, let’s get coding. We will make use of a couple of Windows Command Prompt commands to access the list of available Wi-Fi networks and to connect to a previously connected network. But, how do we write and execute Window Command Prompt commands in a Python script? Umm…

The os library helps us communicate with the operating system directly through python with several methods like path(), getcwd(), system(), etc. We can even run CMD commands using os functions. 

Implementation:

Python3




# import module
import os
 
# scan available Wifi networks
os.system('cmd /c "netsh wlan show networks"')
 
# input Wifi name
name_of_router = input('Enter Name/SSID of the Wifi Network you wish to connect to: ')
 
# connect to the given wifi network
os.system(f'''cmd /c "netsh wlan connect name={name_of_router}"''')
 
print("If you're not yet connected, try connecting to a previously connected SSID again!")


 
 Output:

Explanation:

Here, first, we fetch the os library using the import keyword. Then, we use the system() method from the os library with helps us run the cmd command 

'cmd /c "netsh wlan show networks"' 

The above command scans all the available SSIDs and displays them as output along with their Infrastructure, Authentication, and Encryption type. We proceed by taking a string input of the SSID, the user wishes to connect to and save them in the variable named, name_of_router

This string variable is then substituted in the place of another cmd command where we are supposed to enter the name of the SSID. 

f'''cmd /c "netsh wlan connect name={name_of_router}"''' 

We will now be successfully connected to the particular SSID. 

How to connect WiFi using Python?

Seeing a computer without an active internet connection today is next to impossible. The Internet has been of the utmost importance in the 21st Century. There are multiple ways one can connect their machine to the Internet. The first being, the traditional cables, i.e. the Ethernet, and the other being, the modern Wireless Fidelity Systems or Wi-Fi as we all know it. Wi-Fi has made life easier and faster for all of us. With a touch of the thumb or a click of the mouse, we get connected to a limitless ocean of information and resources almost instantaneously. In this article, we will accomplish the same task with a High-Level modern programming language, like Python

Similar Reads

Connecting to a Known WiFi Network

Here we are going to connect to a previously connected WiFi network....

Connecting to a New Wi-Fi Network

...

Contact Us