def list_dir(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) entries = list(scandir(path)) files = [] dirs = [] for entry in entries: if entry.is_file(): files.append(entry.name) if entry.is_dir(): dirs.append(entry.name) context = { 'SharedFolder': path, 'Files': files, 'Directories': dirs, } return CommandResults( outputs_prefix='SMB.Path', outputs_key_field='SharedFolder', outputs=context, readable_output=tableToMarkdown(f'List Of Entries for {path}', context), )
def read_directory(): #movie_titles = [] register_session("Server", username=USER, password=PASSWORD) for file in scandir(MOVIES_PATH): if fnmatch.fnmatch(file.name, '*.dvdmedia'): title = file.name.replace('_', ' ').replace('.dvdmedia', '').title() #movie_titles.append(title) insert_movie(title, file.name)
def list_dir_content(absoluteDirPath: str) -> Iterable[FileDescriptor]: for dirElement in smbclient.scandir(absoluteDirPath): if not any( map(lambda pattern: fnmatch(dirElement.path, pattern), ignorePatterns)): if dirElement.is_file(follow_symlinks=followSymlinks): yield SmbFileDescriptor(dirElement.path, self.schemeAndLocation) elif dirElement.is_dir(follow_symlinks=followSymlinks): for subDirElement in list_dir_content(dirElement.path): yield subDirElement else: # We can't handle this kind of file logger.warn('Unable to process element [%s]' % dirElement.path) pass
def scandir(self, path, search_pattern="*"): return smbclient.scandir( self._join_path(path), search_pattern=search_pattern, **self._conn_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))