Windows 批处理文件脚本 - 基于 Abmient Temp 的风扇控制速度 Dell R710

问题描述 投票:0回答:1

我正在尝试编写一个 Windows 批处理文件脚本,以根据环境温度为 Dell R710 生成一些输出操作。我安装了 IMPITools 并在系统上运行(它生成环境温度输出到文件)并且有代码片段,我正在努力将其组合在一起以根据返回的值执行操作。

到目前为止我有什么:

REM Find out what the Ambient Temp is..
ipmitool -I lanplus -H <IP Address> -U root -P calvin sensor reading "Ambient Temp" > temp.txt

--> 这给了我一个输出文件,其中给出了(主板的)温度环境:

Ambient Temp     | 22

我想做的是将文件字符串读入 ms dos.bat 文件中的局部变量,然后根据该值 - 获取字符串的实际读取部分,例如例如

22
到局部变量
%temp%
.

有人可以帮助我弄清楚如何执行此操作,以便我可以将文本文件或 IMPI 工具输出的这一部分获取到局部变量中吗?

根据读数的输出,我希望执行不同的操作:

If "%temp%" >50
ECHO The AC is Broken - Call an Engineer and power down the server !
GOTO END

If "%temp%" >30
ECHO Turn the AC Up to MAX - its getting toasty in here 
GOTO END

If "%temp%" >25
ECHO Turn the AC On.
GOTO END

If "%temp%" >= 20 or "%temp%" <= 24
ECHO Turn the AC and Heating Off - save energy and the planet
GOTO END

If "%temp%" < 20
ECHO Turn the heating on - its getting cold in here and close the door !
GOTO END

If "%temp%" < 5
ECHO Call the engineer to fix the heating - its almost freezing in here !
GOTO END

:END

-->> 进展更新 02/09/2021 <<-- Okay, I've managed to get the a for statement to work for this part of the first part..

REM FAN CONTROL ACTIVATED
SET LOCAL

REM Get the current reading of the Temp to the file 
dell_bmc\ipmitool -I lanplus -H <IP> -U <UserName> -P <Pwd> sensor reading "Ambient Temp" >> temp.txt

REM Get the number of lines in the temp.txt file
FOR /F "tokens=3 delims= " %%i IN ('find /v /c "temp" temp.txt') DO set /a lines=%%i

REM Use Set /A to subtract 5 from the line number to get how many lines to skip from the file
set /a startLine=%lines% - 5

REM Set a counter to get the last 5 Temp Readings and Current One
set Cnt=0

