使用Paramiko读取Top命令的输出

4 投票
3 回答
13004 浏览
提问于 2025-04-18 15:47

我正在用Python写一个脚本,目的是登录到ssh并读取刚执行的命令的输出。我使用的是paramiko这个包。现在我想执行“top”这个命令,并把它的输出打印到控制台上。但是我遇到了一些问题,无法做到这一点。以下是我的代码片段:

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()

输出:

正在尝试连接到本地主机 (1/30)

已连接到本地主机

Linux

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

Linux


命令执行完毕,正在关闭SSH连接

我不太确定哪里出了问题。虽然我可以获取其他Linux命令的输出,但不明白为什么“top”命令的输出没有打印出来。

3 个回答

0

在我的情况下,如果不使用 -b 选项来运行 top 命令(在 QNX 系统上),解决办法是使用 exec_command 中的 get_pty=True。举个例子:

    stdin, stdout, stderr = ssh_client.exec_command("top -D 1 -t -i 1", get_pty=True)
    print(stdout.read())

否则,打印出来的结果总是 b''。

1

top 通常使用 curses 这个工具来显示信息,而不是简单地打印出来。你可以试试加上 -b 这个批处理选项,再加上你已经用的 -n 1(不同平台的 top 选项可能会有所不同,可以查看手册页)。将来,建议你更好地定位问题——如果你在命令行中通过 ssh 直接调用 top,而不使用你的脚本,你可能还是会遇到同样的问题。

2

正如 Jason S 指出的那样,

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

这个方法运行得很好。

撰写回答