miketeo/pysmb

pysmb from linux to Windows, Unable to connect to shared device

reedv opened this issue · 4 comments

reedv commented

Trying to connect to an smb share via pysmb and getting error...

smb.smb_structs.OperationFailure: Failed to list  on \\\\H021BSBD20\\shared_folder: Unable to connect to shared device

The code I am using looks like...

from smb.SMBConnection import SMBConnection
import json
import pprint
import warnings

pp = pprint.PrettyPrinter(indent=4)
PROJECT_HOME = "/path/to/my/project/"

# load configs
CONF = json.load(open(f"{PROJECT_HOME}/configs/configs.json"))
pp.pprint(CONF)

# list all files in storage smb dir
#https://pysmb.readthedocs.io/en/latest/api/smb_SMBConnection.html#smb.SMBConnection.SMBConnection.listPath
IS_DIRECT_TCP = False
CNXN_PORT = 139 if not IS_DIRECT_TCP else 445
LOCAL_IP = "172.18.4.69"
REMOTE_NAME = "H021BSBD20"  # exact name shown as Device Name in System Settings
SERVICE_NAME = "\\\\H021BSBD20\\shared_folder"
REMOTE_IP = "172.18.7.102"
try:
    conn = SMBConnection(CONF['smb_creds']['username'], CONF['smb_creds']['password'],
                         my_name=LOCAL_IP, remote_name=REMOTE_NAME,
                         use_ntlm_v2=True,
                         is_direct_tcp=IS_DIRECT_TCP)
    conn.connect(REMOTE_IP, CNXN_PORT)
except Exception:
    warnings.warn("\n\nFailed to initially connect, attempting again with param use_ntlm_v2=False\n\n")
    conn = SMBConnection(CONF['smb_creds']['username'], CONF['smb_creds']['password'],
                         my_name=LOCAL_IP, remote_name=REMOTE_NAME,
                         use_ntlm_v2=False,
                         is_direct_tcp=IS_DIRECT_TCP)
    conn.connect(REMOTE_IP, CNXN_PORT)

files = conn.listPath(f'{SERVICE_NAME}', '\\')
pp.pprint(files)

Using smbclient on my machine, I can successfully connect to the share by doing...

[root@airflowetl etl]# smbclient -U my_user \\\\H021BSBD20\\shared_folder

The amount of backslashes I use in the python code is so that I can create the same string that works when using this smbclient (have tried with less backslashes in the code and that has not helped).

Note that the user that I am using the access the shared folder in the python code and with smbclient is not able to access / log on to the actual machine that the share is hosted on (they are only allowed to access that particular shared folder as shown above).

Does anyone know what could be happening here? Any other debugging steps that could be done?

The first parameter to listPath is invalid. Try using:

files = conn.listPath('shared_folder', '\\')

reedv commented

That worked thanks.
Could you explain why using 'shared_folder' as the servicename works here? I assumed that service name for smb was the full Network Path string that I see when looking at the folder Sharing properties on the shared smb drive (which is what worked for the smbclient example (which also describes that param as "servicename")).
(Not very experienced with Samba / smb).

Each server share (including printer shares) is exposed as a "service". You need to establish a session with the "service" first before you can perform any further operations on its files (in the SMB protocol, this is confusingly known as a tree connection).

I believe Samba client, as an end-user program, abstracts this from the user so you can just pass in the service name and remaining shared path as a single parameter.

pysmb, being an programmer's API, will be a bit more technical and specific in the parameters so as to support more complex usages and for learning purposes.

reedv commented

I see, each share is its own service.
Thank you.