Developer Info: Examples, Documentation, and Hacking

Talking to NetworkManager

NetworkManager provides a detailed and capable D-Bus interface on the system bus. You can use this interface to query network state and the details of network interfaces like current IP addresses or DHCP options, and to activate, deactivate, created, edit, and delete saved network connections.

Example time!

Let's get a list of saved network connections using Python, libnm and GObject Introspection:

   1 #!/bin/env python
   2 import gi
   3 gi.require_version('NM', '1.0')
   4 from gi.repository import NM
   5 
   6 if __name__ == "__main__":
   7     # create Client object
   8     client = NM.Client.new(None)
   9 
  10     # get all connections
  11     connections = client.get_connections()
  12 
  13     # print the connections
  14     for c in connections:
  15         print "%27s : %s" % (c.get_id(), c.get_uuid())

or using older libnm-glib:

   1 #!/bin/env python
   2 from gi.repository import GLib, NetworkManager, NMClient
   3 
   4 main_loop = None
   5 
   6 def connections_read(settings):
   7     for c in settings.list_connections():
   8         print "%27s : %s" % (c.get_id(), c.get_uuid())
   9     main_loop.quit()    
  10 
  11 main_loop = GLib.MainLoop()
  12 settings = NMClient.RemoteSettings.new(None);
  13 # wait for connections to be loaded over D-Bus and log
  14 # them from a callback
  15 settings.connect("connections-read", connections_read)
  16 main_loop.run()

D-Bus API Documentation and Examples

NetworkManager's D-Bus interface is the primary way to make changes to network configuration and state. We've published many code examples using basic D-Bus and various helper libraries.

If you're not using helper libraries like libnm, libnm-glib or libnm-qt this API documentation is your reference. If something is confusing, doesn't work, or isn't documented well enough, please ask us, we're here to help!

NetworkManager GLib convenience library Documentation

It wraps the D-Bus API in easy-to-use GObjects and is often much simpler for glib-based applications to use. We've also published code examples in C and more code examples in Python using GObject Introspection and other languages.

Or there are two legacy glib-based convenience libraries called libnm-util and libnm-glib (replaced by newer libnm now).

Show me more examples!

Sure, there's a bunch of examples for Python, C, glib, Qt, Ruby, Lua, and shell right in the git source tree!

Projects/NetworkManager/Developers (last edited 2016-08-03 08:19:13 by LubomirRintel)