Skip to content

Instantly share code, notes, and snippets.

Created March 16, 2016 13:45
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 = ''
domain_name = 'domainname'
conn = SMBConnection(userID, password, client_machine_name, server_name, domain=domain_name, use_ntlm_v2=True,
conn.connect(server_ip, 445)
shares = conn.listShares()
for share in shares:
if not share.isSpecial and not in ['NETLOGON', 'SYSVOL']:
sharedfiles = conn.listPath(, '/')
for sharedfile in sharedfiles:
# with open('', 'rb') as file:
# conn.storeFile('remotefolder', '', file)
Copy link

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.

Copy link

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)

Copy link

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

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

Copy link

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

Copy link

nstarke commented Oct 24, 2018

The package name under debian is python-smb

Copy link

The package name under debian is python-smb

@nstarke Thanks. saved my life <3

Copy link

root@sfr:/tmp# pip3 install pysmb
Requirement already satisfied: pysmb in /usr/local/lib/python3.6/dist-packages
Requirement already satisfied: pyasn1 in /usr/lib/python3/dist-packages (from pysmb)
root@sfr:/tmp# python3
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

import smb
Traceback (most recent call last):
File "", line 1, in
File "/tmp/", line 2, in
from smb.SMBConnection import SMBConnection
ModuleNotFoundError: No module named 'smb.SMBConnection'; 'smb' is not a package

gos this error, why this happen ?

Copy link

@blinkbink I have the same, did u manage to solve it ?

Copy link

Jack-I commented Jan 25, 2021

@blinkbink I have the same, did u manage to solve it ?

Easy and stupid way (mine):

  1. Download manually from
  2. Copy smb and nmb folders to your site-packages folder (usually C:\users\user\AppData\Local\Continuum\anaconda3\Lib\site-packages if using Anaconda)
  3. Enjoy from smb.SMBConnection import SMBConnection

Copy link

hadrian3689 commented Apr 29, 2021

To use "from smb.SMBConnection import SMBConnection" I was able to get it working with:
sudo apt install python3-smb

Copy link

Fyi, for those having the same issue as me :

pysmb is conflicting with impacket (they both have a file named You need to remove one before using the other, and vice versa.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment