0

I'm trying to use the socket function gethostbyaddr in python. It's working perfectly on Linux but not on OSX

It's working on distant address like 8.8.8.8

>>> socket.gethostbyaddr('8.8.8.8')
('google-public-dns-a.google.com', ['8.8.8.8.in-addr.arpa'], ['8.8.8.8'])

It's also working on lookback 127.0.0.1 :

>>> socket.gethostbyaddr('127.0.0.1')
('localhost', ['1.0.0.127.in-addr.arpa'], ['127.0.0.1'])

But it isn't on a local address 10.102.188.21 :

>>> socket.gethostbyaddr('10.102.188.21')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.herror: [Errno 1] Unknown host

On a Linux :

>>> print(socket.gethostbyaddr('10.102.188.21'))
('Pierre.local', [], ['10.102.188.21'])

Query with dig :

dig -x 10.102.188.21

; <<>> DiG 9.8.3-P1 <<>> -x 10.102.188.21
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 21064
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;21.188.102.10.in-addr.arpa.    IN  PTR

It isn't a proper PTR entry but it should be gotten by the gethostbyaddr()

  • It's not clear from your question whether you would expect that last example to work. Is there an DNS name associated with 10.102.188.21? Are other tools (e.g., `host` or `nslookup`) able to reverse the address correctly? – larsks Jun 08 '15 at 17:24
  • when I query the ip with dig -x I got a reverse dns name associated with the IP (I updated the question) – Gecko Splinter Jun 09 '15 at 07:27
  • Your dig output means the reverse lookup failed. There is no answer section, only the question section which shows what was queried. There is no DNS entry for that host. – mata Jun 09 '15 at 08:27
  • Yes I agree, but why on a Linux system I get a hostname correctly (I update the question) – Gecko Splinter Jun 09 '15 at 08:36
  • Are both systems using the same nameserver? – mata Jun 09 '15 at 09:01
  • Yes they are using the same nameserver. On linux I get the hostname of the machine, it's not register in the DNS, but on OSX I don't. I will test it on local switch just to be sure. – Gecko Splinter Jun 09 '15 at 09:32
  • Probably on linux the hostname is resolved using mdns (chech your /etc/nsswitch.conf) and on osx it isn't... – mata Jun 09 '15 at 10:47
  • Yes it's seem so. Test on a local switch : Computers on Linux can get the hostname of all the computers on the network but on OSX I get an unknown host error. – Gecko Splinter Jun 09 '15 at 11:06

1 Answers1

0

On linux in the /etc/nsswitch.conf there is a mdns option that resolve the hostname. That's why Linux can resolve the hostname on local network

OS X is supposed to resolve it with mDNS also, as it's part of zeroconf

Thanks mata for the help and larsks for zeroconf

  • OS X absolutely supports multicast DNS (this is a core part of "Zeroconf", the system by which your Mac can automatically discover other devices on the local network). I suspect something else is going on. – larsks Jun 09 '15 at 12:44
  • Yes indeed OS X support it. thanks for the information. – Gecko Splinter Jun 09 '15 at 20:37