(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
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".