2014-06-10 15:52:34 -05:00
|
|
|
#!/usr/bin/env python
|
2020-12-23 22:21:36 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2014-06-10 15:52:34 -05:00
|
|
|
#
|
2019-10-01 09:20:35 +02:00
|
|
|
# Copyright (C) 2014 Red Hat, Inc.
|
2014-06-10 15:52:34 -05:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# This example updates a connection's IPv4 method with the Update() method
|
2014-05-19 13:44:02 -04:00
|
|
|
# using the libnm GObject-based convenience APIs.
|
2014-06-10 15:52:34 -05:00
|
|
|
#
|
|
|
|
|
# Configuration settings are described at
|
2021-09-24 14:33:17 +02:00
|
|
|
# https://networkmanager.dev/docs/api/latest/ref-settings.html
|
2014-06-10 15:52:34 -05:00
|
|
|
#
|
|
|
|
|
|
2015-11-11 10:37:07 +01:00
|
|
|
import gi
|
2020-06-09 16:28:32 -04:00
|
|
|
|
|
|
|
|
gi.require_version("NM", "1.0")
|
2014-05-19 13:44:02 -04:00
|
|
|
from gi.repository import GLib, NM
|
2014-11-13 14:27:26 -05:00
|
|
|
import sys, socket
|
2014-06-10 15:52:34 -05:00
|
|
|
|
2014-09-05 13:48:56 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
# parse and validate arguments
|
|
|
|
|
if len(sys.argv) < 3:
|
2017-12-02 12:44:50 +08:00
|
|
|
print("Usage: %s <uuid> <auto|static> [address prefix gateway]" % sys.argv[0])
|
2014-09-05 13:48:56 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
method = sys.argv[2]
|
2014-11-13 14:27:26 -05:00
|
|
|
if (method == "static" or method == "manual") and len(sys.argv) < 5:
|
2020-06-09 16:28:32 -04:00
|
|
|
print(
|
|
|
|
|
"Usage: %s %s static address prefix [gateway]" % (sys.argv[0], sys.argv[1])
|
|
|
|
|
)
|
2014-09-05 13:48:56 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
uuid = sys.argv[1]
|
|
|
|
|
|
|
|
|
|
# Convert method to NM method
|
|
|
|
|
if method == "static":
|
|
|
|
|
method = "manual"
|
|
|
|
|
|
|
|
|
|
main_loop = GLib.MainLoop()
|
|
|
|
|
|
2014-11-13 14:27:26 -05:00
|
|
|
# create Client object
|
|
|
|
|
client = NM.Client.new(None)
|
2014-06-10 15:52:34 -05:00
|
|
|
|
2024-07-19 15:16:54 +02:00
|
|
|
try:
|
|
|
|
|
conn = next(c for c in client.get_connections() if c.get_uuid() == uuid)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
sys.exit("not found connection with uuid=%s" % uuid)
|
2014-06-10 15:52:34 -05:00
|
|
|
|
2024-07-19 15:16:54 +02:00
|
|
|
# add IPv4 setting if it doesn't yet exist
|
|
|
|
|
s_ip4 = conn.get_setting_ip4_config()
|
|
|
|
|
if not s_ip4:
|
|
|
|
|
s_ip4 = NM.SettingIP4Config.new()
|
|
|
|
|
conn.add_setting(s_ip4)
|
2014-06-10 15:52:34 -05:00
|
|
|
|
2024-07-19 15:16:54 +02:00
|
|
|
# set the method and change properties
|
|
|
|
|
s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, method)
|
|
|
|
|
if method == "auto":
|
|
|
|
|
# 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.IPAddress.new(socket.AF_INET, sys.argv[3], int(sys.argv[4]))
|
|
|
|
|
s_ip4.add_address(addr)
|
|
|
|
|
if len(sys.argv) == 6:
|
|
|
|
|
s_ip4.props.gateway = sys.argv[5]
|
2014-11-13 14:27:26 -05:00
|
|
|
|
2024-07-19 15:16:54 +02:00
|
|
|
try:
|
|
|
|
|
conn.commit_changes(True, None)
|
|
|
|
|
print("The connection profile has been updated.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
sys.stderr.write("Error: %s\n" % e)
|