try to work around the broken gethostbyaddr more thoroughly
[tests.git] / system / gethostbyaddr.py
1 ##########
2 # on the virsh containers, DNS resolution using gethostbyaddr
3 # won't work fine, for the hosts under .pl.sophia.inria.fr
4 # although these IPs can be reversed from virtually everywhere else
5 #
6 # this has started with something around fedora35 so I am suspecting python-3.10
7 #
8 # in any case, here's a quick and dirty workaround, as I have bumped my head
9 # against the screen for two good hours and not found any single clue
10 # about how to deal with this properly
11
12 import socket
13 import subprocess
14
15 def workaround_gethostbyaddr(ip):
16     try:
17         return socket.gethostbyaddr(ip)[0]
18     except:
19         # if that fails, try again using the `host` command
20         # which - surprisingly enough - works quite fine
21         # inside the containers
22         command = f"host {ip} 8.8.8.8"
23         completed = subprocess.run(command, shell=True, capture_output=True)
24         pieces = completed.stdout.decode().split("domain name pointer ")
25         if len(pieces) == 2:
26             return pieces[1].replace(".\n", "")
27         else:
28             return None