在Windows上使用Paramiko,SSH到Linux,top命令不返回任何内容

2024-06-16 15:03:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python脚本中使用Paramiko来ssh到远程Linux机器。连接本身工作正常,但是当我尝试运行一个特定的Linux命令时,Paramiko什么也没有返回。当直接在Linux机器上运行时,该命令返回我想要的输出。我的代码如下:

sshapp = paramiko.SSHClient()
sshapp.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshapp.load_system_host_keys
sshapp.connect(host,username=user,password=passwd)
stdin,stdout,stderr = appliance.exec_command("top -n2 | grep 'Cpu(s)' | awk '{print $2}' | awk 'NR==2' | cut -d % -f 1") # grab cpu number as an integer
output = stdout.readlines()
print(output)

如果我用一个更简单的命令(比如ls-la)执行相同的序列,那么输出将按预期返回。但是在上面给出的top命令下,输出是空的。我错过了什么?在

谢谢!在


Tags: 命令脚本机器hostparamikooutput远程linux