From 2c032d8f1c6292c1338a615e6ec40252889ba85c Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Thu, 22 Jan 2015 00:29:39 +0100 Subject: Imported Upstream version 1.0.0 --- examples/python/dbus/update-ip4-method.py | 33 +++++++++++++++++-------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'examples/python/dbus/update-ip4-method.py') diff --git a/examples/python/dbus/update-ip4-method.py b/examples/python/dbus/update-ip4-method.py index 243738c5..28a22289 100755 --- a/examples/python/dbus/update-ip4-method.py +++ b/examples/python/dbus/update-ip4-method.py @@ -21,15 +21,14 @@ # # This example updates a connection's IPv4 method with the Update() method. # +# This uses the new NM 1.0 setting properties. See add-connection-compat.py +# for a similar example using the backward-compatible properties +# # Configuration settings are described at -# https://developer.gnome.org/NetworkManager/0.9/ref-settings.html +# https://developer.gnome.org/NetworkManager/1.0/ref-settings.html # -import socket, struct, dbus, sys - -def ip_to_int(ip_string): - return struct.unpack("=I", socket.inet_aton(ip_string))[0] - +import dbus, sys if len(sys.argv) < 3: print "Usage: %s [address prefix gateway]" % sys.argv[0] @@ -58,21 +57,25 @@ for c_path in settings.ListConnections(): continue # add IPv4 setting if it doesn't yet exist - if not c_settings.has_key('ipv4'): + if 'ipv4' not in c_settings: c_settings['ipv4'] = {} + # clear existing address info + if c_settings['ipv4'].has_key('addresses'): + del c_settings['ipv4']['addresses'] + if c_settings['ipv4'].has_key('address-data'): + del c_settings['ipv4']['address-data'] + if c_settings['ipv4'].has_key('gateway'): + del c_settings['ipv4']['gateway'] + # set the method and change properties c_settings['ipv4']['method'] = method - if method == "auto": - # remove addresses - c_settings['ipv4']['addresses'] = dbus.Array([], signature=dbus.Signature('au')) - elif method == "manual": + if method == "manual": # Add the static IP address, prefix, and (optional) gateway - gw = 0 + addr = dbus.Dictionary({'address': sys.argv[3], 'prefix': dbus.UInt32(int(sys.argv[4]))}) + c_settings['ipv4']['address-data'] = dbus.Array([addr], signature=dbus.Signature('a{sv}')) if len(sys.argv) == 6: - gw = ip_to_int(sys.argv[5]) - addr = dbus.Array([ip_to_int(sys.argv[3]), dbus.UInt32(int(sys.argv[4])), gw], signature=dbus.Signature('u')) - c_settings['ipv4']['addresses'] = dbus.Array([addr], signature=dbus.Signature('au')) + c_settings['ipv4']['gateway'] = sys.argv[5] # Save all the updated settings back to NetworkManager c_obj.Update(c_settings) -- cgit 1.3.0-6-gf8a5