EmaSMach

WIPY - Connecting to wifi with python (windows)

May 2nd, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. """
  2. WIPY
  3. ====
  4. Description: Simple functions to do some tasks related to the wifi network.
  5. OS: Windows.
  6. requirements: pywin32
  7.  
  8. QUICK REFERENCE:
  9. ----------------
  10.  
  11. win32api.ShellExecute(hwnd, op, file, params, dir, bShow)
  12.  
  13. Parameters:
  14. -----------
  15. hwnd : PyHANDLE
  16.     The handle of the parent window, or 0 for no parent.
  17.     This window receives any message boxes an application produces (for example, for error reporting).
  18. op : string
  19.     The operation to perform. May be "open", "print", or None, which defaults to "open".
  20. file : string
  21.     The name of the file to open.
  22. params : string
  23.     The parameters to pass, if the file name contains an executable.
  24.     Should be None for a document file.
  25. dir : string
  26.     The initial directory for the application.
  27. bShow : int
  28.     Specifies whether the application is shown when it is opened.
  29.     If the lpszFile parameter specifies a document file, this parameter is zero.
  30. ----------------------------------------------------------------------------------
  31.  
  32. Notes:  * Usually, the 'name' is the same as 'ssid'.
  33.         * These functions are meant to be used on Windows.
  34.         * It doesn't handle the exceptions when you don't give/allow admin privileges,
  35.             feel free to add you own lines to do this.
  36.         * I tested the functions on Windows 7 Professional 32 bits.
  37.         * In order to work on a profile/ssid that is not saved in your computer,
  38.             you need to manually try to connect to it first.
  39. """
  40. import socket
  41. from win32api import ShellExecute
  42.  
  43.  
  44. def disconnect():
  45.     """Disconenct from the 'wifi'."""
  46.     disconnect_str = "netsh wlan disconnect"
  47.     ShellExecute(0, None, 'cmd', '/C' + disconnect_str, '', 0)
  48.  
  49. def connect(ssid, keep=False):
  50.     """Try to conenct to the given ssid."""
  51.     if keep is True:
  52.         k = "/K"
  53.     else:
  54.         k = '/C'
  55.     connect_str = f"netsh wlan connect name={ssid} ssid={ssid}"
  56.     ShellExecute(0, None, 'cmd', k + connect_str, '', 0)
  57.  
  58. def change_password(ssid, new_passoword):
  59.     """Changes the password of a saved wifi profile"""
  60.     change_str = f"netsh wlan set profileparameter name={ssid} keyMaterial={new_passoword}"
  61.     ShellExecute(0, None, 'cmd', '/C' + change_str, '', 0)
  62.  
  63. def show_password(ssid):
  64.     """Show the password for the given ssid. Admin privileges required."""
  65.     show_str = f"netsh wlan show profile name={ssid} key=clear"
  66.     ShellExecute(0, 'runas', 'cmd', '/K' + show_str, '', 1)
  67.  
  68. def list_profiles():
  69.     """List all the 'wifi' profiles saved in the computer."""
  70.     list_str = "netsh wlan show profiles"
  71.     ShellExecute(0, None, 'cmd', '/K' + list_str, '', 1)
  72.  
  73. # def connectar(ssid, password):
  74. def check_connection(url='www.google.com'):
  75.     try:
  76.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  77.         s.connect((url, 80))
  78.         s.close()
  79.         return True
  80.     except Exception as e:
  81.         print(e)
  82.         return False
  83.  
  84. def simple_connect(ssid, password):
  85.     """Try to conenct to the given ssid using password."""
  86.     change_password(ssid, clave)
  87.     time.sleep(1)
  88.     connect(ssid)
  89.     time.sleep(6)
  90.     if check_connection():
  91.         print(f"Conectado a {ssid} con clave {clave}")
  92.     else:
  93.         print(f"No se pudo conectar a {ssid} usando la clave {clave}")
Add Comment
Please, Sign In to add comment