我希望只使用python标准库在接口上获得活动的essid;我只需要支持linux环境。我该怎么做?
最佳答案
这可以使用SIOCGIWESSID
ioctl
调用来完成。
这段代码可能看起来有点混乱,因为它更类似于您在C代码中看到的东西,而不是Python,但它实际上是通过首先以Python数组的形式分配一些内存(我们将在其中放置ESSID),然后执行一个ioctl
调用来修改该数组。
import array
import fcntl
import socket
import struct
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
maxLength = {
"interface": 16,
"essid": 32
}
calls = {
"SIOCGIWESSID": 0x8B1B
}
def getESSID(interface):
"""Return the ESSID for an interface, or None if we aren't connected."""
essid = array.array("c", "\0" * maxLength["essid"])
essidPointer, essidLength = essid.buffer_info()
request = array.array("c",
interface.ljust(maxLength["interface"], "\0") +
struct.pack("PHH", essidPointer, essidLength, 0)
)
fcntl.ioctl(sock.fileno(), calls["SIOCGIWESSID"], request)
name = essid.tostring().rstrip("\0")
if name:
return name
return None
关于python - 如何获取接口(interface)上的事件ESSID?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14142014/