Skip to content

Instantly share code, notes, and snippets.

@iapyeh
Created January 14, 2021 04:27
Show Gist options
  • Save iapyeh/6513244bd2b983b66970cf953004cbfa to your computer and use it in GitHub Desktop.
Save iapyeh/6513244bd2b983b66970cf953004cbfa to your computer and use it in GitHub Desktop.
Access private files on NAS by samba in python3
# install the dependence by "python3 -m pip install pysmb"
from smb import SMBConnection
def CheckFilesInNASbySamba():
username = 'username'
password = 'password'
path = r'fs/folder/whatever'
system_name = '192.168.0.1'
domain = ''
try:
conn = SMBConnection.SMBConnection(username,password,path,system_name,domain,
use_ntlm_v2=True,
sign_options=SMBConnection.SMBConnection.SIGN_WHEN_SUPPORTED,
is_direct_tcp=True)
connected = conn.connect(system_name,445)
try:
Response = conn.listShares(timeout=30) # obtain a list of shares
print('Shares on: ' + system_name)
for i in range(len(Response)): # iterate through the list of shares
print(" Share[",i,"] =", Response[i].name)
try:
# list the files on each share
Response2 = conn.listPath(Response[i].name,'/',timeout=30)
print(' Files on: ' + system_name + '/' + " Share[",i,"] =",
Response[i].name)
for i in range(len(Response2)):
print(" File[",i,"] =", Response2[i].filename)
except:
print('### can not access the resource')
except:
traceback.print_exc()
print('### can not list shares')
except:
traceback.print_exc()
print('### can not access the system')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment