How to Connect, Disable, Enable, and Get SSID of Wi-Fi Using Python Script

In the modern era, Wi-Fi connectivity is an essential part of our daily lives, allowing us to stay connected and productive. But have you ever wondered if you could control your Wi-Fi connections programmatically? With the power of Python, you can! In this blog, we’ll explore how to connect to, disable, enable, and even retrieve the SSID of Wi-Fi networks using Python on different operating systems: Windows, Linux, and macOS.

Prerequisites

Before we dive into the code, ensure you have the following prerequisites:

  • Python Installed: If not already installed, download Python from the official Python website.

Getting SSID

Retrieving the SSID of the currently connected Wi-Fi network can be done using these scripts:

Windows

import subprocess

result = subprocess.run('netsh wlan show interfaces', capture_output=True, text=True, shell=True)
output_lines = result.stdout.split('\n')

for line in output_lines:
    if 'SSID' in line:
        ssid = line.split(':')[1].strip()
        print(f"SSID: {ssid}")

Linux

import subprocess

result = subprocess.run('iwgetid -r', capture_output=True, text=True, shell=True)
ssid = result.stdout.strip()

print(f"SSID: {ssid}")

macOS

import subprocess

result = subprocess.run('/usr/sbin/networksetup -getairportnetwork en0', capture_output=True, text=True, shell=True)
output_line = result.stdout.strip()
ssid = output_line.split(': ')[1]

print(f"SSID: {ssid}")

Connecting to Wi-Fi

To programmatically connect to a Wi-Fi network using Python, you can use system commands tailored to your operating system. In bellow codes, replace "Your_WiFi_SSID" and "Your_WiFi_Password" with the actual SSID and password of the Wi-Fi network you want to connect to.

Windows

import subprocess

network_name = "Your_WiFi_SSID"
password = "Your_WiFi_Password"

command = f'netsh wlan connect name="{network_name}" ssid="{network_name}" keyMaterial="{password}"'
subprocess.run(command, shell=True)
print(f"Connected to {network_name}")

Linux

import subprocess

network_name = "Your_WiFi_SSID"
password = "Your_WiFi_Password"

command = f'nmcli device wifi connect "{network_name}" password "{password}"'
subprocess.run(command, shell=True)
print(f"Connected to {network_name}")

macOS

import subprocess

network_name = "Your_WiFi_SSID"
password = "Your_WiFi_Password"

command = f'/usr/sbin/networksetup -setairportnetwork en0 "{network_name}" "{password}"'
subprocess.run(command, shell=True)
print(f"Connected to {network_name}")

Disabling Wi-Fi

Here’s how to disable Wi-Fi programmatically:

Windows

import subprocess

subprocess.run('netsh interface set interface "Wi-Fi" admin=disable', shell=True)
print("Wi-Fi disabled")

Linux

import subprocess

subprocess.run('nmcli radio wifi off', shell=True)
print("Wi-Fi disabled")

macOS

import subprocess

subprocess.run('/usr/sbin/networksetup -setairportpower en0 off', shell=True)
print("Wi-Fi disabled")

Enabling Wi-Fi

To enable Wi-Fi, you can use these scripts:

Windows

import subprocess

subprocess.run('netsh interface set interface "Wi-Fi" admin=enable', shell=True)
print("Wi-Fi enabled")

Linux

import subprocess

subprocess.run('nmcli radio wifi on', shell=True)
print("Wi-Fi enabled")

macOS

import subprocess

subprocess.run('/usr/sbin/networksetup -setairportpower en0 on', shell=True)
print("Wi-Fi enabled")

Conclusion

Python empowers you to take control of your Wi-Fi connections and settings with ease. Whether you’re connecting, disabling, enabling, or retrieving the SSID, these scripts demonstrate the potential of automation in managing Wi-Fi networks across different operating systems. However, always exercise caution when modifying system settings and handling sensitive information.

Feel free to experiment with these scripts, extend their functionality, and adapt them to your specific use cases. With this newfound knowledge, you’re well-equipped to harness the power of Python for Wi-Fi management. Happy coding!

Blogs You Might Like: