try to work around the broken gethostbyaddr more thoroughly
[tests.git] / system / gethostbyaddr.py
diff --git a/system/gethostbyaddr.py b/system/gethostbyaddr.py
new file mode 100644 (file)
index 0000000..199fa59
--- /dev/null
@@ -0,0 +1,28 @@
+##########
+# on the virsh containers, DNS resolution using gethostbyaddr
+# won't work fine, for the hosts under .pl.sophia.inria.fr
+# although these IPs can be reversed from virtually everywhere else
+#
+# this has started with something around fedora35 so I am suspecting python-3.10
+#
+# in any case, here's a quick and dirty workaround, as I have bumped my head
+# against the screen for two good hours and not found any single clue
+# about how to deal with this properly
+
+import socket
+import subprocess
+
+def workaround_gethostbyaddr(ip):
+    try:
+        return socket.gethostbyaddr(ip)[0]
+    except:
+        # if that fails, try again using the `host` command
+        # which - surprisingly enough - works quite fine
+        # inside the containers
+        command = f"host {ip} 8.8.8.8"
+        completed = subprocess.run(command, shell=True, capture_output=True)
+        pieces = completed.stdout.decode().split("domain name pointer ")
+        if len(pieces) == 2:
+            return pieces[1].replace(".\n", "")
+        else:
+            return None