怎么用脚本获取主板温度

wen 实用脚本 4

本文目录导读:

怎么用脚本获取主板温度

  1. Linux 系统
  2. Windows 系统
  3. macOS 系统
  4. 跨平台方案
  5. 获取主板型号+温度组合
  6. 注意事项

获取主板温度可以通过多种脚本方式实现,具体取决于你的操作系统和硬件支持,以下是几种常见的方法:

Linux 系统

使用 lm-sensors(推荐)

# 安装 lm-sensors
sudo apt-get install lm-sensors  # Debian/Ubuntu
sudo yum install lm_sensors      # CentOS/RHEL
# 配置传感器
sudo sensors-detect
# 获取主板温度
sensors | grep -i "temp"

使用 Python 脚本

#!/usr/bin/env python3
import subprocess
import re
def get_motherboard_temp():
    try:
        # 执行 sensors 命令
        output = subprocess.check_output(['sensors'], universal_newlines=True)
        # 查找主板温度(通常显示为 Tctl 或 Tdie)
        for line in output.split('\n'):
            if 'Tctl' in line or 'Tdie' in line or 'temp1' in line:
                # 提取温度数值
                match = re.search(r'\+(\d+\.\d+)°C', line)
                if match:
                    return float(match.group(1))
        return None
    except Exception as e:
        print(f"Error: {e}")
        return None
temp = get_motherboard_temp()
if temp:
    print(f"主板温度: {temp}°C")
else:
    print("无法获取主板温度")

直接读取系统文件

# 某些系统直接在 /sys 目录下
cat /sys/class/thermal/thermal_zone*/temp | awk '{print $1/1000}'

Windows 系统

使用 PowerShell + WMI

# 使用 WMI 获取温度(需要管理员权限)
Get-WmiObject -Namespace root\wmi -Class MSAcpi_ThermalZoneTemperature | 
Select-Object -Property CurrentTemperature | 
ForEach-Object { 
    $temp = ($_.CurrentTemperature / 10) - 273.15
    [math]::Round($temp, 1)
}

使用 OpenHardwareMonitor 命令行

# 需要先下载 OpenHardwareMonitor
.\OpenHardwareMonitorReport.exe /TXT | Select-String "Mainboard"

使用 Python + WMI

import wmi
def get_motherboard_temp():
    c = wmi.WMI(namespace="root\\wmi")
    temperatures = c.MSAcpi_ThermalZoneTemperature()
    for temp in temperatures:
        # 转换为摄氏度
        celsius = (temp.CurrentTemperature / 10) - 273.15
        return round(celsius, 1)
    return None
temp = get_motherboard_temp()
print(f"主板温度: {temp}°C")

macOS 系统

使用 osx-cpu-temp 工具

# 安装 osx-cpu-temp
brew install osx-cpu-temp
# 获取温度
osx-cpu-temp -m  # 显示主板温度

使用 Python + py-sysctl

import sysctl
def get_motherboard_temp():
    try:
        # 读取系统控制参数
        temp = sysctl.sysctl("machdep.xcpm.pkg_temp")
        if temp:
            # 转换为摄氏度
            celsius = temp / 100
            return round(celsius, 1)
    except:
        pass
    return None

跨平台方案

使用 Psutil(跨平台)

import psutil
def get_system_temps():
    # psutil 主要获取 CPU/GPU 温度
    temps = psutil.sensors_temperatures()
    if 'coretemp' in temps:  # Intel
        return temps['coretemp'][0].current
    elif 'k10temp' in temps:  # AMD
        return temps['k10temp'][0].current
    return None
temp = get_system_temps()

获取主板型号+温度组合

这是一个更完整的 Linux 示例:

#!/usr/bin/env python3
import subprocess
import re
def get_motherboard_info():
    try:
        # 获取主板信息
        board_info = subprocess.check_output(
            ['dmidecode', '-t', '2'], 
            universal_newlines=True
        )
        # 提取主板型号
        manufacturer = re.search(r'Manufacturer: (.+)', board_info)
        product = re.search(r'Product Name: (.+)', board_info)
        # 获取温度
        sensors_output = subprocess.check_output(
            ['sensors'], 
            universal_newlines=True
        )
        # 解析温度
        temp_match = re.search(r'Package id 0:\s+\+(\d+\.\d+)°C', sensors_output)
        return {
            'manufacturer': manufacturer.group(1).strip() if manufacturer else 'Unknown',
            'model': product.group(1).strip() if product else 'Unknown',
            'temperature': float(temp_match.group(1)) if temp_match else None
        }
    except Exception as e:
        return {'error': str(e)}
info = get_motherboard_info()
print(f"主板: {info.get('manufacturer')} {info.get('model')}")
print(f"温度: {info.get('temperature')}°C")

注意事项

  1. 权限问题:某些系统需要 root/管理员权限
  2. 硬件支持:不是所有主板都支持温度监控
  3. 传感器位置:不同主板传感器命名可能不同
  4. 安全提醒:确保从官方来源下载工具

选择哪种方法取决于你的具体需求和操作系统环境,对于 Linux 系统,lm-sensors 是最可靠的选择;Windows 上可以尝试 WMI 或第三方工具。

抱歉,评论功能暂时关闭!