Skip to content

Commit

Permalink
Implemented output to a file in find command, fixes #78
Browse files Browse the repository at this point in the history
  • Loading branch information
p0dalirius committed Jul 9, 2024
1 parent 7a67c5e commit c53d121
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
11 changes: 7 additions & 4 deletions smbclientng/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,19 @@ def windows_ls_entry(entry, config, pathToPrint=None):

date_str = datetime.datetime.fromtimestamp(entry.get_atime_epoch()).strftime("%Y-%m-%d %H:%M")

output_str = ""
if entry.is_directory():
if config.no_colors:
print("%s %10s %s %s\\" % (meta_string, size_str, date_str, pathToPrint))
output_str = ("%s %10s %s %s\\" % (meta_string, size_str, date_str, pathToPrint))
else:
print("%s %10s %s \x1b[1;96m%s\x1b[0m\\" % (meta_string, size_str, date_str, pathToPrint))
output_str = ("%s %10s %s \x1b[1;96m%s\x1b[0m\\" % (meta_string, size_str, date_str, pathToPrint))
else:
if config.no_colors:
print("%s %10s %s %s" % (meta_string, size_str, date_str, pathToPrint))
output_str = ("%s %10s %s %s" % (meta_string, size_str, date_str, pathToPrint))
else:
print("%s %10s %s \x1b[1m%s\x1b[0m" % (meta_string, size_str, date_str, pathToPrint))
output_str = ("%s %10s %s \x1b[1m%s\x1b[0m" % (meta_string, size_str, date_str, pathToPrint))

return output_str


def local_tree(path, config):
Expand Down
26 changes: 21 additions & 5 deletions smbclientng/modules/Find.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Date created : 23 may 2024


import os
import ntpath
import re
from smbclientng.core.Module import Module
Expand Down Expand Up @@ -39,6 +40,7 @@ def parseArgs(self, arguments):

# Adding positional arguments
parser.add_argument("paths", metavar="PATH", type=str, nargs="*", default=[], help="The starting point(s) for the search.")
parser.add_argument("-q", "--quiet", action="store_true", default=False, help="Suppress normal output.")

# Adding options for filtering
parser.add_argument("-name", type=str, help="Base of file name (the path with the leading directories removed).")
Expand All @@ -52,6 +54,7 @@ def parseArgs(self, arguments):
# Adding actions
parser.add_argument("-ls", action="store_true", default=False, help="List current file in ls -dils format on standard output.")
parser.add_argument("-download", action="store_true", default=False, help="List current file in ls -dils format on standard output.")
parser.add_argument("-o", "--outputfile", type=str, help="Write the names of the files found to the specified file.")

# Other options
parser.add_argument("-maxdepth", type=int, help="Descend at most levels (a non-negative integer) levels of directories below the command line arguments.")
Expand Down Expand Up @@ -189,17 +192,25 @@ def __find_callback(self, entry, fullpath, depth):
else:
self.smbSession.get_file(path=fullpath, keepRemotePath=True)
# Output formats
output_str = ""
if self.options.ls:
if entry.is_directory():
windows_ls_entry(entry=entry, config=self.config, pathToPrint=fullpath)
output_str = windows_ls_entry(entry=entry, config=self.config, pathToPrint=fullpath)
else:
windows_ls_entry(entry=entry, config=self.config, pathToPrint=fullpath)
output_str = windows_ls_entry(entry=entry, config=self.config, pathToPrint=fullpath)
else:
if entry.is_directory():
print("%s" % fullpath.replace(ntpath.sep, '/'))
output_str = ("%s" % fullpath.replace(ntpath.sep, '/'))
else:
print("%s" % fullpath.replace(ntpath.sep, '/'))

output_str = ("%s" % fullpath.replace(ntpath.sep, '/'))

if self.options.outputfile is not None:
with open(self.options.outputfile, 'a') as f:
f.write(output_str + '\n')

if not self.options.quiet:
print(output_str)

return None

def run(self, arguments):
Expand All @@ -219,6 +230,11 @@ def run(self, arguments):

if self.options is not None:
# Entrypoint
if self.options.outputfile is not None:
if not os.path.exists(os.path.dirname(self.options.outputfile)):
os.makedirs(os.path.dirname(self.options.outputfile))
open(self.options.outputfile, 'w').close()

try:
next_directories_to_explore = []
for path in list(set(self.options.paths)):
Expand Down

0 comments on commit c53d121

Please sign in to comment.