Advertisement
here2share

# EZPyFi.py

Nov 30th, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. # EZPyFi.py
  2.  
  3. # Easy way to connect Wifi without any modules
  4.  
  5. import os
  6.  
  7. class Finder:
  8.     def __init__(self, *args, **kwargs):
  9.         self.server_name = kwargs['server_name']
  10.         self.password = kwargs['password']
  11.         self.interface_name = kwargs['interface']
  12.         self.main_dict = {}
  13.  
  14.     def run(self):
  15.         command = """sudo iwlist wlp2s0 scan | grep -ioE 'ssid:"(.*{}.*)'"""
  16.         result = os.popen(command.format(self.server_name))
  17.         result = list(result)
  18.  
  19.         if "Device or resource busy" in result:
  20.                 return None
  21.         else:
  22.             ssid_list = [item.lstrip('SSID:').strip('"\n') for item in result]
  23.             print("Successfully get ssids {}".format(str(ssid_list)))
  24.  
  25.         for name in ssid_list:
  26.             try:
  27.                 result = self.connection(name)
  28.             except Exception as exp:
  29.                 print("Couldn't connect to name : {}. {}".format(name, exp))
  30.             else:
  31.                 if result:
  32.                     print("Successfully connected to {}".format(name))
  33.  
  34.     def connection(self, name):
  35.         try:
  36.             os.system("nmcli d wifi connect {} password {} iface {}".format(name,
  37.        self.password,
  38.        self.interface_name))
  39.         except:
  40.             raise
  41.         else:
  42.             return True
  43.  
  44. if __name__ == "__main__":
  45.     # Server_name is a case insensitive string, and/or regex pattern which demonstrates
  46.     # the name of targeted WIFI device or a unique part of it.
  47.     server_name = "example_name"
  48.     password = "your_password"
  49.     interface_name = "your_interface_name" # i. e wlp2s0  
  50.     F = Finder(server_name=server_name,
  51.                password=password,
  52.                interface=interface_name)
  53.     F.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement