You are hereBlogs / meck's blog / Using D-Bus to communicate with Gnome Network Manager
Using D-Bus to communicate with Gnome Network Manager
Today, I tried to figure out how to execute scripts in Ubuntu on network connection changes. I came across the D-Bus system, which is used for inter-application communication in Linux. It is indeed very easy to access D-Bus within a Python program. If you read on, you will see the source code of a Python script, that - when launched - receives events from the Gnome Network Manager via the D-Bus interface.
The script should be quite self-explaining. I do not feel confident enough to explain the D-Bus details. You should consider the references below, if you need more information.
from dbus.mainloop.glib import DBusGMainLoop import dbus import gobject # set up the main loop DBusGMainLoop(set_as_default=True) loop = gobject.MainLoop() # get set system bus system_bus = dbus.SystemBus() # constants for accessing gnome network manager NM_BUS_NAME = 'org.freedesktop.NetworkManager' NM_OBJECT_PATH = '/org/freedesktop/NetworkManager' NM_INTERF_NAME = 'org.freedesktop.NetworkManager' # get gnome network manager interface network_manager = system_bus.get_object(NM_BUS_NAME, NM_OBJECT_PATH) network_manager_interface = dbus.Interface(network_manager, NM_INTERF_NAME) # define handlers for network status changes def deviceUp(*devices): for dev in devices: print "Network device %s is now active!" % dev # connect the signals of network status changes to handlers network_manager_interface.connect_to_signal("DeviceNowActive", deviceUp) # execute the main loop loop.run()
References
-
http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html
- great - maybe a bit outdated - D-Bus tutorial
- http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt
- description of the Gnome Network Manager D-Bus interface
- meck's blog
- Login to post comments