Skip to content

Commit

Permalink
Update network.py: run_ssh_command
Browse files Browse the repository at this point in the history
Add option to print result to file
  • Loading branch information
kimpaller committed Aug 14, 2023
1 parent 14779b5 commit 4e9a35f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions nebula/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import string
import subprocess
import time
import datetime as dt

import fabric
import nebula.errors as ne
Expand Down Expand Up @@ -135,9 +136,15 @@ def reboot_board(self, bypass_sleep=False):
raise Exception("Exception occurred during SSH Reboot", str(ex))

def run_ssh_command(
self, command, ignore_exceptions=False, retries=3, show_log=True
self,
command,
ignore_exceptions=False,
retries=3,
show_log=True,
print_result_to_file=True
):
result = None
filename = None
for t in range(retries):
log.info(
"ssh command:" + command + " to " + self.dutusername + "@" + self.dutip
Expand All @@ -150,16 +157,24 @@ def run_ssh_command(
if result.failed:
raise Exception("Failed to run command:", command)

if print_result_to_file:
filename = dt.datetime.now().strftime("%Y%m%d%H%M%S")

if show_log and result.stdout:
log.info("result stdout begin -------------------------------")
log.info(f"{result.stdout}")
log.info("result stdout end -------------------------------")
if print_result_to_file:
with open(f"{self.board_name}_out_{filename}.log","w") as f:
f.write(result.stdout)

if show_log and result.stderr:
log.info("result stderr begin -------------------------------")
log.info(f"{result.stderr}")
log.info("result stderr end -------------------------------")

if print_result_to_file:
with open(f"{self.board_name}_err_{filename}.log","w") as f:
f.write(result.stderr)
break
except Exception as ex:
log.warning("Exception raised: " + str(ex))
Expand Down

0 comments on commit 4e9a35f

Please sign in to comment.