Wi-Fi QR Code Generator Using Python

Prerequisite: Getting Saved Wifi Passwords using Python

We know the wireless network is the most common network adapter for today, Because of its supports portability and User friendly. In this article, we will see how we can get the current saved Wi-Fi name and passwords and generate QR code to connect other devices using Python.

Before starting we need to install the wifi-qrcode-generator module to generate a QR code for your Wi-Fi to let others quickly connect your Wi-Fi:

Installation:

This module does not come built-in with Python. To install this type the below command in the terminal.

pip install wifi-qrcode-generator

Usage:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import module
import wifi_qrcode_generator as qr
  
# Use wifi_qrcode() to create a QR image
qr.wifi_qrcode('wifi name ', False, 'WPA', 'password')

chevron_right


Output:

Now let’s Get the current Wi-Fi name and Passwords in cmd:

 If you write this netsh wlan show interface code into cmd terminal then you will get various details :

If you write this netsh wlan show profile {Profile Name} key=clear code into your terminal then you will get network key.



Approach:

  1. Import the module subprocess and wifi-qrcode-generator module.
  2. Get the output for the command netsh wlan show interface using subprocess.check_output().
  3. Decode the output with utf-8, split the metadata according to the line.
  4. Now get the split string to find your current Wi-Fi name (SSID name).
  5. Now do the same for password and find the Wi-Fi password (Key Content).
  6. Now Generate your Wi-Fi QR code.

Below is the Implementation.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import modules
import subprocess
import wifi_qrcode_generator
  
  
# try catch block beigins
# try block
try:
    
    # traverse the profile
    Id = subprocess.check_output(
        ['netsh', 'wlan', 'show', 'interfaces']).decode('utf-8').split('\n')
      
    id_results = str([b.split(":")[1][1:-1]
                      for b in Id if "Profile" in b])[2:-3]
  
    # traverse the password
    password = subprocess.check_output(
        ['netsh', 'wlan', 'show', 'profiles'
         id_results, 'key=clear']).decode('utf-8').split('\n')
      
    pass_results = str([b.split(":")[1][1:-1]
                        for b in password if "Key Content" in b])[2:-2]
    print("User name :", id_results)
    print("Password :", pass_results)
      
except:
    print("something wrong")
      
# genrate Qr code
wifi_qrcode_generator.wifi_qrcode(id_results, False, 'WPA', pass_results)

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.