在 Python 中使用 Paramiko 收集 top 命令的输出 [英] Collect output from top command using Paramiko in Python
本文介绍了在 Python 中使用 Paramiko 收集 top 命令的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里我尝试执行 ssh 命令并打印输出.除了命令 top
外,它工作正常.任何线索如何从顶部收集输出?
Here I am trying to execute ssh commands and print the output. It works fine except the command top
.
Any lead how to collect the output from top ?
import paramiko
from paramiko import SSHClient, AutoAddPolicy, RSAKey
output_cmd_list = ['ls','top']
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname_ip, port, username, password)
for each_command in output_cmd_list:
stdin, stdout, stderr = ssh.exec_command(each_command)
stdout.channel.recv_exit_status()
outlines = stdout.readlines()
resp = ''.join(outlines)
print(resp)
推荐答案
top
是一个需要终端的奇特命令.虽然您可以使用 SSHClient.exec_command
,ANSI 转义码会给你带来很多垃圾.我不确定你想要那个.
The top
is a fancy command that requires a terminal. While you can enable the terminal emulation using get_pty
argument of SSHClient.exec_command
, it would get you lot of garbage with ANSI escape codes. I'm not sure you want that.
相反,以批处理模式执行top
:
Rather, execute the top
in batch mode:
top -b -n 1
See get top
output for non interactive shell.
这篇关于在 Python 中使用 Paramiko 收集 top 命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文