jborean93 / smbprotocol

Python SMBv2 and v3 Client
MIT License
316 stars 73 forks source link

Issuing commands / printing #185

Closed paulschmeida closed 2 years ago

paulschmeida commented 2 years ago

Hi. I am trying to figure out how to replicate this functionality of smbclient: smbclient -U username \\\\server\\printer_share -c "print file.txt"

I've looked through files but I can't figure out how to do it with this library and I would like to avoid using subprocess for compatibility reasons.

adiroiban commented 2 years ago

I guess that first, you can try to use the low-level smbprotocol code to open the printer and write the file to it.

https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/8122dfdb-d11f-44e4-91d6-f290d26e1db0

For sending a file to a printer, the application opens the root of a print share, writes data, and closes the file. The semantics and parameters are the same as specified in section 3.2.4.3, except that TreeConnect.ShareName will be the name of a printer share, and the share relative path MUST be NULL.


I have never used SMB for printing... so I don't know the details.

jborean93 commented 2 years ago

Yea this is theoretically possible and based on the docs you might just be able to do

import smbclient

with smbclient.open_file(r"\\server\printer_share", mode="xb", buffering=0) as fd:
    fd.write(file_bytes)

But even then I've never tested this or are sure if it will work.

paulschmeida commented 2 years ago

Hi, just wanted to confirm that it works exactly as you described @jborean93 :) I can even skip having the files on the filesystem and just pass io.BytesIO() objects to it.