dev/rfkill および PyRIC ライブラリ、rfkill.py: rfkill_list() 問題がすべてのデバイスを見つけることができない

dev/rfkill および PyRIC ライブラリ、rfkill.py: rfkill_list() 問題がすべてのデバイスを見つけることができない

コードスニペットで遊んでいます。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を同時に取得できないのはなぜですか?

それはすべてのように見えますphy#

私が読む方法がdev/rfill間違っていますか?

私のコードに何が問題なのか教えてください。

コード情報:

rfk_rfkill_event = "IBBBB"  #(that should be the same of "@IBBBB" )
RFKILLEVENTLEN = struct.calcsize(rfk_rfkill_event)    

中に入っているのがまさにこれだrfkh.RFKILLEVENTLEN

ベストアンサー1

おすすめ記事