Skip to content

Commit

Permalink
Merge pull request #36 from p0dalirius/implement-wildcards-get-command
Browse files Browse the repository at this point in the history
[enhancement] Implemented wildcards in get command, resolves #28
  • Loading branch information
p0dalirius authored Jun 17, 2024
2 parents bf9b52a + f67c7c8 commit f0a6044
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
33 changes: 22 additions & 11 deletions smbclientng/core/CommandCompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,26 @@ def print_help_format(self):
This function displays the format of file attributes used in the smbclient-ng shell. It explains the meaning
of each character in the file attribute string, such as whether a file is read-only, hidden, or a directory.
"""

print("File attributes format:\n")
print("\x1b[1mdachnrst\x1b[0m")
print("\x1b[90m│││││││└──>\x1b[0m Temporary")
print("\x1b[90m││││││└───>\x1b[0m System")
print("\x1b[90m│││││└────>\x1b[0m Read-Only")
print("\x1b[90m││││└─────>\x1b[0m Normal")
print("\x1b[90m│││└──────>\x1b[0m Hidden")
print("\x1b[90m││└───────>\x1b[0m Compressed")
print("\x1b[90m│└────────>\x1b[0m Archived")
print("\x1b[90m└─────────>\x1b[0m Directory")
if self.config.no_colors:
print("File attributes format:\n")
print("dachnrst")
print("│││││││└──> Temporary")
print("││││││└───> System")
print("│││││└────> Read-Only")
print("││││└─────> Normal")
print("│││└──────> Hidden")
print("││└───────> Compressed")
print("│└────────> Archived")
print("└─────────> Directory")
else:
print("File attributes format:\n")
print("dachnrst")
print("\x1b[90m│││││││└──>\x1b[0m Temporary")
print("\x1b[90m││││││└───>\x1b[0m System")
print("\x1b[90m│││││└────>\x1b[0m Read-Only")
print("\x1b[90m││││└─────>\x1b[0m Normal")
print("\x1b[90m│││└──────>\x1b[0m Hidden")
print("\x1b[90m││└───────>\x1b[0m Compressed")
print("\x1b[90m│└────────>\x1b[0m Archived")
print("\x1b[90m└─────────>\x1b[0m Directory")

32 changes: 26 additions & 6 deletions smbclientng/core/SMBSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,35 @@ def get_file(self, path=None, keepRemotePath=False):
None
"""

tmp_file_path = self.smb_cwd + ntpath.sep + path
# Parse path
path = path.replace('/', ntpath.sep)
if ntpath.sep in path:
tmp_search_path = ntpath.normpath(self.smb_cwd + ntpath.sep + ntpath.dirname(path))
else:
tmp_search_path = ntpath.normpath(self.smb_cwd + ntpath.sep)
# Parse filename
filename = ntpath.basename(path)

# Search for the file
matches = self.smbClient.listPath(
shareName=self.smb_share,
path=tmp_file_path
)

path=tmp_search_path + ntpath.sep + '*'
)

# Filter the entries
matching_entries = []
for entry in matches:
if entry.get_longname() == filename:
matching_entries.append(entry)
elif '*' in filename:
regexp = filename.replace('.', '\\.').replace('*', '.*')
if re.match(regexp, entry.get_longname()):
matching_entries.append(entry)

for entry in matching_entries:
if entry.is_directory():
print("[>] Skipping '%s' because it is a directory." % tmp_file_path)
if self.config.debug:
print("[debug] [>] Skipping '%s' because it is a directory." % (tmp_search_path + ntpath.sep + entry.get_longname()))
else:
try:
if ntpath.sep in path:
Expand All @@ -277,7 +297,7 @@ def get_file(self, path=None, keepRemotePath=False):
)
self.smbClient.getFile(
shareName=self.smb_share,
pathName=tmp_file_path,
pathName=tmp_search_path + ntpath.sep + entry.get_longname(),
callback=f.write
)
f.close()
Expand Down

0 comments on commit f0a6044

Please sign in to comment.