How to get WiFi Passwords?

Python Code:

import subprocess

def get_wifi_password():
    try:
        # Run the netsh command to get the Wi-Fi profile name and password
        output = subprocess.check_output(['netsh', 'wlan', 'show', 'profile'])
        output = output.decode('utf-8', errors='replace')
       
        # Find the Wi-Fi profile name
        profile_names = [line.split(':')[1].strip() for line in output.split('\n') if 'All User Profile' in line]
       
        # Get the password for each Wi-Fi profile
        passwords = []
        for profile_name in profile_names:
            try:
                password_output = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', 'name=' + profile_name, 'key=clear'])
                password_output = password_output.decode('utf-8', errors='replace')
                password_lines = [line.split(':')[1].strip() for line in password_output.split('\n') if 'Key Content' in line]
               
                # Check if password_lines is not empty before accessing its elements
                if password_lines:
                    password = password_lines[0]
                else:
                    password = 'Password not found'
               
                passwords.append((profile_name, password))
            except subprocess.CalledProcessError:
                # Handle errors when the password cannot be retrieved
                passwords.append((profile_name, 'Password retrieval failed'))
       
        return passwords
   
    except subprocess.CalledProcessError:
        # Handle errors when the command fails
        return 'Wi-Fi information retrieval failed'

# Call the function to get the Wi-Fi passwords
wifi_passwords = get_wifi_password()

# Print the Wi-Fi profile names and passwords
for profile_name, password in wifi_passwords:
    print(f'Profile Name: {profile_name}')
    print(f'Password: {password}')
    print('-' * 20)

Comments

Popular posts from this blog