用python或linux终端连接wifi [英] connect wifi with python or linux terminal
问题描述
我正在尝试通过 python 和 linux 终端连接到 wifi,但在这两种情况下它都不适合我.
I am trying to connect to wifi through python and linux terminal but in both cases it is not working with me.
对于 python,我正在使用这个库 https://wifi.readthedocs.org/en/latest/scanning.html扫描和保存方案工作正常,但每当我输入这行代码时scheme.activate() 没有输出
For python, I am using this library https://wifi.readthedocs.org/en/latest/scanning.html scanning and saving the scheme is working fine but whenever I type this line of code scheme.activate() and I get no output
知道这个库有什么问题吗?你以前是否使用过它??
Any ideas what is wrong with the library and if you have used it before or not??
我还尝试使用 CLI 连接到 WiFi 网络.我谷歌了一下,发现我应该做这三个陈述1- iwlist wlan0 scan//扫描无线网络2- iwconfig wlan0 essid "Mywirelessnetwork"//与网络关联3- dhclient wla0//获得 UP
I tried also to connect to WiFi networks using the CLI. I Googled and found that I should do these three statements 1- iwlist wlan0 scan // to scan the wireess networks 2- iwconfig wlan0 essid "Mywirelessnetwork" // to associate with the network 3- dhclient wla0 // To get an UP
每当我执行第 2 步然后检查 iwconfig wlan0 时,我发现无线接口没有关联!!
Whenever I do step 2 and then check iwconfig wlan0 I found that the wireless interface is not associated !!
有什么想法吗???
我想要做的是拥有一个连接到 wifi 的方法库,最好是通过 python 函数或库,并在树莓派上进行测试,因为我正在构建一些需要网络连接的应用程序.
What I am trying to do is to have a library of a way to connect to the wifi preferably through a python function or a library and tested on raspberry PI because I am building some applications that require network connection.
推荐答案
这里是使用 python os
模块和 Linux iwlist
命令搜索列表的一般方法wifi 设备和 nmcli
命令以连接到目标设备.
Here is a general approach using python os
module and Linux iwlist
command for searching through the list of wifi devices and nmcli
command in order to connect to a the intended device.
在此代码中,运行函数查找与您指定的名称(可以是正则表达式模式或服务器名称的唯一部分)匹配的设备的 SSID,然后连接到与您的预期条件匹配的所有设备,通过调用 connection
函数.
In this code the run function finds the SSID of devices that match with your specified name (which can be a regex pattern or a unique part of the server name) then connects to all the devices that match with your expected criteria, by calling the connection
function.
"""
Search for a specific wifi ssid and connect to it.
written by kasramvd.
"""
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()
这篇关于用python或linux终端连接wifi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!