diff options
Diffstat (limited to 'examples')
| -rwxr-xr-x | examples/js/get_ips.js | 184 | ||||
| -rw-r--r-- | examples/python/gi/checkpoint.py | 130 | ||||
| -rwxr-xr-x | examples/python/gi/vpn-import.py | 74 |
3 files changed, 388 insertions, 0 deletions
diff --git a/examples/js/get_ips.js b/examples/js/get_ips.js new file mode 100755 index 0000000000..490ecb2a8a --- /dev/null +++ b/examples/js/get_ips.js @@ -0,0 +1,184 @@ +#!/usr/bin/env gjs + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright 2014,2017 Red Hat, Inc. + */ + +const System = imports.system; +const NM = imports.gi.NM; +const GLib = imports.gi.GLib; + +/* + * This example shows how to get addresses, routes and DNS information + * from NMIP4Config and NMIP6Config (got out of NMDevice) + */ + +function show_addresses (dev, family) +{ + let ip_cfg; + if (family == GLib.SYSDEF_AF_INET) + ip_cfg = dev.get_ip4_config (); + else + ip_cfg = dev.get_ip6_config (); + + if (ip_cfg == null) { + print ("None"); + return; + } + + let nm_addresses = ip_cfg.get_addresses (); + if (nm_addresses.length == 0) { + print ("None"); + return; + } + + for (let nm_address of nm_addresses) { + let addr = nm_address.get_address (); + let prefix = nm_address.get_prefix (); + + print (addr + "/" + prefix); + } +} + +function show_gateway (dev, family) +{ + let ip_cfg; + if ((family == GLib.SYSDEF_AF_INET)) + ip_cfg = dev.get_ip4_config (); + else + ip_cfg = dev.get_ip6_config (); + + let gw; + if (ip_cfg == null) + gw = "None" + else { + gw = ip_cfg.get_gateway (); + if (gw == '') + gw = "None" + } + + print (gw); +} + +function show_routes (dev, family) +{ + let ip_cfg; + if ((family == GLib.SYSDEF_AF_INET)) + ip_cfg = dev.get_ip4_config (); + else + ip_cfg = dev.get_ip6_config (); + + if (ip_cfg == null) { + print ("None"); + return; + } + + let nm_routes = ip_cfg.get_routes (); + if (nm_routes.length == 0) { + print ("None"); + return; + } + + for (let nm_route of nm_routes) { + let dest = nm_route.get_dest (); + let prefix = nm_route.get_prefix (); + let next_hop = nm_route.get_next_hop (); + let metric = nm_route.get_metric (); + + print (dest + "/" + prefix + " " + next_hop + " " + metric); + } +} + +function show_dns (dev, family) +{ + let ip_cfg; + if ((family == GLib.SYSDEF_AF_INET)) + ip_cfg = dev.get_ip4_config (); + else + ip_cfg = dev.get_ip6_config (); + + if (ip_cfg == null) { + print ("None"); + return; + } + + print ("Nameservers: " + ip_cfg.get_nameservers ()); + print ("Domains: " + ip_cfg.get_domains ()); + print ("Searches: " + ip_cfg.get_searches ()); + if ((family == GLib.SYSDEF_AF_INET)) + print ("WINS: " + ip_cfg.get_wins_servers ()); +} + +if (ARGV.length != 1) { + print ("Usage: get_ips.js <interface>"); + System.exit (1); +} + + +let dev_iface = ARGV[0]; +let c = NM.Client.new (null); + +let dev = c.get_device_by_iface (dev_iface); + +if (dev == null) { + print ("Device '%s' not found " + dev_iface); + System.exit (1); +} + +print ("Device: " + dev_iface + " - " + dev.get_type_description ()); +print ("---------------------------------------"); +print (); + +print ("IPv4 addresses:"); +print ("---------------"); +show_addresses (dev, GLib.SYSDEF_AF_INET); +print (); + +print ("IPv4 gateway:"); +print ("-------------"); +show_gateway (dev, GLib.SYSDEF_AF_INET); +print (); + +print ("IPv4 routes:"); +print ("------------"); +show_routes (dev, GLib.SYSDEF_AF_INET); +print (); + +print ("IPv6 addresses:"); +print ("---------------"); +show_addresses (dev, GLib.SYSDEF_AF_INET6); +print (); + +print ("IPv6 gateway:"); +print ("-------------"); +show_gateway (dev, GLib.SYSDEF_AF_INET6); +print (); + +print ("IPv6 routes:"); +print ("------------"); +show_routes (dev, GLib.SYSDEF_AF_INET6); +print (); + +print ("IPv4 DNS:"); +print ("------------"); +show_dns (dev, GLib.SYSDEF_AF_INET); +print (); + +print ("IPv6 DNS:"); +print ("------------"); +show_dns (dev, GLib.SYSDEF_AF_INET6); +print (); diff --git a/examples/python/gi/checkpoint.py b/examples/python/gi/checkpoint.py new file mode 100644 index 0000000000..513e6c3a0a --- /dev/null +++ b/examples/python/gi/checkpoint.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python +# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +# vim: ft=python ts=4 sts=4 sw=4 et ai + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# Copyright 2017 Red Hat, Inc. + +import sys + +import gi +gi.require_version('NM', '1.0') +from gi.repository import GLib, NM + +def usage(): + print "Usage: %s [COMMAND [ARG]...]" % sys.argv[0] + print "" + print " COMMANDS: show" + print " create TIMEOUT [DEV]..." + print " destroy PATH|NUMBER" + print " rollback PATH|NUMBER" + print + sys.exit(1) + +def create_cb(client, result, data): + try: + checkpoint = client.checkpoint_create_finish(result) + print("%s" % checkpoint.get_path()) + except Exception, e: + sys.stderr.write("Failed: %s\n" % e.message) + main_loop.quit() + +def do_create(client): + if len(sys.argv) < 3: + sys.exit("Failed: %s\n" % e.message) + + timeout = int(sys.argv[2]) + devices = [] + for arg in sys.argv[3:]: + d = client.get_device_by_iface(arg) + if d is None: + sys.exit("Unknown device %s" % arg) + devices.append(d) + + client.checkpoint_create_async(devices, timeout, 0, None, create_cb, None) + +def destroy_cb(client, result, data): + try: + if client.checkpoint_destroy_finish(result) == True: + print "Success" + except Exception, e: + sys.stderr.write("Failed: %s\n" % e.message) + main_loop.quit() + +def find_checkpoint(client, arg): + try: + num = int(arg) + path = "/org/freedesktop/NetworkManager/Checkpoint/%u" % num + except Exception, e: + path = arg + + for c in client.get_checkpoints(): + if c.get_path() == path: + return c + return None + +def do_destroy(client): + if len(sys.argv) < 3: + sys.exit("Missing checkpoint path") + + checkpoint = find_checkpoint(client, sys.argv[2]) + if checkpoint is None: + sys.exit("Uknown checkpoint %s" % sys.argv[2]) + + client.checkpoint_destroy_async(checkpoint, None, destroy_cb, None) + +def rollback_cb(client, result, data): + try: + res = client.checkpoint_rollback_finish(result) + for path in res: + d = client.get_device_by_path(path) + if d is None: + iface = path + else: + iface = d.get_iface() + print "%s => %s" % (iface, "OK" if res[path] == 0 else "ERROR") + except Exception, e: + sys.stderr.write("Failed: %s\n" % e.message) + main_loop.quit() + +def do_rollback(client): + if len(sys.argv) < 3: + sys.exit("Missing checkpoint path") + + checkpoint = find_checkpoint(client, sys.argv[2]) + if checkpoint is None: + sys.exit("Uknown checkpoint %s" % sys.argv[2]) + + client.checkpoint_rollback_async(checkpoint, None, rollback_cb, None) + +def do_show(client): + for c in client.get_checkpoints(): + print "%s:" % c.get_path() + print " created: %u" % c.get_created() + print " timeout: %u seconds" % c.get_rollback_timeout() + print " devices:", ' '.join(sorted(map(lambda x: x.get_iface(), c.get_devices()))) + +if __name__ == '__main__': + nm_client = NM.Client.new(None) + main_loop = GLib.MainLoop() + + if len(sys.argv) < 2 or sys.argv[1] == 'show': + do_show(nm_client) + sys.exit(0) + elif sys.argv[1] == 'create': + do_create(nm_client) + elif sys.argv[1] == 'destroy': + do_destroy(nm_client) + elif sys.argv[1] == 'rollback': + do_rollback(nm_client) + else: + usage() + + main_loop.run() diff --git a/examples/python/gi/vpn-import.py b/examples/python/gi/vpn-import.py new file mode 100755 index 0000000000..6210908aa5 --- /dev/null +++ b/examples/python/gi/vpn-import.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- +# vim: ft=python ts=4 sts=4 sw=4 et ai +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Copyright 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 succesfully added to NetworkManager.") + except Exception, 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() |
