Lesen-Ausgabe des Top-Befehls mit Paramiko

Schreibe ich ein Skript in Python für login, ssh und Lesen Sie die Ausgabe der Befehle gerade ausgeführt werden. Ich bin mit paramiko-Paket für diese. Ich bin versucht, durch den Befehl "top" und bekommen dessen Ausgabe auf der Konsole ausgegeben. Allerdings bin ich nicht in der Lage, dies zu tun. Finden Sie die snippet:

import sys
import time
import select
import paramiko

host = 'localhost'
i = 1

#
# Try to connect to the host.
# Retry a few times if it fails.
#
while True:
    print 'Trying to connect to %s (%i/30)' % (host, i)

    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, port=22, username='dummy', password='dummy')
        print "Connected to %s" % host
        break
    except paramiko.AuthenticationException:
        print "Authentication failed when connecting to %s" % host
        sys.exit(1)
    except:
        print "Could not SSH to %s, waiting for it to start" % host
        i += 1
        time.sleep(2)

    # If we could not connect within time limit
    if i == 30:
        print "Could not connect to %s. Giving up" % host
        sys.exit(1)

# Send the command (non-blocking)
stdin, stdout, stderr = ssh.exec_command("uname")

# Wait for the command to terminate
while not stdout.channel.exit_status_ready():
    # Only print data if there is data to read in the channel
    if stdout.channel.recv_ready():
        rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
        if len(rl) > 0:
            # Print data from stdout
        print '-------------------------------'
            print stdout.channel.recv(1024)
        print '-------------------------------'

# Send the command (non-blocking)
stdin, stdout, stderr = ssh.exec_command("top -n 1")

# Wait for the command to terminate
while not stdout.channel.exit_status_ready():
    # Only print data if there is data to read in the channel
    if stdout.channel.recv_ready():
        rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
        if len(rl) > 0:
            # Print data from stdout
        print '-------------------------------'
            print stdout.channel.recv(1024)
        print '-------------------------------'

# Send the command (non-blocking)
stdin, stdout, stderr = ssh.exec_command("uname")

# Wait for the command to terminate
while not stdout.channel.exit_status_ready():
    # Only print data if there is data to read in the channel
    if stdout.channel.recv_ready():
        rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
        if len(rl) > 0:
            # Print data from stdout
        print '-------------------------------'
            print stdout.channel.recv(1024)
        print '-------------------------------'

#
# Disconnect from the host
#
print "Command done, closing SSH connection"
ssh.close()

Ausgabe:
Trying to connect to localhost (1/30)

Verbunden mit localhost

Linux

-------------------------------

Linux


Befehl getan, schließen der SSH-Verbindung

Ich bin mir nicht sicher, wo ich falsch mache. Ich bin in der Lage zu bekommen die Ausgabe von anderen linux-Befehle aber. Aber nicht sicher, warum top-Befehl die Ausgabe ist nicht immer gedruckt.

InformationsquelleAutor N Deepak Prasath | 2014-08-03

Schreibe einen Kommentar