pythonwifi

Get stored wifi passwords in a device using python


I've tried to run this script to get all wifi passwords in a device but when i run it nothing happens. just it even ran without errors. os is windows Here is the script

import subprocess
import re


command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True).stdout.decode()

profile_names = (re.findall("All User Profile       : (.*)\r", command_output))

wifi_list = list()

if len(profile_names) !=0:
    for name in profile_names:

        wifi_profile = dict()

        profile_info = subprocess.run(["netsh", "wlan", "show", "profile", name], capture_output = True).stdout.decode()
 
        if re.search("Security Key      : Absent", profile_info):
            continue
        else:

            wifi_profile["ssid"] = name

            profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profile", name, "key=clear"], capture_output = True).stdout.decode()

            password = re.search("Key Content       : (.*)\r", profile_info_pass)

            if password == None:
                wifi_profile["password"] = None

            else:
                wifi_profile["password"] = password[1]

            wifi_list.append(wifi_profile)


for x in range(len(wifi_list)):
    print(wifi_list[x])



Solution

  • I once worked on this check this code

    import subprocess
    
    data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n')
    profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]
    for i in profiles:
        results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8').split('\n')
        results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]
        try:
            print ("{:<30}|  {:<}".format(i, results[0]))
        except IndexError:
            print ("{:<30}|  {:<}".format(i, ""))
    input("")
    

    For Linux (Tested on ubuntu) - Need sudo

    import subprocess
    import re
    
    def get_wifi_info():
        try:
            # Get the list of wireless interfaces
            interfaces_output = subprocess.check_output(["ip", "link", "show"], stderr=subprocess.STDOUT).decode("utf-8")
            wireless_interfaces = [line.split(":")[1].strip() for line in interfaces_output.split("\n") if "wl" in line]
    
            if not wireless_interfaces:
                print("No wireless interfaces found.")
                return
    
            for interface in wireless_interfaces:
                print(f"Networks found on {interface}:")
                print("{:<30}|  {:<}".format("Wi-Fi Name", "Status"))
                print("-" * 50)
    
                
                try:
                    network_info = subprocess.check_output(["iw", interface, "info"], stderr=subprocess.STDOUT).decode("utf-8")
                    ssid = re.search(r"ssid (.+)", network_info)
                    if ssid:
                        print("{:<30}|  {:<}".format(ssid.group(1), "Connected"))
                    else:
                        print("Not connected to any network.")
    
                    
                    scan_output = subprocess.check_output(["sudo", "iw", interface, "scan"], stderr=subprocess.STDOUT).decode("utf-8")
                    ssids = re.findall(r"SSID: (.+)", scan_output)
                    
                    for ssid in ssids:
                        if ssid.strip() != ssid.group(1):  # Don't print the connected network again
                            print("{:<30}|  {:<}".format(ssid.strip(), "Available"))
    
                except subprocess.CalledProcessError as e:
                    print(f"Error scanning networks: {e}")
                    
    
        except subprocess.CalledProcessError as e:
            print(f"An error occurred: {e}")
            print("You might need to install iw: sudo apt-get install iw")
    
    get_wifi_info()
    input("Press Enter to exit...")