def upload_for_smb(root, paths, target_path, checkpoint): if len(paths) == 0: return for path in paths: print('send path : ' + path) try: smbclient.mkdir(target_path + '/' + path,username=username, password=password, port=smb_port) except: pass send_files = get_event_files(root, path) for send_file in send_files: try: smbclient.stat(target_path + '/' + path + '/' + send_file.split('/')[-1],username=username, password=password, port=smb_port) except: print('sending ' + send_file) dest = smbclient.open_file(target_path+'/'+path+'/'+send_file.split('/')[-1], 'wb',username=username, password=password, port=smb_port) src = open(send_file, 'rb') while True: temp = src.read(1024 * 4) if len(temp) == 0: break dest.write(temp) src.close() dest.close() make_checkpoint(paths, checkpoint)
def test_islink_with_dir_link(smb_share): src_dir_name = "%s\\dir" % smb_share dst_dir_name = "%s\\link" % smb_share mkdir(src_dir_name) symlink(src_dir_name, dst_dir_name) assert islink(dst_dir_name) is True
def upload(self,localFile,remotePath): try: smbclient.register_session(self.host, username=self.username, password=self.password, connection_timeout=30) except Exception as e: self.error = str(e) return False # single file if not os.path.isdir(localFile): try: with open(localFile, mode="rb") as f: with smbclient.open_file("\\{0}\{1}".format(self.host,remotePath), mode="wb") as remoteFile: while True: part = f.read(4096) if not part: break remoteFile.write(part) except Exception as e: self.error = str(e) smbclient.delete_session(self.host) return False smbclient.delete_session(self.host) return True # Directory else: try: smbclient.mkdir("\\{0}\{1}".format(self.host,remotePath)) except OSError as e: if e.errno != errno.EEXIST: return False for root, dirs, files in os.walk(localFile): for dir in dirs: fullPath = os.path.join(root,dir) fullPath=fullPath.replace("/","\\") try: smbclient.mkdir("\\{0}\{1}\{2}".format(self.host,remotePath,fullPath[len(localFile)+1:])) except OSError as e: if e.errno != errno.EEXIST: return False for _file in files: try: fullPath = os.path.join(root,_file) with open(fullPath, mode="rb")as f: fullPath=fullPath.replace("/","\\") with smbclient.open_file("\\{0}\{1}\{2}".format(self.host,remotePath,fullPath[len(localFile)+1:]), mode="wb") as remoteFile: while True: part = f.read(4096) if not part: break remoteFile.write(part) except Exception as e: self.error = str(e) smbclient.delete_session(self.host) return False smbclient.delete_session(self.host) return True return False
def smb_mkdir(client: SMBClient, args: dict): hostname = args.get('hostname') username = args.get('username') password = args.get('password') path = handle_path(args.get('path')) path = os.path.join(hostname or client.hostname, path) client.create_session(hostname, username, password) mkdir(path) return f"Directory: {path} was created successfully"
def smb_share(request, smb_real): # Use some non ASCII chars to test out edge cases by default. share_path = u"%s\\%s" % (smb_real[request.param[1]], u"Pýtæs†-[%s] 💩" % time.time()) delete_session(smb_real[2]) # Test out forward slashes also work with the share-encrypted test if request.param[0] == 'share-encrypted': share_path = share_path.replace('\\', '/') mkdir(share_path, username=smb_real[0], password=smb_real[1], port=smb_real[3]) try: yield share_path finally: rmtree(share_path, username=smb_real[0], password=smb_real[1], port=smb_real[3])
def smb_dfs_share(request, smb_real): test_folder = u"Pýtæs†-[%s] 💩" % time.time() if request.param[1]: target_share_path = u"%s\\%s" % (smb_real[request.param[1]], test_folder) dfs_path = u'\\\\%s\\dfs\\%s\\%s' % (smb_real[2], request.param[0], test_folder) else: target_share_path = u"\\\\%s\\dfs\\%s" % (smb_real[2], test_folder) dfs_path = target_share_path mkdir(target_share_path, username=smb_real[0], password=smb_real[1], port=smb_real[3]) try: yield dfs_path finally: rmtree(target_share_path, username=smb_real[0], password=smb_real[1], port=smb_real[3]) config = ClientConfig() config._domain_cache = [] config._referral_cache = []
def mkdir(self, path): return smbclient.mkdir(self._join_path(path), **self._conn_kwargs)
def mkdir(self, path, create_parents=True, **kwargs): wpath = _as_unc_path(self.host, path) if create_parents: smbclient.makedirs(wpath, exist_ok=False, **kwargs) else: smbclient.mkdir(wpath, **kwargs)
from smbclient import ( listdir, mkdir, register_session, rmdir, scandir, ) # Optional - register the server with explicit credentials register_session("server", username="******", password="******") # Create a directory (only the first request needs credentials) mkdir(r"\\server\share\directory", username="******", password="******") # Remove a directory rmdir(r"\\server\share\directory") # List the files/directories inside a dir for filename in listdir(r"\\server\share\directory"): print(filename) # Use scandir as a more efficient directory listing as it already contains info like stat and attributes. for file_info in scandir(r"\\server\share\directory"): file_inode = file_info.inode() if file_info.is_file(): print("File: %s %d" % (file_info.name, file_inode)) elif file_info.is_dir(): print("Dir: %s %d" % (file_info.name, file_inode)) else: print("Symlink: %s %d" % (file_info.name, file_inode))
def test_islink_with_dir(smb_share): dir_name = "%s\\dir" % smb_share mkdir(dir_name) assert islink(dir_name) is False
def test_isdir_with_dir(smb_share): dir_name = "%s\\dir" % smb_share mkdir(dir_name) assert isdir(dir_name) is True