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

# -*- 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 @ 2022-03-31 10:48  王亚锋  阅读(512)  评论(0)    收藏  举报