Reading Top Command Output Using Paramiko

I am writing a script in Python to log into ssh and read the output of the commands just executed. For this I am using the paramiko package. I am trying to execute the command "top" and get its output printed to the console. However, I cannot do this. Find the 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()

      

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

Connected to localhost

Linux

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

Linux


Executing command, closing SSH connection

I am not sure where I am going wrong. However, I can get the output of other Linux commands. But not sure why the top command output is not printing.

+3


source to share


2 answers


top

usually uses curses for display, not just printing. Try -b

for the package option along with -n 1

yours (the top options are platform dependent, check the man page). And in the future, try to isolate the problem more - if you called top

via ssh

on the command line without a script, you still have a problem.



+1


source


as Jason S pointed out

stdin, stdout, stderr = ssh.exec_command("top -b -n1")
print stdout.read()

      



works great.

+1


source







All Articles