我尝试使用openvpn.service
D-Bus API提取Systemd
状态。
In [1]: import dbus
In [2]: sysbus = dbus.SystemBus()
In [3]: systemd1 = sysbus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
In [4]: manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager')
In [5]: service = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Service')
In [6]: unit = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Unit')
In [7]: unit.ActiveState('openvpn.service')
---------------------------------------------------------------------------
DBusException Traceback (most recent call last)
<ipython-input-7-22857e7dcbd7> in <module>()
----> 1 unit.ActiveState('openvpn.service')
/usr/local/lib/python3.4/dist-packages/dbus/proxies.py in __call__(self, *args, **keywords)
68 # we're being synchronous, so block
69 self._block()
---> 70 return self._proxy_method(*args, **keywords)
71
72 def call_async(self, *args, **keywords):
/usr/local/lib/python3.4/dist-packages/dbus/proxies.py in __call__(self, *args, **keywords)
143 signature,
144 args,
--> 145 **keywords)
146
147 def call_async(self, *args, **keywords):
/usr/local/lib/python3.4/dist-packages/dbus/connection.py in call_blocking(self, bus_name, object_path, dbus_interface, method, signature, args, timeout, byte_arrays, **kwargs)
649 # make a blocking call
650 reply_message = self.send_message_with_reply_and_block(
--> 651 message, timeout)
652 args_list = reply_message.get_args_list(**get_args_opts)
653 if len(args_list) == 0:
DBusException: org.freedesktop.DBus.Error.UnknownMethod: Unknown method 'ActiveState' or interface 'org.freedesktop.systemd1.Unit'.
In [8]: manager.GetUnit('openvpn.service')
Out[8]: dbus.ObjectPath('/org/freedesktop/systemd1/unit/openvpn_2eservice')
In [9]: u = manager.GetUnit('openvpn.service')
In [10]: u.ActiveState
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-6998e589f206> in <module>()
----> 1 u.ActiveState
AttributeError: 'dbus.ObjectPath' object has no attribute 'ActiveState'
In [11]: manager.GetUnitFileState('openvpn.service')
Out[11]: dbus.String('enabled')
有一个ActiveState
属性,contains a state value that reflects whether the unit is currently active or not
。我已成功阅读UnitFileState
,但我无法弄清楚如何阅读ActiveState
属性。
答案 0 :(得分:2)
看了some random examples [more permanent link]之后,我有些运气了
import dbus
sysbus = dbus.SystemBus()
systemd1 = sysbus.get_object('org.freedesktop.systemd1',
'/org/freedesktop/systemd1')
manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager')
service = sysbus.get_object('org.freedesktop.systemd1',
object_path=manager.GetUnit('openvpn.service'))
interface = dbus.Interface(service,
dbus_interface='org.freedesktop.DBus.Properties')
print(interface.Get('org.freedesktop.systemd1.Unit', 'ActiveState'))
[编辑:] 记录版本,以防万一:
dbus.version == (1, 2, 14)
systemd 244 (244.1-1-arch)
Python 3.8.1
答案 1 :(得分:0)
您可能需要调用dbus.Interface(sysbus.get_object('org.freedesktop.systemd1', u), 'org.freedesktop.systemd1.Unit')
并访问该对象上的ActiveState
属性。您的代码当前尝试访问对象路径上的ActiveState
属性,该对象本身不是对象。