|
4 | 4 | """
|
5 | 5 | import socket
|
6 | 6 | import os
|
7 |
| -import struct |
8 |
| -import array |
9 |
| -import fcntl |
10 |
| -import platform |
11 | 7 | from urllib import parse
|
12 | 8 |
|
| 9 | +import psutil |
| 10 | + |
13 | 11 | from DIRAC.Core.Utilities.ReturnValues import S_OK, S_ERROR
|
14 | 12 |
|
15 | 13 |
|
16 | 14 | def discoverInterfaces():
|
17 |
| - max_possible = 128 |
18 |
| - maxBytes = max_possible * 32 |
19 |
| - mySocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
20 |
| - names = array.array("B", b"\0" * maxBytes) |
21 |
| - # 0x8912 SICGIFCONF |
22 |
| - fcntlOut = fcntl.ioctl(mySocket.fileno(), 0x8912, struct.pack("iL", maxBytes, names.buffer_info()[0])) |
23 |
| - outbytes = struct.unpack("iL", fcntlOut)[0] |
24 |
| - namestr = names.tobytes() |
25 |
| - |
26 |
| - if "32" in platform.architecture()[0]: |
27 |
| - step = 32 |
28 |
| - offset = 32 |
29 |
| - else: |
30 |
| - step = 40 |
31 |
| - offset = 16 |
32 |
| - |
33 |
| - ifaces = {} |
34 |
| - for i in range(0, outbytes, step): |
35 |
| - name = namestr[i : i + offset].split(b"\0", 1)[0].decode() |
36 |
| - ip = namestr[i + 20 : i + 24] |
37 |
| - ifaces[name] = {"ip": socket.inet_ntoa(ip), "mac": getMACFromInterface(name)} |
38 |
| - return ifaces |
39 |
| - |
40 |
| - |
41 |
| -def getMACFromInterface(ifname): |
42 |
| - mySocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
43 |
| - # bytearray is only needed here for Python 2 |
44 |
| - info = bytearray(fcntl.ioctl(mySocket.fileno(), 0x8927, struct.pack("256s", ifname[:15].encode()))) |
45 |
| - return "".join(["%02x:" % char for char in info[18:24]])[:-1] |
| 15 | + interfaces = {k: {a.family: a.address for a in v} for k, v in psutil.net_if_addrs().items()} |
| 16 | + return { |
| 17 | + k: {"ip": v.get(socket.AF_INET, "0.0.0.0"), "mac": v.get(psutil.AF_LINK, "00:00:00:00:00:00")} |
| 18 | + for k, v in interfaces.items() |
| 19 | + } |
46 | 20 |
|
47 | 21 |
|
48 | 22 | def getFQDN():
|
|
0 commit comments