Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python SMB Example
from smb.SMBConnection import SMBConnection
userID = 'user'
password = 'password'
client_machine_name = 'localpcname'
server_name = 'servername'
server_ip = '0.0.0.0'
domain_name = 'domainname'
conn = SMBConnection(userID, password, client_machine_name, server_name, domain=domain_name, use_ntlm_v2=True,
is_direct_tcp=True)
conn.connect(server_ip, 445)
shares = conn.listShares()
for share in shares:
if not share.isSpecial and share.name not in ['NETLOGON', 'SYSVOL']:
sharedfiles = conn.listPath(share.name, '/')
for sharedfile in sharedfiles:
print(sharedfile.filename)
conn.close()
# with open('pysmb.py', 'rb') as file:
# conn.storeFile('remotefolder', 'pysmb.py', file)
@tklenke

This comment has been minimized.

Copy link

tklenke commented Feb 15, 2017

I banged my head against the wall trying to get this to run on Jessie x86 distro. Kept getting
ImportError: No module named SMBConnection
Finally figured out that replacing
from smb.SMBConnection import SMBConnection
with the following works.

from smb import *
from SMBConnection import *
@BSCowboy

This comment has been minimized.

Copy link

BSCowboy commented Nov 2, 2017

according to the docs, I think you could also use:
smb.SMBConnection import SMBConnection

@brucevanhorn2

This comment has been minimized.

Copy link

brucevanhorn2 commented Jun 27, 2018

What's the deal with the commented out storeFile code at the end? That's the part I need, but when I uncomment and change it, I get an error stating that it can't connect to the resource, even the code that lists the shares demonstrates that it can connect.

@brucevanhorn2

This comment has been minimized.

Copy link

brucevanhorn2 commented Jun 27, 2018

I figured it out. My mistake was probably common. The first param is the share name. I was trying to add the sub-folder, the destination, in that same string like foo/bar. I actually needed to put it in the destination file name. So to save to share foo in a folder called bar it would be

with open('/home/bruce/PycharmProjects/smb_test/venv/test-file.txt', 'rb') as file:
    conn.storeFile('foo', 'bar/test-file.txt', file)
@omidraha

This comment has been minimized.

Copy link

omidraha commented Sep 11, 2018

What's the package name of this 'smb' module?


>>> import smb
Tracebac
```k (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named smb
>>> 
@Wolf0fmusic

This comment has been minimized.

Copy link

Wolf0fmusic commented Oct 2, 2018

from smb.SMBConnection import *
^
This will work after installing the whole pysmb packages. :)

@nstarke

This comment has been minimized.

Copy link

nstarke commented Oct 24, 2018

The package name under debian is python-smb

@Splint3r7

This comment has been minimized.

Copy link

Splint3r7 commented Oct 6, 2019

The package name under debian is python-smb

@nstarke Thanks. saved my life <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.