0

I used paramiko to execute "virsh list --all" command remotely, but can't get the whole output, my code is

import paramiko


def paramiko_connect(hostname, command):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=hostname)
    std_in, std_out, std_err = ssh.exec_command(command)
    output = std_out.read().decode("utf-8")
    return output


cmd = "virsh list --all"
result = paramiko_connect("10.10.10.10", cmd)
print(result)

the code's output is

 Id    Name                           State
----------------------------------------------------


but the real output in remote server is

 Id    Name                           State
----------------------------------------------------
 1     VM_Test                      running

How can I get the remote server's real output?

Claire
  • 61
  • 5
  • I added the ssh.close() before return output, but still can't get the whole output, the ssh connect is working, only can read partly content. – Claire Dec 02 '22 at 07:51
  • yes, I have tried stdout.readlines(), still got the same output – Claire Dec 02 '22 at 09:28
  • stderr is empty, and status_code is 0, **ssh hostname virsh list --al**, I tried this command which result is ```Id Name State ---------------------------------------------------- ```, so the root cause in there, How can I collect this kind of command from remote server? – Claire Dec 02 '22 at 10:04
  • Thank you very much, using invoke_shell is succeed. – Claire Dec 02 '22 at 10:32

0 Answers0