From 99ff27f5e9ae5b1bf6147493ce3fe366b858e57c Mon Sep 17 00:00:00 2001 From: Juneliu Date: Mon, 22 Jan 2024 13:55:02 +0800 Subject: [PATCH] runcmd(): add if_stdout=False (#36) --- hypervisor/ssh.py | 5 +++-- hypervisor/virt/libvirt/libvirtcli.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hypervisor/ssh.py b/hypervisor/ssh.py index 5bafdbe..3a877fa 100644 --- a/hypervisor/ssh.py +++ b/hypervisor/ssh.py @@ -71,9 +71,10 @@ 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)) @@ -81,7 +82,7 @@ def runcmd(self, 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: diff --git a/hypervisor/virt/libvirt/libvirtcli.py b/hypervisor/virt/libvirt/libvirtcli.py index a931e51..b7f7443 100644 --- a/hypervisor/virt/libvirt/libvirtcli.py +++ b/hypervisor/virt/libvirt/libvirtcli.py @@ -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})")