33#
44# Update a property of a connection profile, looked up by connection id
55#
6+ # This version uses connection_manager.connection_profile().to_settings_dict()
7+ # to retrieve the connection profile from NetworkManager as a settings dict.
8+ #
9+ # It then updates it dynamically using the given arguments:
10+ # The default is to set ipv4.dns-search to ["domain1.com", "domain2.com"].
11+ #
12+ # The dynamically updated dict is then used to update connection profile of NM.
13+ #
614# The IPv4 settings of connections profiles are documented here:
715# https://networkmanager.dev/docs/api/latest/settings-ipv4.html
816#
917import asyncio
1018import sdbus
1119from functools import partial
20+ from sdbus_async .networkmanager import ConnectionProfile
1221from sdbus_async .networkmanager import NetworkManagerSettings
1322from sdbus_async .networkmanager import NetworkConnectionSettings
1423from pprint import pprint
1726
1827async def update_connection_async (args : Dict [str , Any ]) -> None :
1928 """Update the settings for [key][entry] of the 1st matching connection"""
29+
30+ # Get the connection path of the connection(s) with the recieved id
2031 fn = NetworkManagerSettings ().get_connections_by_id (args ["connection_id" ])
2132 connection_paths = await fn
22- settings_domain , setting = args ["connection_setting" ]
23- if connection_paths :
24- connection_settings = NetworkConnectionSettings (connection_paths [0 ])
25- properties = await connection_settings .get_settings ()
26- # For compatibility with old tools, NM adds and prefers them, delete:
27- properties ["ipv4" ].pop ("addresses" ) # -> Use ["ipv4"]["address-data"]
28- properties ["ipv4" ].pop ("routes" ) # ----> Use ["ipv4"]["route-data"]
33+ if not connection_paths :
34+ print (f"No connection { id } , create with add-wifi-psk-connection-async" )
35+ return
36+
37+ # Get the profile settings of the first connecttion with given id
38+ connection_manager = NetworkConnectionSettings (connection_paths [0 ])
39+ existing_connection_profile = await connection_manager .connection_profile ()
40+ settings = existing_connection_profile .to_settings_dict ()
41+
42+ # Update the given setting's property using the given value
43+ setting , property = args ["connection_setting" ]
44+ settings [setting ][property ] = args ["value" ]
45+
46+ # Get a new ConnectionProfile with the change incorporated
47+ new_connection_profile = ConnectionProfile .from_settings_dict (settings )
48+
49+ # Update the new ConnectionProfile in NetworkManager's configuration
50+ await connection_manager .update (new_connection_profile .to_dbus ())
2951
30- # Update the setting's value in the given configuration group:
31- properties [settings_domain ][setting ] = args ["value" ]
32- await connection_settings .update (properties )
52+ print (f'Updated { new_connection_profile .connection .uuid } .{ setting } :' )
53+ partial (pprint , sort_dicts = False )(settings )
3354
34- print (f'Updated { properties ["connection" ]["uuid" ]} .{ settings_domain } :' )
35- partial (pprint , sort_dicts = False )(properties [settings_domain ])
36- else :
37- print (f"No connection matching { id } " )
55+ # Restore the previous connection profile:
56+ await connection_manager .update (existing_connection_profile .to_dbus ())
3857
3958
4059if __name__ == "__main__" :
@@ -43,7 +62,6 @@ async def update_connection_async(args: Dict[str, Any]) -> None:
4362 # Set MyConnectionExample.ipv4.dns-search to "domain1.com,domain2.com":
4463 "connection_id" : "MyConnectionExample" ,
4564 "connection_setting" : ("ipv4" , "dns-search" ),
46- # "as" is the so-called DBus signature, it means "array of strings":
47- "value" : ("as" , ["domain1.com" , "domain2.com" ]),
65+ "value" : ["domain1.com" , "domain2.com" ],
4866 }
4967 asyncio .run (update_connection_async (args ))
0 commit comments