本文目录导读:

获取硬盘温度通常需要借助系统工具或调用硬件API,无法通过纯浏览器脚本实现,以下是几种主流操作系统的实现方法:
Linux 系统(推荐使用 smartctl)
# 安装 smartmontools(如果未安装) sudo apt-get install smartmontools # Debian/Ubuntu sudo yum install smartmontools # CentOS/RHEL # 获取所有硬盘温度(需root权限) sudo smartctl -A /dev/sda | grep Temperature_Celsius # 输出示例:190 Temperature_Celsius 0x0022 047 041 ... 47 (Min/Max 41/49)
自动化脚本(Bash):
#!/bin/bash
for disk in /dev/sd[a-z]; do
if [ -e "$disk" ]; then
temp=$(sudo smartctl -A "$disk" | grep -i "Temperature_Celsius" | awk '{print $10}')
[ -n "$temp" ] && echo "$disk: ${temp}°C"
fi
done
Windows 系统(使用 WMI 或第三方工具)
PowerShell 脚本(需管理员权限):
# 使用 WMI 获取(部分系统支持)
Get-WmiObject -Namespace "root\wmi" -Class MSStorageDriver_ATAPISmartData |
Select-Object InstanceName, @{n="Temp";e={[int]$_.Temperature}}
# 或使用第三方工具 smartctl (需下载Windows版)
# 下载地址:https://www.smartmontools.org/wiki/Download#BuildforWindows
批处理脚本(配合第三方工具):
@echo off
for %%d in (C D E) do (
"C:\Program Files\smartmontools\bin\smartctl.exe" -A %%d: | findstr Temperature_Celsius
)
macOS 系统
# 使用 smartctl(需安装 Homebrew) brew install smartmontools sudo smartctl -A /dev/disk0 | grep Temperature # 输出示例:Temperature: 42 Celsius
替代工具(无sudo):
# 使用 osx-cpu-temp(主要监控CPU,但部分机型支持硬盘) brew install osx-cpu-temp sudo osx-cpu-temp -d
跨平台 API 方案(Python + psutil)
import psutil
import subprocess
def get_disk_temperatures_linux():
try:
result = subprocess.run(
['sudo', 'smartctl', '-A', '/dev/sda'],
capture_output=True, text=True, check=True
)
for line in result.stdout.split('\n'):
if 'Temperature_Celsius' in line:
return int(line.split()[9])
except:
return None
def get_disk_temperatures_windows():
# 需安装 wmi 模块:pip install wmi
import wmi
c = wmi.WMI(namespace="root\\wmi")
for disk in c.MSStorageDriver_ATAPISmartData():
return int(disk.Temperature)
注意事项:
- 权限要求:几乎所有方法都需要管理员/root权限
- NVMe 硬盘:部分 NVMe 硬盘不兼容 smartctl,需使用
nvme-cli:sudo nvme smart-log /dev/nvme0 | grep temperature
- 固态硬盘/机械硬盘:部分 SSD 报告的温度字段不同(可能是
Airflow_Temperature_Cel) - 监控工具推荐:
- Linux:
hddtemp(需安装,更简单但数据不全面) - Windows:
HWiNFO64(提供GUI和命令行接口) - 跨平台:
Glances(Python监控工具,支持温度显示)
- Linux:
安全提示:
- 频繁查询可能会导致硬盘响应延迟(建议间隔≥30秒)
- 生产环境建议使用专用的硬件监控工具(如Zabbix、Prometheus结合node_exporter)