1

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'
>>>
Alex
  • 669
  • 15
  • 37
  • 1
    The ping you provided shows forward resolution. Can you show us what socket.gethostbyname('ZUNIT1') reveals? – user590028 Sep 15 '14 at 10:53
  • 2
    Install samba/libnss-winbind, and set up the various configuration like shown http://superuser.com/questions/670714/setup-netbios-client-in-ubuntu-13-10 (in particular the /etc/nsswitch.conf configuration and make sure smb.conf is configured to be on your domain) – nos Sep 15 '14 at 10:56
  • @User590028 gethostbyname works correctly (see update in question). – Alex Sep 15 '14 at 11:22
  • @nos I've installed samba and configured /etc/nsswitch.conf and smb.conf. In ubuntu-12.04 the libnss-winbind package doesn't exist (but it does in later versions). I still have the same problem. So perhaps I need to upgrade the machine and install libnss-winbind. – Alex Sep 15 '14 at 12:40
  • @nos I've installed the libnss-winbind package and I still don't get netbios names from gethostbyaddr calls on Linux. nmblookup -A 10.10.1.22 gives me the netbios name of the windows machine. – Alex Sep 17 '14 at 14:51
  • I think I've worked out what's going on, the workgroup of the machines I'm attempting to get the netbios name is different than the workgroup the rest of the machines use (including the samba configuration). gethostbyaddr works for machines on the workgroup specified in my samba configuration. – Alex Sep 17 '14 at 15:50

1 Answers1

0

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

Alex
  • 669
  • 15
  • 37