pythonlinuxpython-3.xubuntu-12.04netbios

In Python on Linux, how do I get socket.gethostbyaddr to return the netbios name of a windows machine


I'm having issues using socket.gethostbyaddr to return netbios names of windows machines. On windows this returns

Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:19:30) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.    
>>> import socket
>>> socket.gethostbyaddr("10.10.1.22")
('ZUNIT1', [], ['10.10.1.22'])
>>>

On my linux machine I get

Python 3.2.3 (default, Feb 27 2014, 21:31:18)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyaddr("10.10.1.22")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.herror: [Errno 1] Unknown host
>>>

I've installed winbind on the linux machine and modified /etc/nsswitch.conf so I can use netbios names now to do things like ping the unit using the netbios name.

alexm@malv:~$ ping ZUNIT1
PING ZUNIT1 (10.10.1.22) 56(84) bytes of data.
64 bytes from 10.10.1.22: icmp_req=1 ttl=128 time=0.461 ms
64 bytes from 10.10.1.22: icmp_req=2 ttl=128 time=0.371 ms
^C
--- ZUNIT1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1006ms
rtt min/avg/max/mdev = 0.371/0.416/0.461/0.045 ms
alexm@malv:~$

Can anyone tell me what I need to do to get Python to return netbios names from the socket.gethostbyaddr method?

EDIT for @user590028, gethostbyname works as expected.

Python 3.2.3 (default, Feb 27 2014, 21:31:18) 
[GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostbyname("ZUNIT1")
'10.10.1.22'
>>>

Solution

  • The issue was the linux machine had a different workgroup to the machine that I was attempting to retrieve the name for.