Skip to content

Commit

Permalink
runcmd(): add if_stdout=False (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Junefen authored Jan 22, 2024
1 parent 946b929 commit 99ff27f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions hypervisor/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,18 @@ def rsa_transfer(self):
sftp = paramiko.SFTPClient.from_transport(transport)
return sftp, transport

def runcmd(self, cmd):
def runcmd(self, cmd, if_stdout=False):
"""Executes SSH command on remote hostname.
:param str cmd: The command to run
:param str if_stdout: default to return the stderr
"""
ssh = self._connect()
logger.info(">>> {}".format(cmd))
stdin, stdout, stderr = ssh.exec_command(cmd)
code = stdout.channel.recv_exit_status()
stdout, stderr = stdout.read(), stderr.read()
ssh.close()
if not stderr:
if if_stdout or not stderr:
logger.info("<<< stdout\n{}".format(stdout.decode()))
return code, stdout.decode()
else:
Expand Down
2 changes: 1 addition & 1 deletion hypervisor/virt/libvirt/libvirtcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def guest_ip(self, guest_name):
if gateway and guest_mac:
option = "grep 'Nmap scan report for' | grep -Eo '([0-9]{1,3}[\.]){3}[0-9]{1,3}'| tail -1"
cmd = f"nmap -sP -n {gateway} | grep -i -B 2 {guest_mac} | {option}"
ret, output = self.ssh.runcmd(cmd)
ret, output = self.ssh.runcmd(cmd, if_stdout=True)
if not ret and output is not None and output != "":
guest_ip = output.strip()
logger.info(f"Succeeded to get libvirt guest ip ({guest_ip})")
Expand Down

0 comments on commit 99ff27f

Please sign in to comment.