python - 使用 Paramiko 读取 Top 命令的输出

标签 python linux ssh paramiko pexpect

我正在用 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 命令的输出没有被打印出来。

最佳答案

作为Jason S指出

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

工作得很好。

关于python - 使用 Paramiko 读取 Top 命令的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25101619/

相关文章:

python - 如何按名称更改目录的用户和组权限?

C linux sem_wait 在线程中(有时)不起作用

linux - 在 linux 中比较 2 个文件的不同单词

linux - 如何从 Linux 程序 screen 上读取最新的一行?

linux - 权限被拒绝(公钥)ssh

java - 布鲁克林起始蓝图 : VMs created but Brooklyn can't SSH in

linux - 如何在 ssh 命令中转义引号

python - 正则表达式删除任何数字/字母组合?

python - Influxdb 读/写操作

python - 如何将 x lim 设置为我的 matplotlib 直方图数据系列的 99.5 个百分点?