Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh/runcmd(): add if_stdout=False #36

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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