summaryrefslogtreecommitdiff
path: root/examples/python/gi/update-ip4-method.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/python/gi/update-ip4-method.py')
-rwxr-xr-xexamples/python/gi/update-ip4-method.py48
1 files changed, 19 insertions, 29 deletions
diff --git a/examples/python/gi/update-ip4-method.py b/examples/python/gi/update-ip4-method.py
index 79889232ca..983578cf4f 100755
--- a/examples/python/gi/update-ip4-method.py
+++ b/examples/python/gi/update-ip4-method.py
@@ -24,22 +24,11 @@
# using the libnm GObject-based convenience APIs.
#
# 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
#
from gi.repository import GLib, NM
-import sys, struct, socket
-
-def ip_to_int(ip_string):
- return struct.unpack("=I", socket.inet_aton(ip_string))[0]
-
-# callback function
-def commit_cb(connection, error, data):
- if error is (None):
- print("The connection profile has been updated.")
- else:
- print(error)
- main_loop.quit()
+import sys, socket
if __name__ == "__main__":
# parse and validate arguments
@@ -48,7 +37,7 @@ if __name__ == "__main__":
sys.exit(1)
method = sys.argv[2]
- if method == "static" or method == "manual" and len(sys.argv) < 5:
+ if (method == "static" or method == "manual") and len(sys.argv) < 5:
print "Usage: %s %s static address prefix [gateway]" % (sys.argv[0], sys.argv[1])
sys.exit(1)
@@ -60,10 +49,10 @@ if __name__ == "__main__":
main_loop = GLib.MainLoop()
- # create RemoteSettings object
- settings = NM.RemoteSettings.new(None)
+ # create Client object
+ client = NM.Client.new(None)
- all_connections = settings.list_connections()
+ all_connections = client.get_connections()
for c in all_connections:
if c.get_uuid() != uuid:
continue
@@ -75,21 +64,22 @@ if __name__ == "__main__":
c.add_setting(s_ip4)
# set the method and change properties
- s_ip4.set_property(NM.SETTING_IP4_CONFIG_METHOD, method)
+ s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, method)
if method == "auto":
- # remove addresses
+ # remove addresses and gateway
s_ip4.clear_addresses()
+ s_ip4.props.gateway = None
elif method == "manual":
# Add the static IP address, prefix, and (optional) gateway
- addr = NM.IP4Address.new()
- addr.set_address(ip_to_int(sys.argv[3]))
- addr.set_prefix(int(sys.argv[4]))
- if len(sys.argv) == 6:
- addr.set_gateway(ip_to_int(sys.argv[5]))
+ addr = NM.IPAddress.new(socket.AF_INET, sys.argv[3], int(sys.argv[4]))
s_ip4.add_address(addr)
-
- c.commit_changes(commit_cb, None)
-
- # run main loop
- main_loop.run()
+ if len(sys.argv) == 6:
+ s_ip4.props.gateway = sys.argv[5]
+
+ try:
+ c.commit_changes(True, None)
+ print("The connection profile has been updated.")
+ except Exception, e:
+ print("Error: %s" % e)
+ break