progrmam in python to connect to wifi code example
Example: 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('"\n') 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 = "example_name"
password = "your_password"
interface_name = "your_interface_name"
F = Finder(server_name=server_name,
password=password,
interface=interface_name)
F.run()