#!/usr/bin/env python # # vim: ft=python ts=4 sts=4 sw=4 et ai # -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- # # 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 (C) 2014 Red Hat, Inc. # import sys, socket, struct from gi.repository import GLib, GObject, NetworkManager, NMClient def update_addresses(cfg, device): addresses = cfg.get_addresses() if not len(addresses): return print "(%s) IPv4 addresses" % device.get_iface() for iaddr in addresses: addr = iaddr.get_address() prefix = iaddr.get_prefix() gateway = iaddr.get_gateway() addr_struct = struct.pack("=I", addr) gateway_struct = struct.pack("=I", gateway) print(" %s/%d %s") % (socket.inet_ntop(socket.AF_INET, addr_struct), prefix, socket.inet_ntop(socket.AF_INET, gateway_struct)) def notify_addresses(cfg, property, device): update_addresses(cfg, device) def notify_ip4_config(device, property): print "(%s) notify IP4Config (state %d)" % (device.get_iface(), device.get_state()) old_cfg = device.current_ip4_config # disconnect old signal handler if old_cfg: old_cfg.disconnect_by_func(notify_addresses) new_cfg = device.get_ip4_config() device.current_ip4_config = new_cfg if new_cfg: new_cfg.connect('notify::addresses', notify_addresses, device) update_addresses(new_cfg, device) if __name__ == "__main__": c = NMClient.Client.new() for device in c.get_devices(): print "Connecting to %s" % device.get_iface() device.current_ip4_config = None device.connect('notify::ip4-config', notify_ip4_config) loop = GLib.MainLoop() loop.run()