簡體   English   中英

從python訪問iwd dbus接口

[英]Accessing iwd dbus interface from python

我正在嘗試使用 python3 dbus 包來控制嵌入式 Linux 目標上的無線(mips MT7628,如果重要的話)。

系統很可能設置正確,因為我在這個嵌入式目標上啟動並運行了 iwd + dhclient。

我可以通過 eth0 和 wlan0 連接。

我也可以通過 iwctl 控制 iwd。 到現在為止還挺好。

我現在需要從 python3 控制 iwd; 具體來說,我需要能夠通過串行線發送掃描結果(真正的目標,與我的開發板相反,將是無頭的)。

我對 dbus 接口不知所措。 我需要一些例子來開始。

我嘗試了一些方法:

# python
Python 3.8.1 (default, Feb 21 2020, 11:13:38) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbus
>>> bus = dbus.SystemBus()
>>> wlan0 = bus.get_object('net.connman.iwd', '/net/connman/iwd/phy1/1')
>>> p = wlan0.Properties(dbus_interface='net.connman.iwd.Station')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/site-packages/dbus/proxies.py", line 72, in __call__
  File "/usr/lib/python3.8/site-packages/dbus/proxies.py", line 141, in __call__
  File "/usr/lib/python3.8/site-packages/dbus/connection.py", line 652, in call_blocking
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotFound: No matching method found
>>> p = wlan0.Scan(dbus_interface='net.connman.iwd.Station')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/site-packages/dbus/proxies.py", line 141, in __call__
  File "/usr/lib/python3.8/site-packages/dbus/connection.py", line 652, in call_blocking
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotFound: No matching method found
>>> 

這在 WiFi 工作時:

# iwctl 
[iwd]# adapter list
                                    Adapters                                  *
--------------------------------------------------------------------------------
  Name               Powered   Vendor              Model               
--------------------------------------------------------------------------------
  phy1               on        --

[iwd]# device list
                                    Devices                                   *
--------------------------------------------------------------------------------
  Name                Address             Powered   Adapter   Mode      
--------------------------------------------------------------------------------
  wlan1               42:c1:f8:85:67:ba   on        phy1      station   

[iwd]# station list
                            Devices in Station Mode                           *
--------------------------------------------------------------------------------
  Name                State          Scanning
--------------------------------------------------------------------------------
  wlan1               connected              

[iwd]# 

...但我顯然錯過了一些非常基本的東西。

有人能指出我正確的方向嗎?

回答我自己的問題以幫助任何會在這里絆倒的人:

事實證明,iwd 和 connman 存儲庫都在子目錄“test”中提供了完整的 python3 示例。

使這些適應我的特定需求被證明是微不足道的。

相關目錄可以在這里 (iwd)這里 (connman) 找到

我的具體問題的解決方案是在以下腳本中: scan-for-networkslist-known-networks

掃描網絡:

#!/usr/bin/python3

import sys
import dbus


if (len(sys.argv) != 2):
    print("Usage: %s <device>" % (sys.argv[0]))
    sys.exit(1)

bus = dbus.SystemBus()
device = dbus.Interface(bus.get_object("net.connman.iwd", sys.argv[1]),
                                    "net.connman.iwd.Station")
device.Scan()

list_known_networks:

#!/usr/bin/python3

import sys
import dbus

bus = dbus.SystemBus()

manager = dbus.Interface(bus.get_object('net.connman.iwd', '/'),
                         'org.freedesktop.DBus.ObjectManager')

forget = None
if len(sys.argv) >= 4 and sys.argv[1] == 'forget':
    forget = (sys.argv[2], sys.argv[3])

print('Known Networks:')

for path, interfaces in manager.GetManagedObjects().items():
    if 'net.connman.iwd.KnownNetwork' not in interfaces:
        continue

    network = interfaces['net.connman.iwd.KnownNetwork']

    if (network['Name'], network['Type']) == forget:
        obj = dbus.Interface(bus.get_object('net.connman.iwd', path),
                             'net.connman.iwd.KnownNetwork')
        obj.Forget()
        continue

    print("[ %s ]" % network['Name'])

    for key in network:
        val = network[key]
        if type(val) == dbus.Boolean:
            val = 'True' if val else 'False'
        print("    %s = %s" % (key, val))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM