【发布时间】:2014-09-15 10:40:52
【问题描述】:
我在使用 socket.gethostbyaddr 返回 Windows 机器的 netbios 名称时遇到问题。在 Windows 上,这会返回
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'
>>>
【问题讨论】:
-
您提供的 ping 显示正向分辨率。你能告诉我们 socket.gethostbyname('ZUNIT1') 揭示了什么吗?
-
安装 samba/libnss-winbind,并设置各种配置,如 superuser.com/questions/670714/… 所示(特别是 /etc/nsswitch.conf 配置并确保 smb.conf 配置为在您的域上)
-
@User590028 gethostbyname 工作正常(请参阅有问题的更新)。
-
@nos 我已经安装了 samba 并配置了 /etc/nsswitch.conf 和 smb.conf。在 ubuntu-12.04 中 libnss-winbind 包不存在(但在更高版本中存在)。我仍然有同样的问题。所以也许我需要升级机器并安装 libnss-winbind。
-
@nos 我已经安装了 libnss-winbind 包,但我仍然没有从 Linux 上的 gethostbyaddr 调用中获得 netbios 名称。 nmblookup -A 10.10.1.22 给了我 Windows 机器的 netbios 名称。
标签: python linux python-3.x ubuntu-12.04 netbios