1 | # |
---|
2 | # Copyright (C) 2022 Soleta Networks <info@soleta.eu> |
---|
3 | # |
---|
4 | # This program is free software: you can redistribute it and/or modify it under |
---|
5 | # the terms of the GNU Affero General Public License as published by the |
---|
6 | # Free Software Foundation; either version 3 of the License, or |
---|
7 | # (at your option) any later version. |
---|
8 | |
---|
9 | import array |
---|
10 | import fcntl |
---|
11 | import socket |
---|
12 | import struct |
---|
13 | |
---|
14 | def ethtool(interface): |
---|
15 | try: |
---|
16 | ETHTOOL_GSET = 0x00000001 |
---|
17 | SIOCETHTOOL = 0x8946 |
---|
18 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
19 | sockfd = sock.fileno() |
---|
20 | ecmd = array.array( |
---|
21 | "B", struct.pack("I39s", ETHTOOL_GSET, b"\x00" * 39) |
---|
22 | ) |
---|
23 | interface = interface.encode("utf-8") |
---|
24 | ifreq = struct.pack("16sP", interface, ecmd.buffer_info()[0]) |
---|
25 | fcntl.ioctl(sockfd, SIOCETHTOOL, ifreq) |
---|
26 | res = ecmd.tobytes() |
---|
27 | speed = struct.unpack("12xH29x", res)[0] |
---|
28 | except IOError: |
---|
29 | speed = 0 |
---|
30 | finally: |
---|
31 | sock.close() |
---|
32 | return speed |
---|
33 | |
---|
34 | def getifaddr(device): |
---|
35 | """ |
---|
36 | """ |
---|
37 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
38 | return socket.inet_ntoa(fcntl.ioctl( |
---|
39 | s.fileno(), |
---|
40 | 0x8915, # SIOCGIFADDR |
---|
41 | struct.pack('256s', bytes(device[:15], 'utf-8')) |
---|
42 | )[20:24]) |
---|
43 | |
---|
44 | def getifhwaddr(device): |
---|
45 | """ |
---|
46 | """ |
---|
47 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
---|
48 | hwaddr = fcntl.ioctl( |
---|
49 | s.fileno(), |
---|
50 | 0x8927, # SIOCGIFHWADDR |
---|
51 | struct.pack('256s', bytes(device[:15], 'utf-8')) |
---|
52 | )[18:24] |
---|
53 | return "%02x:%02x:%02x:%02x:%02x:%02x" % struct.unpack("BBBBBB", hwaddr) |
---|