REM Use "More" to read the file and skip a number of lines in the file
FOR /F "tokens=4 delims= " %%A IN ('more /E +%startLine% temp.txt') DO (
REM You would use the "TYPE" version if you were only reading one value from the file.
REM FOR /F "tokens=4 delims= " %%A IN ('type temp.txt') DO (
  Echo %%A - A Value
  CALL SET Temp_Current=%%A
  set /a Cnt+=1
  call Set Temp_Current[%%Cnt%%]=%%A
)
ECHO Temp-Current[0] %Temp_Current%
Echo Temp-Current[1] %Temp_Current[1]%
Echo Temp-Current[2] %Temp_Current[2]%
Echo Temp-Current[3] %Temp_Current[3]%
Echo Temp-Current[4] %Temp_Current[4]%
Echo Temp-Current[5] %Temp_Current[5]%
CALL SET Temp_Current = %Temp_Current[5]%
REM Temp-Current [5] is the latest temp reading as its the last line in the file

REM To stop the file getting to big - we will re-output the temp.txt file and ensure it only contains up-to the last 10 readings.
SET /a TargetLeft=%lines%/5-1
SET /a NewStartPos=%TargetLeft%*5
more /E +%NewStartPos% temp.txt >temp2.txt
REM  Replace the file with limited number of lines that was generated to be the new temp.txt file output 
COPY temp2.txt temp.txt /Y

我现在如何实现逻辑检查块算法部分 - 正如您所指出的,IF 无法用于检查 %Temp_Current% 的值来执行我想要执行的操作?

windows batch-file
1个回答
0
投票

这是我的第一个脚本,用于根据 Poweredge R720xd 上的一个处理器的温度来控制风扇速度

ECHO OFF
SET LOCAL
CLS
REM Set Server IP address, Username and Password for the IPMI connection
set serverip=192.168.1.211
set username=root
set password=Password
REM Set Temperature thresholds and fan speeds in decimal and hex
set temp1=45
set fan1=30
set hex1=0x1e
set temp2=50
set fan2=35
set hex2=0x23
set temp3=55
set fan3=45
set hex3=0x28
set temp4=60
set fan4=55
set hex4=0x37
set temp5=65
set fan5=65
set hex5=0x41
ECHO ---------------------------------------------------------
ECHO    Dell Poweredge Manual Fan Speed Control Script v1.1
ECHO ---------------------------------------------------------
ECHO.
ECHO  Setting Manual Fan Speed Control
ipmitool -I lanplus -H %serverip% -U %username% -P %password% raw 0x30 0x30 0x01 0x00
ECHO.
ECHO  Setting fans speed at %fan1%%%
ipmitool -I lanplus -H %serverip% -U %username% -P %password% raw 0x30 0x30 0x02 0xff %hex1%
timeout /t 3
:temperature_check
CLS
REM erase previous temp.txt file
del temp.txt
ECHO ---------------------------------------------------------
ECHO    Dell Poweredge Manual Fan Speed Control Script v1.1
ECHO ---------------------------------------------------------
ECHO.
ECHO  Reading Temperature Sensors and Creating Temp.txt file
REM Saves current temperatures to temp.txt file
ipmitool -I lanplus -H %serverip% -U %username% -P %password% sdr type temperature > temp.txt
ECHO  Reading Fan Speed Sensors and Other PSU and Temp Sensors
ECHO ---------------------------------------------------------
ipmitool -I lanplus -H %serverip% -U %username% -P %password% sdr list full
REM Reads current temperature from temp.txt
set Cnt=0
set Temp_Current[3]=0
set Temp_Current[4]=0
REM Get the number of lines in the temp.txt file
FOR /F "tokens=3 delims= " %%i IN ('find /v /c "temp" temp.txt') DO set /a lines=%%i
set /a startLine=%lines%
FOR /F "tokens=9 delims= " %%A IN ('type temp.txt') DO (
REM  Echo %%A
  CALL SET Temp_Current=%%A
  set /a Cnt+=1
  call Set Temp_Current[%%Cnt%%]=%%A
)
Echo  [CPU 0] Current Temperature %Temp_Current[3]%
Echo  [CPU 1] Current Temperature %Temp_Current[4]%
if %Temp_Current[4]% GTR %temp5% goto temp05
if %Temp_Current[4]% GTR %temp4% goto temp04
if %Temp_Current[4]% GTR %temp3% goto temp03
if %Temp_Current[4]% GTR %temp2% goto temp02
if %Temp_Current[4]% == %temp2% goto temp01
if %Temp_Current[4]% LSS %temp2% goto temp01
ECHO Script is not working!
goto end
:temp01
Echo  Current CPU Temperature is below %temp1% degrees Celsius.
Echo  Setting Fans Speed at %fan1%%%
ipmitool -I lanplus -H %serverip% -U %username% -P %password% raw 0x30 0x30 0x02 0xff %hex1%
timeout /t 3
goto temperature_check
:temp02
Echo  Current CPU Temperature is below %temp2% degrees Celsius.
Echo  Setting Fans Speed at %fan2%%%
ipmitool -I lanplus -H %serverip% -U %username% -P %password% raw 0x30 0x30 0x02 0xff %hex2%
timeout /t 3
goto temperature_check
:temp03
Echo  Current CPU Temperature is below %temp3% degrees Celsius.
Echo  Setting Fans Speed at %fan3%%%
ipmitool -I lanplus -H %serverip% -U %username% -P %password% raw 0x30 0x30 0x02 0xff %hex3%
timeout /t 3
goto temperature_check
:temp04
Echo  Current CPU Temperature is below %temp4% degrees Celsius.
Echo  Setting Fans Speed at %fan4%%%
ipmitool -I lanplus -H %serverip% -U %username% -P %password% raw 0x30 0x30 0x02 0xff %hex4%
timeout /t 3
goto temperature_check
:temp05
Echo  Current CPU Temperature is below %temp5% degrees Celsius.
Echo  Setting Fans Speed at %fan5%%%
ipmitool -I lanplus -H %serverip% -U %username% -P %password% raw 0x30 0x30 0x02 0xff %hex5%
timeout /t 3
goto temperature_check

:end
Echo Ending Script
Pause
© www.soinside.com 2019 - 2024. All rights reserved.