2017-11-06 18:32:00 +01:00
|
|
|
#!/usr/bin/env python
|
2020-12-23 22:21:36 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2017-11-06 18:32:00 +01:00
|
|
|
#
|
2019-10-01 09:20:35 +02:00
|
|
|
# Copyright (C) 2014 Red Hat, Inc.
|
2017-11-06 18:32:00 +01:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# This example imports a VPN connection, by loading the glib based
|
|
|
|
|
# VPN plugin.
|
|
|
|
|
|
|
|
|
|
import gi
|
2020-06-09 16:28:32 -04:00
|
|
|
|
|
|
|
|
gi.require_version("NM", "1.0")
|
2017-11-06 18:32:00 +01:00
|
|
|
from gi.repository import GLib, NM
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
|
print("Expects one argument: the filename")
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
filename = sys.argv[1]
|
|
|
|
|
|
|
|
|
|
connection = None
|
|
|
|
|
for vpn_info in NM.VpnPluginInfo.list_load():
|
|
|
|
|
print("TRY: plugin %s" % (vpn_info.get_filename()))
|
|
|
|
|
try:
|
|
|
|
|
vpn_plugin = vpn_info.load_editor_plugin()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("SKIP: cannot load plugin: %s" % (e))
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
connection = vpn_plugin.import_(filename)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("SKIP: failure to import %s" % (e))
|
|
|
|
|
continue
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if connection is None:
|
2020-06-09 16:28:32 -04:00
|
|
|
print('None of the VPN plugins was able to import "%s"' % (filename))
|
2017-11-06 18:32:00 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
connection.normalize()
|
|
|
|
|
|
2020-06-09 16:28:32 -04:00
|
|
|
print(
|
|
|
|
|
'connection imported from "%s" using plugin "%s" ("%s", %s)'
|
|
|
|
|
% (filename, vpn_info.get_filename(), connection.get_id(), connection.get_uuid())
|
|
|
|
|
)
|
2017-11-06 18:32:00 +01:00
|
|
|
|
|
|
|
|
client = NM.Client.new(None)
|
|
|
|
|
|
|
|
|
|
main_loop = GLib.MainLoop()
|
|
|
|
|
|
2020-06-09 16:28:32 -04:00
|
|
|
|
2017-11-06 18:32:00 +01:00
|
|
|
def added_cb(client, result, data):
|
|
|
|
|
try:
|
|
|
|
|
client.add_connection_finish(result)
|
2018-09-14 23:49:20 -04:00
|
|
|
print("The connection profile has been successfully added to NetworkManager.")
|
2021-03-16 00:48:35 +00:00
|
|
|
except Exception as e:
|
2017-11-06 18:32:00 +01:00
|
|
|
print("ERROR: failed to add connection: %s\n" % e)
|
|
|
|
|
main_loop.quit()
|
|
|
|
|
|
2020-06-09 16:28:32 -04:00
|
|
|
|
2017-11-06 18:32:00 +01:00
|
|
|
client.add_connection_async(connection, True, None, added_cb, None)
|
|
|
|
|
|
|
|
|
|
main_loop.run()
|