Steve Broberg
Steve Broberg

Reputation: 4394

What would cause pysmb to fail when OSX Finder “Connect to Server…” succeeds?

(originally asked on StackOverflow, but I think there are more appropriate experts here):

I'm trying to transfer a file from a remote samba share (on a windows server) in a python script (running on OSX 10.10). I am able to mount the share using Finder's Go->"Connect to Server..." dialog, but when I attempt to use the same credentials with the pysmb module in python (v 2.7.6), I get "Connection Refused.":

>>> 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

Similarly, if I try to use the NetBIOS package to get the remote server's name (to confirm I'm getting that correct), it just times out:

>>> 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')

The same code works fine to get files from a samba share on my ubuntu server at home. I suspect it may be some permissions or firewall issue on the server itself. Any ideas on what ports/permissions need to be opened to make this work?

EDIT: With boardrider's suggestion below, I tried the connect function by specifying port 445. However, that generates a "Connection reset by peer" error:

>>> 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

Upvotes: 3

Views: 3138

Answers (1)

user3657041
user3657041

Reputation: 855

This worked for me: 1. username does not have domain part 2. is_direct_tcp=True 3. connection to port 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 Authentication (on SMB2) successful!

Upvotes: 5

Related Questions