miketeo/pysmb

NotConnectedError when trying to connect to a share from Linux

mivade opened this issue · 6 comments

I have a Linux cluster which has tools such as smbclient installed but as far as I know no NetBIOS service. I have the following test code:

import os
import socket
from smb.SMBConnection import SMBConnection

username = os.getenv("SMB_USERNAME")
password = os.getenv("SMB_PASSWORD")
host = os.getenv("SMB_HOST")
ip = socket.gethostbyname(host)

conn = SMBConnection(username, password, '', host)
conn.connect(ip)

print([s.name for s in conn.listShares()])

which results in the following traceback:

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    conn.connect(ip)
  File "/home/mdepalatis/shared/miniconda3/envs/pyfs/lib/python3.8/site-packages/smb/SMBConnection.py", line 122, in connect
    self._pollForNetBIOSPacket(timeout)
  File "/home/mdepalatis/shared/miniconda3/envs/pyfs/lib/python3.8/site-packages/smb/SMBConnection.py", line 594, in _pollForNetBIOSPacket
    raise NotConnectedError
smb.base.NotConnectedError

Why is NetBIOS required? I am able to connect to shares just fine using smbclient directly so this isn't a general connection error.

See also althonos/fs.smbfs#2

@mivade : You need to provide the client and server's machine name for the 3rd/4th parameter.
You can use a random client name within 15 chars, eg. TESTCLIENT, but the server machine name must match the one that is configured on the server (note that server machine name may not be the same as the hostname)

On Windows, you can get the machine from the System panel (shown under the Device field).
sc

So the problem is that I don't have access to the Windows machine and have no way of figuring out what the server name is if it's different from the hostname (sure, I could ask, but that only works so long as I'm given UNC paths that are always using the same host which is not guaranteed). I'm not super familiar with how Samba shares work, but smbclient can resolve the host provided I give it a FQDN.

You can use the nmblookup command which is part of the Samba suite to do the reverse lookup.
nmblookup -A IP_address
Line entries with <20> contain the remote server's machine name.

That results in No reply from <IP> for me (but pinging the IP address works fine).

If your UNC path looks this: \\servername\path ,
you already have the remote server name as indicated by 'servername'

conn = SMBConnection(username, password, 'TESTCLIENT', 'SERVERNAME')
conn.connect(ip)

OK, that gave me a hint to try. Apparently you cannot use the FQDN at all but only the name. Additionally I also have to specify the domain keyword argument:

conn = SMBConnection(username, password, "TEST", short_host, domain=domain)
conn.connect(ip)