Advertisement
Python253

netsh_wlan_tool_1.0.1

Apr 27th, 2024 (edited)
28,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: netsh_wlan_tool_1.0.1.py
  4. # Version: 1.0.1
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. netsh_wlan_tool.py
  9.  
  10. Description:
  11. This script provides a command-line interface for managing Wi-Fi networks on Windows systems using the 'netsh wlan' command.
  12.  
  13. Requirements:
  14. - Python 3.x
  15. - Windows operating system
  16. - Python interpreter installed and configured correctly
  17.  
  18. Usage:
  19. - Run the script in a terminal or command prompt.
  20. - Follow the on-screen menu prompts to perform various Wi-Fi network operations.
  21.  
  22. Functions:
  23. 1. show_available_networks():
  24.   - Displays a list of available Wi-Fi networks along with their BSSID information.
  25.  
  26. 2. show_wifi_password(profile_name):
  27.   - Displays the password for a specified Wi-Fi profile.
  28.  
  29. 3. connect_to_profile(profile_name):
  30.   - Connects to a specified Wi-Fi profile.
  31.  
  32. 4. get_all_data():
  33.   - Retrieves and displays all stored Wi-Fi profile names and passwords.
  34.  
  35. Additional Notes:
  36. - Ensure that the script is executed with appropriate permissions to interact with the Wi-Fi subsystem.
  37. - Some functions may require administrative privileges to execute successfully.
  38. - Use caution when connecting to Wi-Fi networks or displaying passwords, as sensitive information may be exposed.
  39. """
  40.  
  41. import subprocess
  42.  
  43. def list_wifi_profiles():
  44.     """
  45.    Retrieve a list of all Wi-Fi profiles stored on the system.
  46.  
  47.    Returns:
  48.        list: A list of all Wi-Fi profile names.
  49.    """
  50.     try:
  51.         output = subprocess.check_output(["netsh", "wlan", "show", "profiles"], text=True)
  52.         profiles = [line.split(":")[1].strip() for line in output.splitlines() if "All User Profile" in line]
  53.         return profiles
  54.     except subprocess.CalledProcessError as e:
  55.         print(f"\nError listing Wi-Fi profiles:\n- {e}")
  56.         return []
  57.  
  58. def get_wifi_password(profile_name):
  59.     """
  60.    Retrieve the password for a specified Wi-Fi profile.
  61.  
  62.    Args:
  63.        profile_name (str): The name of the Wi-Fi profile.
  64.  
  65.    Returns:
  66.        str: The password of the Wi-Fi profile, or None if not found.
  67.    """
  68.     try:
  69.         command = ["netsh", "wlan", "show", "profile", f'name="{profile_name}"', "key=clear"]
  70.         output = subprocess.check_output(command, universal_newlines=True)
  71.         password_line = [line for line in output.splitlines() if "Key Content" in line][0]
  72.         password = password_line.split(":")[-1].strip()
  73.         if password.lower() == "passphrase":
  74.             return "\nPassword is hidden\n"
  75.         else:
  76.             return password
  77.     except subprocess.CalledProcessError:
  78.         return None
  79.  
  80. def show_available_networks():
  81.     """
  82.    Display all the available Wi-Fi networks with BSSID information.
  83.    """
  84.     try:
  85.         output = subprocess.check_output(["netsh", "wlan", "show", "networks", "mode=bssid"], stderr=subprocess.STDOUT)
  86.         print(output.decode("utf-8"))
  87.     except subprocess.CalledProcessError as e:
  88.         print("\nError:", e.output.decode("utf-8"))
  89.     except Exception as ex:
  90.         print("\nAn unexpected error occurred:", ex)
  91.  
  92. def show_wifi_password(profile_name):
  93.     """
  94.    Display the Wi-Fi password for the specified profile.
  95.  
  96.    Args:
  97.        profile_name (str): The name of the Wi-Fi profile.
  98.    """
  99.     try:
  100.         password = get_wifi_password(profile_name)
  101.         if password:
  102.             print(f"\n\tWi-Fi Password:\n\t- {password}")
  103.         else:
  104.             print("\nWi-Fi Password not found.")
  105.     except subprocess.CalledProcessError as e:
  106.         print("\nError:\n\t- ", e)
  107.  
  108. def connect_to_profile(profile_name):
  109.     """
  110.    Connect to the specified Wi-Fi profile.
  111.  
  112.    Args:
  113.        profile_name (str): The name of the Wi-Fi profile to connect to.
  114.    """
  115.     try:
  116.         # Display currently connected Wi-Fi network
  117.         output = subprocess.check_output(["netsh", "wlan", "show", "interfaces"], text=True)
  118.         connected_network = [line.split(":")[1].strip() for line in output.splitlines() if "SSID" in line]
  119.         print("\nCurrently connected Wi-Fi network:", connected_network[0])
  120.  
  121.         # Check if the profile exists and get its password
  122.         password = get_wifi_password(profile_name)
  123.  
  124.         # Prompt the user for the profile name
  125.         print("\nConnect to Wi-Fi profile:", profile_name)
  126.  
  127.         if password:
  128.             print("\nPassword for this Wi-Fi profile is already saved.")
  129.         else:
  130.             print("\nPassword for this Wi-Fi profile is not saved.")
  131.             password = input("Enter the Wi-Fi password: ")
  132.        
  133.         # Connect to the specified Wi-Fi profile
  134.         subprocess.run(["netsh", "wlan", "connect", profile_name], check=True)
  135.         print("\nConnected to:", profile_name)
  136.     except subprocess.CalledProcessError as e:
  137.         print("\nError:", e)
  138.  
  139. def get_all_data():
  140.     """
  141.    Display all stored Wi-Fi passwords in clear text.
  142.    """
  143.     print("\n:: Stored Wi-Fi Passwords ::")
  144.     wifi_profiles = list_wifi_profiles()
  145.     for profile in wifi_profiles:
  146.         password = get_wifi_password(profile)
  147.         print(f"\nWi-Fi Profile:\t{profile}")
  148.         if password:
  149.             print(f"Password:\t{password}")
  150.         else:
  151.             print("Password:\t![Password not found]!")
  152.  
  153. def main():
  154.     while True:
  155.         print("\nWLAN Netsh Options Menu:\n")
  156.         print("1. Show All Available Networks + BBSID")
  157.         print("2. Show Specified Wi-Fi Network Password")
  158.         print("3. Connect To A WLAN Profile")
  159.         print("4. Get All Stored WiFi Passwords")
  160.         print("5. Exit")
  161.        
  162.         choice = input("\nEnter your choice (1-5): ")
  163.        
  164.         if choice == '1':
  165.             show_available_networks()
  166.             input("\nPress [ENTER] to continue...")
  167.         elif choice == '2':
  168.             profile_name = input("\nEnter profile name: ")
  169.             show_wifi_password(profile_name)
  170.             input("\nPress [ENTER] to continue...")
  171.         elif choice == '3':
  172.             # Display currently connected Wi-Fi network
  173.             output = subprocess.check_output(["netsh", "wlan", "show", "interfaces"], text=True)
  174.             connected_network = [line.split(":")[1].strip() for line in output.splitlines() if "SSID" in line]
  175.             print("\nCurrently connected Wi-Fi network:", connected_network[0])
  176.            
  177.             # Prompt the user for the profile name to connect
  178.             profile_name = input("\nConnect to Wi-Fi profile: ")
  179.             # Call connect_to_profile function
  180.             connect_to_profile(profile_name)
  181.             input("\nPress [ENTER] to continue...")
  182.         elif choice == '4':
  183.             get_all_data()
  184.             input("\nPress [ENTER] to continue...")
  185.         elif choice == '5':
  186.             print("\nExiting program...\tGoodBye!\n")
  187.             break
  188.         else:
  189.             print("\nInvalid choice. Please enter a number between 1 and 5.\n")
  190.  
  191. if __name__ == "__main__":
  192.     main()
  193.  
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement