我在使用socket.gethostbyaddr返回windows机器的netbios名称时遇到问题。在窗口上,返回
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'])
>>>
在我的linux机器上,我得到了
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
>>>
我已经在linux机器上安装了winbind,并修改了/etc/nsswitch.conf,这样我现在就可以使用netbios名称来执行一些操作,比如使用netbios名称ping单元。
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:~$
有人能告诉我我需要做什么才能让Python从socket.gethostbyaddr方法返回netbios名称吗?
编辑@user590028,gethostbyname按预期工作。
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'
>>>
问题是linux机器的工作组与我试图检索其名称的机器不同。