source: ogClient-Git/src/utils/net.py @ 81ee4b0

Last change on this file since 81ee4b0 was 902e019, checked in by Jose M. Guisado <jguisado@…>, 3 years ago

Add utils modules

  • disk.py

Disk discovery

  • fs.py

Uses psutil to fetch fs usage information

  • menu.py

ogBrowser menu generation

  • net.py: gets nic status information

IP address, MAC address and ethernet speed.

  • probe.py: probes mountpoints for operating systems

Uses hivexget command to try fetching Windows installation
information.
Looks for /etc/os-release for probing linux systems.

  • Property mode set to 100644
File size: 1.6 KB
Line 
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
9import array
10import fcntl
11import socket
12import struct
13
14def 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
34def 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
44def 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)
Note: See TracBrowser for help on using the repository browser.