#!/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, NetworkManager, NMClient def notify_ip4_config(self, property): print "notify: %s ::::: state %d" % (property, self.get_state()) cfg = self.get_ip4_config() if cfg == None: return; for iaddr in cfg.get_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)) if __name__ == "__main__": c = NMClient.Client.new() for d in c.get_devices(): print "Connecting to %s" % d.get_iface() d.connect('notify::ip4-config', notify_ip4_config) loop = GLib.MainLoop() loop.run()