python 连接linux 获取cpu温度 以及top打印信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
# @Time : 2022/3/31 9:05
# @Author : wangyafeng
# @FileName: ToLinux.py
# @Email : yafengwang@dingtalk.com
# @Software: PyCharm
 
import paramiko,time,datetime
 
 
# 创建SSHClient实例对象
ssh = paramiko.SSHClient()
 
# 调用方法,标识没有远程机器的公钥,允许访问
# key = paramiko.RSAKey.from_private_key_file('/home/jeff/.ssh/id_rsa')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
# 连接远程机器 地址端口用户名密码
ssh.connect("192.168.2.203", 22, "root", "root")
#查看内存
while True:
    # print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    # stdin, stdout, stderr = ssh.exec_command("free -g")
    # out = str(stdout.read(), encoding='utf-8')
    # print("===================内存=================================")
    # print("total", out[97:99])
    # print("buff/cache",out[146:148])
    # print("available",out[158:160])
    #同理  sensors同样  查看温度
    print("=====温度======",datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    sensorsstdin, sensorsstdout, sensorsstderr = ssh.exec_command("sensors")
    sensorsout=str(sensorsstdout.read(), encoding='utf-8')
    print(sensorsout)
    time.sleep(2)
 
 
 
'''
1、连接linux
2、执行命令
3、解析
4、写入excle
5、加个while True:  
''''''执行top 获取服务器进程信息'''import paramiko, time, datetime
import schedule
 
# 创建SSHClient实例对象
ssh = paramiko.SSHClient()
 
# 调用方法,标识没有远程机器的公钥,允许访问
# key = paramiko.RSAKey.from_private_key_file('/home/jeff/.ssh/id_rsa')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
# 连接远程机器 地址端口用户名密码
ssh.connect("118.190.199.159", 22, "root", "KDcPv41jdnMdJsIWGMugG*fE")
 
 
# 查看内存
 
def job(name):
    print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), name)
    timestr=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    stdin, stdout, stderr = ssh.exec_command("top -b -n1")
    out = str(stdout.read(), encoding='utf-8')
    print(out)
    with open('202322abc.txt', 'a') as f:
        f.write('=======================================')
        f.write(timestr)
        f.write('\n')
        f.write(out)
 
 
name = 'gettop'
schedule.every(2).minutes.do(job, name)
 
while True:
    schedule.run_pending()  # run_pending:运行所有可以运行的任务
复制代码
复制代码

 

  

posted @   王亚锋  阅读(488)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示