Skip to content

Commit

Permalink
Added 'bat' command with syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
p0dalirius committed Jun 3, 2024
1 parent 4a70914 commit 532b909
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion smbclientng/core/CommandCompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def complete(self, text, state):
if s.lower().startswith(remainder.lower())
]

elif command in ["get", "rm"]:
elif command in ["bat", "cat", "get", "rm"]:
# Choose local files and directories
path = ""
if '\\' in remainder.strip() or '/' in remainder.strip():
Expand Down
30 changes: 30 additions & 0 deletions smbclientng/core/InteractiveShell.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import traceback
from rich.console import Console
from rich.table import Table
from rich.syntax import Syntax
from smbclientng.core.CommandCompleter import CommandCompleter
from smbclientng.core.utils import b_filesize, unix_permissions, windows_ls_entry, local_tree

Expand Down Expand Up @@ -227,6 +228,35 @@ def process_command(self, command, arguments=[]):

# Commands ================================================================

@command_arguments_required
@active_smb_connection_needed
@smb_share_is_set
def command_bat(self, arguments, command):
# Command arguments required : Yes
# Active SMB connection needed : Yes
# SMB share needed : Yes

path = ' '.join(arguments)
try:
rawcontents = self.smbSession.read_file(path=path)
if rawcontents is not None:
encoding = charset_normalizer.detect(rawcontents)["encoding"]
if encoding is not None:
filecontent = rawcontents.decode(encoding).rstrip()
lexer = Syntax.guess_lexer(path=ntpath.basename(path), code=filecontent)
# Some trickery for the files undetected by the lexer
if lexer == "default":
if '<?xml' in filecontent:
lexer = "xml"
elif '<html>' in filecontent:
lexer = "html"
syntax = Syntax(code=filecontent, line_numbers=True, lexer=lexer)
Console().print(syntax)
else:
print("[!] Could not detect charset of '%s'." % path)
except impacket.smbconnection.SessionError as e:
print("[!] SMB Error: %s" % e)

@command_arguments_required
@active_smb_connection_needed
@smb_share_is_set
Expand Down

0 comments on commit 532b909

Please sign in to comment.