python paramiko获取top实时数据

  • Post author:
  • Post category:python


from logging import exception
import paramiko

class top():
    def __init__(self, ip='XXXXXX', port=22, name='XXX', pwd='XXXX',timeout=30):
        self.ip = ip
        self.port = port
        self.name = name
        self.pwd = pwd
        self.timeout = timeout
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        #连接
        try:
            self.client.connect(self.ip, self.port, self.name, self.pwd)
            print('----------连接成功-----------------')
        except:
            print("----------连接失败-----------------")

    #实时读取
    def realtime(self,cmd,notice=False):
        try:
            stdin, stdout, stderr = self.client.exec_command(cmd,get_pty=True)
            for line in stdout:
                print(line.strip("\n"))
            for error in stderr:
                print(error.strip("\n"))
            if notice:
                self.client.close()
        except Exception as e:
            print("execute command %s error, error message is %s" % (cmd, e))

# 执行命令

if __name__ == "__main__":
    ssh=top()  
    cmd = 'top -d 1'#设置读取一次时使用“top -n 1”,实时读取“top”  
    ssh.realtime(cmd)

注意get_pty默认False,在使用top时,需设置成True

self.client.exec_command(cmd,get_pty=True)

运行结果:

Tasks: 229 total,   1 running, 227 sleeping,   0 stopped,   1 zombie
%Cpu(s):  1.4 us,  5.8 sy,  0.0 ni, 92.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7802.2 total,   5953.6 free,    694.6 used,   1154.0 buff/cache
top - 13:28:10 up 56 min,  0 users,  load average: 0.22, 0.55, 0.61
Tasks: 227 total,   1 running, 225 sleeping,   0 stopped,   1 zombie
%Cpu(s):  1.5 us,  4.4 sy,  0.0 ni, 94.1 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   7802.2 total,   5949.2 free,    694.9 used,   1158.0 buff/cache
MiB Swap:  11429.0 total,  11429.0 free,      0.0 used.   6099.4 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND    
 2668 resourc+  20   0    5648   2588   2172 S   6.2   0.0   0:05.23 resourced
 6144 root      20   0       0      0      0 I   6.2   0.0   0:36.31 kworker/u+
 9163 root      20   0       0      0      0 S   6.2   0.0   0:06.84 kworker/u+
 9689 root      20   0    3352   2412   1864 R   6.2   0.0   0:00.02 top        
    1 root      20   0    5196   4236   2376 S   0.0   0.1   0:00.68 init
    2 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kthreadd
    3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp
    4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp
    8 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu+
    9 root      20   0       0      0      0 S   0.0   0.0   0:00.10 ksoftirqd+
   10 root      20   0       0      0      0 I   0.0   0.0   0:04.62 rcu_preem+
   11 root      rt   0       0      0      0 S   0.0   0.0   0:00.03 migration+
   13 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/0
   14 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/1
   15 root      rt   0       0      0      0 S   0.0   0.0   0:00.31 migration+
   16 root      20   0       0      0      0 S   0.0   0.0   0:00.04 ksoftirqd+
   19 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/2



版权声明:本文为qq_31802739原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。