Skip to content Skip to sidebar Skip to footer

After Executing A Command By Python Paramiko How Could I Save Result?

As you see below, is it possible to save the result? Cause, at second and third stdout.read() I couldn't reach the result. import paramiko import os dssh = paramiko.SSHClient() dss

Solution 1:

Imagine that stdout is an ordinary file. What do you expect to get if you call file.read() the second time? -- nothing (empty string) unless the file has changed outside.

To save the string:

output = stdout.read()

You might find Fabric simpler to use (it uses paramiko to execute commands under the hood).

Solution 2:

You can try this Generic API

def ssh_ctrl(ip, user, password,cmd):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh.connect(hostname=ip, username=user, password=password, timeout=tout, compress = True,look_for_keys=False, allow_agent=False)
    except (socket.error,paramiko.AuthenticationException,paramiko.SSHException) as message:
        print"ERROR: SSH connection to "+ip+" failed: " +str(message)
        sys.exit(1)

    stdin, stdout, ssh_stderr = ssh.exec_command(cmd)
    out = stdout.read()
    stdin.flush()
    ssh.close()
    return out

Post a Comment for "After Executing A Command By Python Paramiko How Could I Save Result?"