weixin_34318994 2021-04-18 18:20 采纳率: 100%
浏览 286
已采纳

Python3 paramiko执行TOP不输出执行结果,请大神帮忙看看什么问题

#!/usr/bin/python3
# -*- coding:utf-8 -*-

import paramiko
import time

def main(host, username, password, commands):
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(host, username=username, password=password,
                   port=22, allow_agent=False, look_for_keys=False)
    channel = client.invoke_shell()  # 请求交互式Shell会话
    for command in commands:
        channel.send(command + "\n")  # 发送指令
        while not channel.recv_ready():  # 等待数据到达
            time.sleep(1)
        output = channel.recv(40960)  
        print(output.decode())
    client.close()

if __name__ == '__main__':
    host = '192.168.33.129'
    username = 'lyun'
    password = '8'
    commands = ["top -d 1 -b | grep 'edac-poller'"] 
    main(host, username, password, commands)

在linux系统操作可以输出执行结果

代码执行没有

有的服务可以输出执行结果

 

 

 

 

 

 

  • 写回答

6条回答 默认 最新

  • weixin_34318994 2021-04-19 21:28
    关注
    已解决 ,是宽度不够,加宽点就可以了
    
    channel = client.invoke_shell(width=800) 
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?