2 # vim:set ts=4 sw=4 expandtab:
3 # (c) Copyright 2008 The Trustees of Princeton University
11 SIOCGIFADDR_struct = "16xH2xI8x"
12 SIOCGIFHWADDR = 0x8927
13 SIOCGIFHWADDR_struct = "16x2x6B8x"
16 ip = socket.ntohl(nip)
17 return "%d.%d.%d.%d" % ((ip & 0xff000000) >> 24,
18 (ip & 0x00ff0000) >> 16,
19 (ip & 0x0000ff00) >> 8,
22 def gifaddr(interface):
25 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
26 ifreq = fcntl.ioctl(s.fileno(), SIOCGIFADDR, struct.pack("16s16x", interface))
27 (family, ip) = struct.unpack(SIOCGIFADDR_struct, ifreq)
28 if family == socket.AF_INET:
37 interfaces = os.listdir("/sys/class/net")
43 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
44 for interface in interfaces:
46 ifreq = fcntl.ioctl(s.fileno(), SIOCGIFADDR,
47 struct.pack("16sH14x", interface, socket.AF_INET))
48 (family, ip) = struct.unpack(SIOCGIFADDR_struct, ifreq)
49 if family == socket.AF_INET:
50 ret[interface] = _format_ip(ip)
54 ret[interface] = "0.0.0.0"
60 def gifhwaddr(interface):
63 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
64 ifreq = fcntl.ioctl(s.fileno(), SIOCGIFHWADDR, struct.pack("16s16x", interface))
65 mac = struct.unpack(SIOCGIFHWADDR_struct, ifreq)
66 return "%02x:%02x:%02x:%02x:%02x:%02x" % mac