X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sioc.py;h=1213dd691d761b9499d01816dbfa2e91c5c86668;hb=refs%2Fheads%2Fmaster;hp=d596f771396f0a7330c60d94778db2a4e30e870f;hpb=0a370cfb64046b2e70343191f4b74372367dc861;p=pyplnet.git diff --git a/sioc.py b/sioc.py index d596f77..1213dd6 100644 --- a/sioc.py +++ b/sioc.py @@ -1,4 +1,3 @@ -# $Id$ # vim:set ts=4 sw=4 expandtab: # (c) Copyright 2008 The Trustees of Princeton University @@ -21,6 +20,9 @@ def _format_ip(nip): (ip & 0x000000ff)) def gifaddr(interface): + # for python3 + if isinstance(interface, str): + interface = interface.encode() s = None try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) @@ -35,18 +37,24 @@ def gifaddr(interface): def gifconf(): ret = {} - ip = subprocess.Popen(["/sbin/ip", "-4", "addr", "ls"], + ip = subprocess.Popen(["/sbin/ip", "-4", "addr", "show"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, close_fds=True) + stderr=subprocess.PIPE, close_fds=True, + universal_newlines=True) (stdout, stderr) = ip.communicate() - ip.wait() + # no wait is needed when using communicate for line in stdout.split("\n"): - if line.startswith(" inet "): - fields = line.split(" ") - ret[fields[-1]] = fields[5].split("/")[0] + fields = [ field.strip() for field in line.split() ] + if fields and fields[0] == "inet": + # fields[-1] is the last column in fields, which has the interface name + # fields[1] has the IP address / netmask width + ret[fields[-1]] = fields[1].split("/")[0] return ret def gifhwaddr(interface): + # for python3 + if isinstance(interface, str): + interface = interface.encode() s = None try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)