我希望在仅使用Python标准库的接口上获得活动的ESSID;我只需要支持Linux环境。我该怎么做呢?
发布于 2013-01-03 23:34:05
这可以使用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 Nonehttps://stackoverflow.com/questions/14142014
复制相似问题