我正在玩一段代码:https://github.com/wraith-wireless/PyRIC/blob/master/pyric/utils/rfkill.py,试图得到rfkill_list()
使用 Kali-linux (应该是 Debian 滚动...??)
#!/usr/bin/env python3
"""
https://github.com/wraith-wireless/PyRIC/blob/master/pyric/utils/rfkill.py
rfkill writes and reads rfkill_event structures to /dev/rfkill using fcntl
Results and useful information can be found in /sys/class/rfkill which contains
one or more rfkill<n> directories where n is the index of each 'wireless'
device. In each rfkill<n> are several files some of which are:
o type: type of device i.e. wlan, bluetooth etc
o name: in the case of 802.11 cards this is the physical name
"""
dpath = '/dev/rfkill'
spath = '/sys/class/rfkill'
ipath = 'sys/class/ieee80211' # directory of physical indexes
import sys
_PY3_ = sys.version_info.major == 3
RFKILL_STATE = [False,True] # Unblocked = 0, Blocked = 1
import fcntl
import os
import struct
import pyric
import errno
import pyric.net.wireless.rfkill_h as rfkh
def getname(idx):
"""
returns the phyical name of the device
:param idx: rfkill index
:returns: the name of the device
"""
fin = None
try:
fin = open(os.path.join(spath,"rfkill{0}".format(idx),'name'),'r')
return fin.read().strip()
except IOError:
raise pyric.error(errno.EINVAL,"No device at {0}".format(idx))
finally:
if fin: fin.close()
def rfkill_list():
"""
list rfkill event structures (rfkill list)
:returns: a dict of dicts name -> {idx,type,soft,hard}
"""
rfks = {}
fin = open(dpath,'r') # this will raise an IOError if rfkill is not supported
flags = fcntl.fcntl(fin.fileno(),fcntl.F_GETFL)
print('flags : ', flags, 'rfkh : ', rfkh.RFKILLEVENTLEN)
fcntl.fcntl(fin.fileno(),fcntl.F_SETFL,flags|os.O_NONBLOCK)
while True:
try:
stream = fin.read(rfkh.RFKILLEVENTLEN)
print('stream : ',type(stream), ' len ; ', len(stream), bytes(stream,'ascii'))
if _PY3_:
# noinspection PyArgumentList
stream = bytes(stream,'ascii')
if len(stream) < rfkh.RFKILLEVENTLEN: raise IOError('python 3')
idx,t,op,s,h = struct.unpack(rfkh.rfk_rfkill_event,stream)
if op == rfkh.RFKILL_OP_ADD:
rfks[getname(idx)] = {'idx':idx,
'type':rfkh.RFKILL_TYPES[t],
'soft':RFKILL_STATE[s],
'hard':RFKILL_STATE[h]}
except IOError as excp:
print('error ',excp)
break
fin.close()
return rfks
print(rfkill_list())
我对此不确定,但我希望代码打印出包含我所有无线接口的字典字典rfkill list
:
0: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
1: phy1: Wireless LAN
Soft blocked: no
Hard blocked: no
2: phy2: Wireless LAN
Soft blocked: no
Hard blocked: no
但运行我的脚本(如上所述)给了我:
{'phy0': {'idx': 0, 'type': 'wlan', 'soft': False, 'hard': False}}
根据 :https://github.com/torvalds/linux/blob/master/include/uapi/linux/rfkill.h
* Structure used for userspace communication on /dev/rfkill,
* used for events from the kernel and control to the kernel.
*/
struct rfkill_event {
__u32 idx;
__u8 type;
__u8 op;
__u8 soft;
__u8 hard;
} __attribute__((packed));
而我的脚本以流形式给出:
stream : <class 'str'> len ; 8 b'\x00\x00\x00\x00\x01\x00\x00\x00'
stream : <class 'str'> len ; 8 b'\x00\x01\x00\x00\x00\x01\x00\x00'
stream : <class 'str'> len ; 8 b'\x00\x00\x02\x00\x00\x00\x01\x00'
并使用另一个脚本来阅读整个dev/rfkill
我得到:
len stream decoded : ---> 27
b'\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x00'
我错过了什么吗?这是预期的结果吗,为什么我没有同时得到 phy0、phy1 和 phy2 ?
看起来 API 中每个phy#
.
我读书dev/rfill
的方式不对吗?
请让我知道我的代码有什么问题。
关于代码:
rfk_rfkill_event = "IBBBB" #(that should be the same of "@IBBBB" )
RFKILLEVENTLEN = struct.calcsize(rfk_rfkill_event)
是里面的东西rfkh.RFKILLEVENTLEN