0

Python's socket.gethostbyaddr() return empty aliaslist on linux

I've tried with Python2 and Python3 on both Windows and Linux. It works in windows but not on Linux (both Ubuntu and RedHat) using the same IP to resolve of course. I also did a wireshark capture on both system and we clearly see the DNS answer containing all aliases.

import socket
print(socket.gethostbyaddr('192.168.11.1'))

On windows I get an answer like so which is OK:

('test123.com', ['alias1.test123.com', 'alias2.test123.com'], ['192.168.11.1'])

On linux I get :

('alias1.test123.com', [], ['192.168.11.1'])

Anybody knows the fix?

Tobias Wilfert
  • 919
  • 3
  • 14
  • 26
  • aliaslist is a (possibly empty) list of alternative host names for the same address –  Apr 03 '19 at 15:59
  • There's no "fix" because your examples don't work consistently. On my Windows machine I don't get alias entries either. It looks exactly the same as on Linux or on the WSL Ubuntu. This depends on lot on how the A and CNAME records are arranged, and what exactly your query is. – Zan Lynx Apr 03 '19 at 16:47
  • The query is a PTR request for 1.11.168.192.in-addr.arpa – Guillaume Vertefeuille Apr 03 '19 at 20:04
  • @GuillaumeVertefeuille That's a reverse DNS request for 192.168.11.1 which is a private IP. The only name server that can answer that is your own. – Zan Lynx Apr 08 '19 at 16:07
  • You may have a PTR record pointing to a CNAME record. Which can cause weird behavior like this. Because PTR records are supposed to point at names with A or AAAA records aka canonical names. But because it was useful and most DNS servers supported it some people point at a CNAME aka alias. But the way those resolve is not standard. One might return the CNAME and its targets while another resolver might just collapse the chain and return the final answer. – Zan Lynx Apr 08 '19 at 16:11

0 Answers0