summaryrefslogtreecommitdiffstats
path: root/src/utils/net.py
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2022-04-18 10:59:35 +0200
committerJose M. Guisado <jguisado@soleta.eu>2022-04-21 09:47:04 +0200
commit902e0195055099c9ab59567407e5e5a90e80e7e7 (patch)
treee90fea613fcecffa694b3448311b0ee62f98af85 /src/utils/net.py
parent79d3062f8166f17a0b8566184c4c3a8b0c9073c6 (diff)
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.
Diffstat (limited to 'src/utils/net.py')
-rw-r--r--src/utils/net.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/utils/net.py b/src/utils/net.py
new file mode 100644
index 0000000..01c6f04
--- /dev/null
+++ b/src/utils/net.py
@@ -0,0 +1,53 @@
+#
+# Copyright (C) 2022 Soleta Networks <info@soleta.eu>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the
+# Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+import array
+import fcntl
+import socket
+import struct
+
+def ethtool(interface):
+ try:
+ ETHTOOL_GSET = 0x00000001
+ SIOCETHTOOL = 0x8946
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sockfd = sock.fileno()
+ ecmd = array.array(
+ "B", struct.pack("I39s", ETHTOOL_GSET, b"\x00" * 39)
+ )
+ interface = interface.encode("utf-8")
+ ifreq = struct.pack("16sP", interface, ecmd.buffer_info()[0])
+ fcntl.ioctl(sockfd, SIOCETHTOOL, ifreq)
+ res = ecmd.tobytes()
+ speed = struct.unpack("12xH29x", res)[0]
+ except IOError:
+ speed = 0
+ finally:
+ sock.close()
+ return speed
+
+def getifaddr(device):
+ """
+ """
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ return socket.inet_ntoa(fcntl.ioctl(
+ s.fileno(),
+ 0x8915, # SIOCGIFADDR
+ struct.pack('256s', bytes(device[:15], 'utf-8'))
+ )[20:24])
+
+def getifhwaddr(device):
+ """
+ """
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ hwaddr = fcntl.ioctl(
+ s.fileno(),
+ 0x8927, # SIOCGIFHWADDR
+ struct.pack('256s', bytes(device[:15], 'utf-8'))
+ )[18:24]
+ return "%02x:%02x:%02x:%02x:%02x:%02x" % struct.unpack("BBBBBB", hwaddr)