3

(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
2
  • The documentation gives conn.connect(server_ip, 139), so maybe the port you use in the script is not correct. Also Since Windows 2000, SMB runs, by default, with a thin layer, similar to the Session Message packet of NBT's Session Service, on top of TCP, using TCP port 445 rather than TCP port 139—a feature known as "direct host SMB".
    – boardrider
    May 1, 2015 at 10:14
  • Thanks for the tip. I had originally specified 139 explicitly, but it made no difference, so I tried used the default. However, both produce the "Connection refused" result above. When I tried it with 445, I got a different result. I'll update my question above as it's more legible than putting the result in this comment. May 1, 2015 at 16:21

1 Answer 1

5

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!

1
  • why port 445? I'm able to connect to a synology nas but its running on port 5000
    – andrewgi
    Jan 26, 2019 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.