Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- WIPY
- ====
- Description: Simple functions to do some tasks related to the wifi network.
- OS: Windows.
- requirements: pywin32
- QUICK REFERENCE:
- ----------------
- win32api.ShellExecute(hwnd, op, file, params, dir, bShow)
- Parameters:
- -----------
- hwnd : PyHANDLE
- The handle of the parent window, or 0 for no parent.
- This window receives any message boxes an application produces (for example, for error reporting).
- op : string
- The operation to perform. May be "open", "print", or None, which defaults to "open".
- file : string
- The name of the file to open.
- params : string
- The parameters to pass, if the file name contains an executable.
- Should be None for a document file.
- dir : string
- The initial directory for the application.
- bShow : int
- Specifies whether the application is shown when it is opened.
- If the lpszFile parameter specifies a document file, this parameter is zero.
- ----------------------------------------------------------------------------------
- Notes: * Usually, the 'name' is the same as 'ssid'.
- * These functions are meant to be used on Windows.
- * It doesn't handle the exceptions when you don't give/allow admin privileges,
- feel free to add you own lines to do this.
- * I tested the functions on Windows 7 Professional 32 bits.
- * In order to work on a profile/ssid that is not saved in your computer,
- you need to manually try to connect to it first.
- """
- import socket
- from win32api import ShellExecute
- def disconnect():
- """Disconenct from the 'wifi'."""
- disconnect_str = "netsh wlan disconnect"
- ShellExecute(0, None, 'cmd', '/C' + disconnect_str, '', 0)
- def connect(ssid, keep=False):
- """Try to conenct to the given ssid."""
- if keep is True:
- k = "/K"
- else:
- k = '/C'
- connect_str = f"netsh wlan connect name={ssid} ssid={ssid}"
- ShellExecute(0, None, 'cmd', k + connect_str, '', 0)
- def change_password(ssid, new_passoword):
- """Changes the password of a saved wifi profile"""
- change_str = f"netsh wlan set profileparameter name={ssid} keyMaterial={new_passoword}"
- ShellExecute(0, None, 'cmd', '/C' + change_str, '', 0)
- def show_password(ssid):
- """Show the password for the given ssid. Admin privileges required."""
- show_str = f"netsh wlan show profile name={ssid} key=clear"
- ShellExecute(0, 'runas', 'cmd', '/K' + show_str, '', 1)
- def list_profiles():
- """List all the 'wifi' profiles saved in the computer."""
- list_str = "netsh wlan show profiles"
- ShellExecute(0, None, 'cmd', '/K' + list_str, '', 1)
- # def connectar(ssid, password):
- def check_connection(url='www.google.com'):
- try:
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect((url, 80))
- s.close()
- return True
- except Exception as e:
- print(e)
- return False
- def simple_connect(ssid, password):
- """Try to conenct to the given ssid using password."""
- change_password(ssid, clave)
- time.sleep(1)
- connect(ssid)
- time.sleep(6)
- if check_connection():
- print(f"Conectado a {ssid} con clave {clave}")
- else:
- print(f"No se pudo conectar a {ssid} usando la clave {clave}")
Add Comment
Please, Sign In to add comment