diff --git a/Makefile.am b/Makefile.am index cda6437ccf..ca819b4634 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,20 +1,20 @@ -SUBDIRS = marshallers - -SUBDIRS += \ - libnm-util \ - libnm-glib \ - src \ - include \ - introspection \ - callouts \ - system-settings \ - tools \ - policy \ - initscript \ - test \ - po \ - man \ - docs +SUBDIRS = \ + marshallers \ + libnm-util \ + libnm-glib \ + src \ + include \ + introspection \ + callouts \ + system-settings \ + tools \ + policy \ + initscript \ + test \ + po \ + man \ + docs \ + examples EXTRA_DIST = \ CONTRIBUTING \ diff --git a/configure.ac b/configure.ac index 124f47c713..6c70b458cf 100644 --- a/configure.ac +++ b/configure.ac @@ -494,6 +494,8 @@ docs/Makefile docs/libnm-glib/Makefile docs/libnm-util/Makefile NetworkManager.pc +examples/Makefile +examples/python/Makefile ]) AC_OUTPUT diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000000..c2ddf781cc --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1 @@ +SUBDIRS=python diff --git a/examples/python/Makefile.am b/examples/python/Makefile.am new file mode 100644 index 0000000000..a64727a7fd --- /dev/null +++ b/examples/python/Makefile.am @@ -0,0 +1,4 @@ +EXTRA_DIST = \ + nm-state.py \ + add-system-connection.py + diff --git a/examples/python/add-system-connection.py b/examples/python/add-system-connection.py new file mode 100644 index 0000000000..e9fbe4e6f1 --- /dev/null +++ b/examples/python/add-system-connection.py @@ -0,0 +1,31 @@ +#!/bin/env python + +import dbus + +s_wired = dbus.Dictionary({'duplex': 'full'}) +s_con = dbus.Dictionary({ + 'type': '802-3-ethernet', + 'uuid': '7371bb78-c1f7-42a3-a9db-5b9566e8ca07', + 'id': 'MyConnection'}) + +addr1 = dbus.Array([dbus.UInt32(50462986L), dbus.UInt32(0L), dbus.UInt32(16908554L)], signature=dbus.Signature('u')) +s_ip4 = dbus.Dictionary({ + 'addresses': dbus.Array([addr1], signature=dbus.Signature('au')), + 'method': 'manual'}) + +s_ip6 = dbus.Dictionary({'method': 'ignore'}) + +con = dbus.Dictionary({ + '802-3-ethernet': s_wired, + 'connection': s_con, + 'ipv4': s_ip4, + 'ipv6': s_ip6}) + + +bus = dbus.SystemBus() + +proxy = bus.get_object("org.freedesktop.NetworkManagerSystemSettings", "/org/freedesktop/NetworkManagerSettings") +settings = dbus.Interface(proxy, "org.freedesktop.NetworkManagerSettings") + +settings.AddConnection(con) + diff --git a/examples/python/nm-state.py b/examples/python/nm-state.py new file mode 100644 index 0000000000..86aa321906 --- /dev/null +++ b/examples/python/nm-state.py @@ -0,0 +1,52 @@ +#!/bin/env python + +import dbus + +bus = dbus.SystemBus() + +proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager") +manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager") + +# Get device-specific state +devices = manager.GetDevices() +for d in devices: + dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d) + prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties") + + # Get the device's current state and interface name + state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State") + name = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface") + + # and print them out + if state == 8: # activated + print "Device %s is activated" % name + else: + print "Device %s is not activated" % name + + +# Get active connection state +manager_prop_iface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties") +active = manager_prop_iface.Get("org.freedesktop.NetworkManager", "ActiveConnections") +for a in active: + ac_proxy = bus.get_object("org.freedesktop.NetworkManager", a) + prop_iface = dbus.Interface(ac_proxy, "org.freedesktop.DBus.Properties") + state = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "State") + + # Connections in NM are a collection of settings that describe everything + # needed to connect to a specific network. Lets get those details so we + # can find the user-readable name of the connection. + con_path = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "Connection") + con_service = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "ServiceName") + + # ask the provider of the connection for its details + service_proxy = bus.get_object(con_service, con_path) + con_iface = dbus.Interface(service_proxy, "org.freedesktop.NetworkManagerSettings.Connection") + con_details = con_iface.GetSettings() + con_name = con_details['connection']['id'] + + if state == 2: # activated + print "Connection '%s' is activated" % con_name + else: + print "Connection '%s' is activating" % con_name + +