File: vpn-import.py

package info (click to toggle)
network-manager 1.52.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 71,048 kB
  • sloc: ansic: 480,022; python: 11,394; xml: 8,504; sh: 5,535; perl: 596; cpp: 178; javascript: 130; ruby: 107; makefile: 57; lisp: 22
file content (65 lines) | stat: -rwxr-xr-x 1,563 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright (C) 2014 Red Hat, Inc.
#

#
# This example imports a VPN connection, by loading the glib based
# VPN plugin.

import gi

gi.require_version("NM", "1.0")
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:
    print('None of the VPN plugins was able to import "%s"' % (filename))
    sys.exit(1)

connection.normalize()

print(
    'connection imported from "%s" using plugin "%s" ("%s", %s)'
    % (filename, vpn_info.get_filename(), connection.get_id(), connection.get_uuid())
)

client = NM.Client.new(None)

main_loop = GLib.MainLoop()


def added_cb(client, result, data):
    try:
        client.add_connection_finish(result)
        print("The connection profile has been successfully added to NetworkManager.")
    except Exception as e:
        print("ERROR: failed to add connection: %s\n" % e)
    main_loop.quit()


client.add_connection_async(connection, True, None, added_cb, None)

main_loop.run()