Search code examples
pythonlinuxsambaos.walk

pysmb to get directory tree of a smb share server


i manage to connect and access a smb share server using pysmb. what i meant is to read/write/delete/create files/folders to/from the server.

majority of the time i need to read file ( be it jpg or csv and etc) from the server base on the smb device and service name (pysmb terms).

basically i have no idea what is the filename and directory name in the smb devices. meaning the naming is dynamics.

i am wondering is it a good idea to get the filtered directory tree first before processing read files. the numbers of files and directories is not known with about 3 months data about 60TB.

listShares(timeout=30)[source]
listPath(service_name, path, search=55, pattern='*', timeout=30)

the above methods about to get only 1 specific level of the hierarchy. what i want is the similar output from os.walk.path().

anybody have experience in idea? can i get suggestions? thank you very much.


Solution

  • Not sure if this is what you want. but i'm working on similar kind of stuff so here you go.

    I use Impacket which actually use some base classes from pysmb. https://github.com/CoreSecurity/impacket

    I hope your listPath method is returning output in text format and not SharedFile instance.

    What i mean is, store below values while listing them.

    get_longname is_directory get_filesize

    I have tree method which traverse through share/path and checks if SharedFile instance is directory, & does recursive call to itself.

    def tree(self, path):    
       for x in range(0, path.count('\\')):
                print '|  ',
        print '%s' % os.path.basename(path.replace('\\', '/'))
    
        self.do_ls('%s\\*' % path, pretty=False) #Stores files data in listdata[]
    
        for file, is_directory, size in self.listdata:
                if file in ('.', '..'):
                    continue
                if is_directory > 0:
                    self.tree(ntpath.join(path, file))
                else:
                    for x in range(0, path.count('\\')):
                        print '|  ',
                    print '|-- %s (%d bytes)' % (file, size)
    
    
    >>>d.tree('test')
    .snapshot
    |   hourly.0
    |   |   dir0
    |   |   |   Test051-89
    |   |   |   Test051_perf3100-test_43
    |   |   |   |   Test051_perf3100-test_52
    |   |-- a.txt (8 bytes)
    |   |-- dir0 - Shortcut.lnk (1834 bytes)
    |   |-- Thumbs.db (46080 bytes)
    |   |   20743
    |   |   |-- file.txt (82 bytes)
    |   |   |-- link.txt (82 bytes)
    |   |   |   targetdir
    |   |   |   |-- file2.txt (39 bytes)
    |   |-- target.txt (6394368 bytes)
    |   |   linkdir
    |   |   |-- file2.txt (39 bytes)