나는 코드 조각을 가지고 놀고 있습니다.https://github.com/wraith-wireless/PyRIC/blob/master/pyric/utils/rfkill.py, 얻으려고 노력 중rfkill_list()
Kali-linux 사용(데비안 롤링이어야 합니다...??)
#!/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를 동시에 얻지 못하는 이유는 무엇입니까?
그것은 모든 것처럼 보입니다 phy#
.
내가 읽는 방식이 dev/rfill
잘못된 걸까?
내 코드에 어떤 문제가 있는지 알려주세요.
코드 정보:
rfk_rfkill_event = "IBBBB" #(that should be the same of "@IBBBB" )
RFKILLEVENTLEN = struct.calcsize(rfk_rfkill_event)
안에 들어 있는 게 바로 이거야rfkh.RFKILLEVENTLEN