[英]Reading output of Top command using Paramiko
I am writing a script in Python for login to ssh and read the output of commands just executed. 我正在用Python写一个脚本来登录ssh并读取刚刚执行的命令的输出。 I am using paramiko package for this.
我正在使用paramiko包。 I am trying to execute command "top" and get its output printed on the console.
我正在尝试执行命令“top”并在控制台上打印其输出。 However, I am not able to do this.
但是,我无法做到这一点。 Please 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()
Output: Trying to connect to localhost (1/30) 输出:尝试连接到localhost(1/30)
Linux Linux的
Linux Linux的
Command done, closing SSH connection 命令完成,关闭SSH连接
I am not sure, where I am doing wrong. 我不确定,我做错了什么。 I am able to get output of other linux commands though.
我能够获得其他linux命令的输出。 But not sure, why top command's output is not getting printed.
但不确定,为什么top命令的输出没有打印出来。
top
normally uses curses for display rather than just printing. top
通常使用curses进行显示而不仅仅是打印。 Try the -b
for batch option along with the -n 1
you have (top options vary by platform, check the manpage). 尝试使用
-b
作为批处理选项以及您拥有的-n 1
(顶部选项因平台而异,请查看联机帮助页)。 And in the future, try isolating the problem more - if you were to invoke top
via ssh
on the command line without your script you would still have an issue. 并且在将来,尝试更多地隔离问题 - 如果您在命令行
top
通过ssh
调用top
而没有脚本,那么您仍然会遇到问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.