Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to connect wifi using python

import os


class Finder:
    def __init__(self, *args, **kwargs):
        self.server_name = kwargs['server_name']
        self.password = kwargs['password']
        self.interface_name = kwargs['interface']
        self.main_dict = {}

    def run(self):
        command = """sudo iwlist wlp2s0 scan | grep -ioE 'ssid:"(.*{}.*)'"""
        result = os.popen(command.format(self.server_name))
        result = list(result)

        if "Device or resource busy" in result:
                return None
        else:
            ssid_list = [item.lstrip('SSID:').strip('"
') for item in result]
            print("Successfully get ssids {}".format(str(ssid_list)))

        for name in ssid_list:
            try:
                result = self.connection(name)
            except Exception as exp:
                print("Couldn't connect to name : {}. {}".format(name, exp))
            else:
                if result:
                    print("Successfully connected to {}".format(name))

    def connection(self, name):
        try:
            os.system("nmcli d wifi connect {} password {} iface {}".format(name,
       self.password,
       self.interface_name))
        except:
            raise
        else:
            return True

if __name__ == "__main__":
    # Server_name is a case insensitive string, and/or regex pattern which demonstrates
    # the name of targeted WIFI device or a unique part of it.
    server_name = "example_name"
    password = "your_password"
    interface_name = "your_interface_name" # i. e wlp2s0  
    F = Finder(server_name=server_name,
               password=password,
               interface=interface_name)
    F.run()
Comment

PREVIOUS NEXT
Code Example
Python :: python read in integers separated by spaces 
Python :: argparse required arguments 
Python :: python num perfect squares 
Python :: color name to hex python 
Python :: how to take input in python 
Python :: django.db.utils.ProgrammingError: relation "users" does not exist in django 3.0 
Python :: creating a list in python 
Python :: found features with object datatype 
Python :: make a nested list flat python 
Python :: selenium get parent element 
Python :: import excel python 
Python :: python list only files not directories 
Python :: python string to datetime object 
Python :: python convert to percentage 
Python :: from array to tuple python 
Python :: python data structures 9.4 
Python :: simple secatter plot 
Python :: how to write post method using flask 
Python :: python multiline string 
Python :: python add to file 
Python :: print even numbers in python 
Python :: calculate the distance between two points 
Python :: ejercicios con funciones en python 
Python :: Python pandas first and last element of column 
Python :: how to send file in django response 
Python :: pandas sep 
Python :: create square matrix python 
Python :: matplotlib boxplot colors 
Python :: django orm sum 
Python :: create dataframe from two variables 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =