Python3 通过dbus获取客户端缓存的登录用户

  • Post author:
  • Post category:python


DBus分为两种类型:

system bus(系统总线),用于系统(Linux)和用户程序之间进行通信和消息的传递;

session bus(回话总线),用于桌面(GNOME, KDE等)用户程序之间进行通信。

import dbus
session_bus = dbus.SessionBus()
system_bus = dbus.SystemBus()

To obtain a proxy object, call the

get_object

method on the

Bus

. For example,

NetworkManager

has the well-known name

org.freedesktop.NetworkManager

and exports an object whose object path is

/org/freedesktop/NetworkManager

, plus an object per network interface at object paths like

/org/freedesktop/NetworkManager/Devices/eth0

. You can get a proxy for the object representing eth0 like this:

要获取代理对象,请在总线上调用get_object方法。例如,NetworkManager有一个众所周知的名称org.freedesktop.NetworkManager并导出一个对象路径为/org/freedesktop/NetworkManager的对象,再加上对象路径为/org/freedesktop/NetworkManager/Devices/eth0的每个网络接口的对象。您可以获取代表此对象的eth0:

proxy = system_bus.get_object('org.freedesktop.NetworkManager',
                       '/org/freedesktop/NetworkManager/Devices/0')
# 通过Interface对象间接调用
eth0_dev_iface = dbus.Interface(proxy,dbus_interface='org.freedesktop.DBus.Introspectable') 
props2 = eth0_dev_iface.Introspect() 

实现: 通过dbus获取客户端缓存的登录用户。

先打开终端,输入gdbus introspect     –system     –dest org.freedesktop.Accounts    –object-path /org/freedesktop/Accounts 简洁查看接口和对应的方法。

node /org/freedesktop/Accounts {
  interface org.freedesktop.DBus.Properties {
    methods:
      Get(in  s interface_name,
          in  s property_name,
          out v value);
      GetAll(in  s interface_name,
             out a{sv} properties);
      Set(in  s interface_name,
          in  s property_name,
          in  v value);
    signals:
      PropertiesChanged(s interface_name,
                        a{sv} changed_properties,
                        as invalidated_properties);
    properties:
  };
  interface org.freedesktop.DBus.Introspectable {
    methods:
      Introspect(out s xml_data);
    signals:
    properties:
  };
  interface org.freedesktop.DBus.Peer {
    methods:
      Ping();
      GetMachineId(out s machine_uuid);
    signals:
    properties:
  };
  interface org.freedesktop.Accounts {
    methods:
      @org.freedesktop.DBus.GLib.Async("")
      ListCachedUsers(out ao users);
      @org.freedesktop.DBus.GLib.Async("")
      FindUserById(in  x id,
                   out o user);
      @org.freedesktop.DBus.GLib.Async("")
      FindUserByName(in  s name,
                     out o user);
      @org.freedesktop.DBus.GLib.Async("")
      CreateUser(in  s name,
                 in  s fullname,
                 in  i accountType,
                 out o user);
      @org.freedesktop.DBus.GLib.Async("")
      CacheUser(in  s name,
                out o user);
      @org.freedesktop.DBus.GLib.Async("")
      UncacheUser(in  s name);
      @org.freedesktop.DBus.GLib.Async("")
      DeleteUser(in  x id,
                 in  b removeFiles);
    signals:
      UserAdded(o user);
      UserDeleted(o user);
    properties:
      readonly s DaemonVersion = '0.6.55';
      readonly b HasNoUsers = false;
      readonly b HasMultipleUsers = false;
      readonly ao AutomaticLoginUsers = [];
  };
  node User1000 {
  };
};

完整代码如下:

#!/usr/bin/python3

import dbus

if __name__ == '__main__':
    # 系统总线
    system_dbus = dbus.SystemBus()
    proxy = system_dbus.get_object('org.freedesktop.Accounts', '/org/freedesktop/Accounts')

    manager = dbus.Interface(
        proxy,
        'org.freedesktop.Accounts'
    ) --指定要调用的接口

    props = manager.ListCachedUsers()  #调用ListCachedUsers查看全部的缓存用户
    print("props: ", props)

    finduser = manager.FindUserByName('zs')  #根据用户名找到用户
    print("finduser: ", finduser)

参考链接:


https://dbus.freedesktop.org/doc/dbus-python/tutorial.html#making-method-calls


https://www.wanghongxu.cn/2019/12/27/zai-pythonzhong-shi-yong-dbushe-systemdtong-xun/



版权声明:本文为Katherine130原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。