当OSX Finder“连接到服务器......”成功时,会导致pysmb失败的原因是什么?

时间:2015-04-30 19:16:44

标签: firewall samba python netbios

(最初在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

1 个答案:

答案 0 :(得分:1)

这对我有用: 1.用户名没有域名部分 2. is_direct_tcp = True 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上)成功!