Skip to content

Commit

Permalink
Implemented --logfile option
Browse files Browse the repository at this point in the history
  • Loading branch information
p0dalirius committed Jul 8, 2024
1 parent 33fc89d commit decc118
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions smbclientng/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def parseArgs():
parser.add_argument("--no-colors", dest="no_colors", action="store_true", default=False, help="No colors mode.")
parser.add_argument("-S", "--startup-script", metavar="startup_script", required=False, type=str, help="File containing the list of commands to be typed at start of the console.")
parser.add_argument("-N", "--not-interactive", dest="not_interactive", required=False, action="store_true", default=False, help="Non interactive mode.")
parser.add_argument("-L", "--logfile", dest="logfile", metavar="LOGFILE", required=False, default=None, type=str, help="File to write logs to.")

group_target = parser.add_argument_group("Target")
group_target.add_argument("--host", action="store", metavar="HOST", required=True, type=str, help="IP address or hostname of the SMB Server to connect to.")
Expand Down Expand Up @@ -107,7 +108,7 @@ def main():
config.not_interactive = options.not_interactive
config.startup_script = options.startup_script

logger = Logger(config=config, logfile=None)
logger = Logger(config=config, logfile=options.logfile)

sessionsManager = SessionsManager(config=config, logger=logger)

Expand Down Expand Up @@ -135,8 +136,7 @@ def main():
)
shell.run()

if options.debug:
print("[debug] Exiting the console.")
logger.debug("Exiting the console.")


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions smbclientng/core/InteractiveShell.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def run(self):
while self.running:
try:
user_input = input(self.__prompt()).strip()
self.logger.write_to_logfile(self.__prompt() + user_input)
self.process_line(commandLine=user_input)
except KeyboardInterrupt as e:
self.logger.print()
Expand Down
14 changes: 8 additions & 6 deletions smbclientng/core/Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self, config, logfile=None):
k += 1
self.logfile = self.logfile + (".%d" % k)
open(self.logfile, "w").close()
self.debug("Writting logs to logfile: '%s'" % self.logfile)

def print(self, message="", end='\n'):
"""
Expand All @@ -64,7 +65,7 @@ def print(self, message="", end='\n'):
print(nocolor_message, end=end)
else:
print(message, end=end)
self.__write_to_logfile(nocolor_message, end=end)
self.write_to_logfile(nocolor_message, end=end)

def info(self, message):
"""
Expand All @@ -81,7 +82,7 @@ def info(self, message):
print("[info] %s" % nocolor_message)
else:
print("[\x1b[1;92minfo\x1b[0m] %s" % message)
self.__write_to_logfile("[info] %s" % nocolor_message)
self.write_to_logfile("[info] %s" % nocolor_message)

def debug(self, message):
"""
Expand All @@ -99,7 +100,7 @@ def debug(self, message):
print("[debug] %s" % nocolor_message)
else:
print("[debug] %s" % message)
self.__write_to_logfile("[debug] %s" % nocolor_message)
self.write_to_logfile("[debug] %s" % nocolor_message)

def error(self, message):
"""
Expand All @@ -116,9 +117,9 @@ def error(self, message):
print("[error] %s" % nocolor_message)
else:
print("[\x1b[1;91merror\x1b[0m] %s" % message)
self.__write_to_logfile("[error] %s" % nocolor_message)
self.write_to_logfile("[error] %s" % nocolor_message)

def __write_to_logfile(self, message, end='\n'):
def write_to_logfile(self, message, end='\n'):
"""
Writes the provided message to the log file specified during Logger instance initialization.
Expand All @@ -130,5 +131,6 @@ def __write_to_logfile(self, message, end='\n'):

if self.logfile is not None:
f = open(self.logfile, "a")
f.write(message + end)
nocolor_message = re.sub(r"\x1b[\[]([0-9;]+)m", "", message)
f.write(nocolor_message + end)
f.close()

0 comments on commit decc118

Please sign in to comment.