【问题标题】:What would cause pysmb to fail when OSX Finder “Connect to Server…” succeeds?当 OSX Finder “连接到服务器...”成功时,什么会导致 pysmb 失败?
【发布时间】:2015-04-30 19:16:44
【问题描述】:

(最初是在 StackOverflow 上问的,但我认为这里有更合适的专家):

我正在尝试使用 python 脚本(在 OSX 10.10 上运行)从远程 samba 共享(在 Windows 服务器上)传输文件。我可以使用Finder的Go->“连接到服务器...”对话框安装共享,但是当我尝试在python(v 2.7.6)中使用与pysmb模块相同的凭据时,我得到“连接被拒绝。 ":

>>> from smb.SMBConnection import SMBConnection
>>> conn =SMBConnection('myuser', 'mypassword','me','remote-server-netbios-name')
>>> assert conn.connect('remoteserver.mycompany.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/smb/SMBConnection.py", line 103, in connect
    self.sock.connect(( ip, port ))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused

同样,如果我尝试使用 NetBIOS 包来获取远程服务器的名称(以确认我得到了正确的名称),它只会超时:

>>> from nmb.NetBIOS import NetBIOS
>>> 
>>> def getBIOSName(remote_smb_ip, timeout=30):
...     try:
...         bios = NetBIOS()
...         srv_name = bios.queryIPForName(remote_smb_ip, timeout=timeout)
...     except:
...         print >> sys.stderr, "Looking up timeout, check remote_smb_ip again!!"
...     finally:
...         bios.close()
...         return srv_name
... 
>>> getBIOSName('remoteserver.mycompany.com')

相同的代码可以很好地从我家中的 ubuntu 服务器上的 samba 共享中获取文件。我怀疑这可能是服务器本身的一些权限或防火墙问题。关于需要打开哪些端口/权限才能完成这项工作的任何想法?

编辑: 根据下面 boardrider 的建议,我通过指定端口 445 尝试了连接功能。但是,这会产生“对等方重置连接”错误:

>>> assert conn.connect('remoteserver.mycompany.com', 445)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/smb/SMBConnection.py", line 112, in connect
    self._pollForNetBIOSPacket(timeout)
  File "/Library/Python/2.7/site-packages/smb/SMBConnection.py", line 511, in _pollForNetBIOSPacket
    d = self.sock.recv(read_len)
socket.error: [Errno 54] Connection reset by peer

【问题讨论】:

  • 文档给出了conn.connect(server_ip, 139),所以可能你在脚本中使用的端口不正确。另外 自 Windows 2000 起,SMB 默认运行一个薄层,类似于 NBT 的会话服务的会话消息包,在 TCP 之上,使用 TCP 端口 445 而不是 TCP 端口 139——这一特性被称为“直接主机 SMB”。
  • 感谢您的提示。我最初明确指定了 139,但没有任何区别,所以我尝试使用默认值。但是,两者都会产生上面的“连接被拒绝”结果。当我用 445 尝试它时,我得到了不同的结果。我将在上面更新我的问题,因为它比将结果放在此评论中更清晰。

标签: firewall samba python netbios


【解决方案1】:

这对我有用: 1.用户名没有域部分 2. is_direct_tcp=真 3. 连接445端口

conn = SMBConnection('user', 'password', socket.gethostname(), 'remote_server_name', 'domain_name', is_direct_tcp=True)
assert conn.connect('server_ip', 445)

SMB.SMBConnection INFO 身份验证(在 SMB2 上)成功!

【讨论】:

  • 为什么是端口 445?我能够连接到 Synology NAS,但它在端口 5000 上运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多