230 lines
6.7 KiB
Batchfile
230 lines
6.7 KiB
Batchfile
@echo off
|
|
setlocal enabledelayedexpansion
|
|
|
|
:: Script Info
|
|
set "ScriptVersion=0.0.1"
|
|
set "ScriptName=bmctool"
|
|
|
|
set "OUTPUT_DIR=%CD%\bmctool_output"
|
|
set "COMMAND="
|
|
set "PLUGIN="
|
|
|
|
:: 检查配置文件
|
|
:CONFIG_CHECK
|
|
:: Step1. 导入默认配置
|
|
set "CONF_PATH=%CD%\setting"
|
|
for /f "usebackq delims=" %%a in (`
|
|
powershell -command "$json = Get-Content '%CONF_PATH%\bmcconf.json' | ConvertFrom-Json; $json.ip"
|
|
`) do set "BMC_IP=%%a"
|
|
|
|
for /f "usebackq delims=" %%a in (`
|
|
powershell -command "$json = Get-Content '%CONF_PATH%\bmcconf.json' | ConvertFrom-Json; $json.user"
|
|
`) do set "USERNAME=%%a"
|
|
|
|
for /f "usebackq delims=" %%a in (`
|
|
powershell -command "$json = Get-Content '%CONF_PATH%\bmcconf.json' | ConvertFrom-Json; $json.pass"
|
|
`) do set "PASSWORD=%%a"
|
|
|
|
:: Step2. 查询插件脚本是否启用
|
|
:: 如果不存在plugin路径则直接跳过该步骤
|
|
set "PLUGIN_DIR=%CD%\plugin"
|
|
if not exist %PLUGIN_DIR% goto :END_CONFIG_CHECK
|
|
|
|
for %%f in ("%PLUGIN_DIR%\plugin_*.bat") do (
|
|
set "filename=%%~nf"
|
|
|
|
for /f "tokens=2 delims=_" %%a in ("!filename!") do (
|
|
set "plugin_id=%%a"
|
|
)
|
|
|
|
:: echo Find plugin : !plugin_id!
|
|
for /f "usebackq delims=" %%a in (`
|
|
powershell -command "$json = Get-Content '%CONF_PATH%\plugin.json' | ConvertFrom-Json; $json.!plugin_id!"
|
|
`) do set "is_enable=%%a"
|
|
|
|
if "!is_enable!" == "enable" (
|
|
:: echo !plugin_id! is valid
|
|
set "PLUGIN=!plugin_id!"
|
|
set "PluginBat=%PLUGIN_DIR%\plugin_!PLUGIN!.bat"
|
|
goto :END_CONFIG_CHECK
|
|
)
|
|
)
|
|
|
|
:END_CONFIG_CHECK
|
|
:: Default Scirpt Varible
|
|
:: set "BMC_IP=192.168.100.2"
|
|
:: set "USERNAME=ADMIN"
|
|
:: set "PASSWORD=ADMIN"
|
|
|
|
|
|
:: 解析参数
|
|
:PARSE_ARGS
|
|
if "%1"=="" goto :USAGE
|
|
if "%1"=="-h" goto :USAGE
|
|
if "%1"=="--help" goto :USAGE
|
|
|
|
:ARG_LOOP
|
|
if "%1"=="" goto :ARGS_PARSED
|
|
if "%1"=="-i" (
|
|
set "BMC_IP=%2"
|
|
shift
|
|
) else if "%1"=="--ip" (
|
|
set "BMC_IP=%2"
|
|
shift
|
|
) else if "%1"=="-u" (
|
|
set "USERNAME=%2"
|
|
shift
|
|
) else if "%1"=="--user" (
|
|
set "USERNAME=%2"
|
|
shift
|
|
) else if "%1"=="-p" (
|
|
set "PASSWORD=%2"
|
|
shift
|
|
) else if "%1"=="--pass" (
|
|
set "PASSWORD=%2"
|
|
shift
|
|
) else (
|
|
:: 剩余参数作为命令
|
|
set "COMMAND=%COMMAND%%1"
|
|
)
|
|
shift
|
|
goto :ARG_LOOP
|
|
|
|
:ARGS_PARSED
|
|
:: 验证参数
|
|
:: echo BMC IP : %BMC_IP%
|
|
:: echo USERNAME: %USERNAME%
|
|
:: echo PASSWORD: %PASSWORD%
|
|
:: echo Command : %COMMAND%
|
|
set "IPMI=ipmitool/ipmitool.exe -I lanplus -H %BMC_IP% -U %USERNAME% -P %PASSWORD%"
|
|
|
|
if "%COMMAND%"=="" set "COMMAND=info"
|
|
|
|
:: 创建输出目录
|
|
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"
|
|
|
|
:: 执行命令
|
|
call :EXECUTE_COMMAND
|
|
exit /b %ERRORLEVEL%
|
|
|
|
:: 命令执行函数
|
|
:EXECUTE_COMMAND
|
|
if /i "%COMMAND%"=="info" (
|
|
call :GET_SYSTEM_INFO
|
|
) else if /i "%COMMAND:~0,5%"=="power" (
|
|
call :MANAGE_POWER %COMMAND:~5%
|
|
) else if /i "%COMMAND:~0,3%"=="set" (
|
|
call :SET_CONFIG %COMMAND:~3%
|
|
) else if /i "%COMMAND%" == "sensor" (
|
|
call :GET_SENSOR_ELIST
|
|
) else if /i "%COMMAND%" == "sdr" (
|
|
call :GET_SDR_ELIST
|
|
) else if /i "%COMMAND%" == "fru" (
|
|
call :GET_FRU_INFO
|
|
) else (
|
|
:: 调用插件
|
|
if defined PluginBat (
|
|
call %PluginBat% -i %BMC_IP% -u %USERNAME% -p %PASSWORD% %COMMAND%
|
|
|
|
if !errorlevel! == 1 (
|
|
echo Invalid Command !
|
|
exit /b 1
|
|
)
|
|
) else (
|
|
echo Invalid Command !
|
|
exit /b 1
|
|
)
|
|
)
|
|
exit /b 0
|
|
|
|
:: 获取BMC系统信息
|
|
:GET_SYSTEM_INFO
|
|
echo Run Command GET_SYSTEM_INFO
|
|
%IPMI% mc info
|
|
exit /b 0
|
|
|
|
:: 电源管理
|
|
:MANAGE_POWER
|
|
echo Run Command MANAGE_POWER
|
|
echo Param: %1
|
|
set "action=%1"
|
|
if /i "%action%"=="on" (
|
|
%IPMI% power on
|
|
) else if /i "%action%"=="off" (
|
|
%IPMI% power off
|
|
) else if /i "%action%"=="cycle" (
|
|
%IPMI% power cycle
|
|
)
|
|
exit /b 0
|
|
|
|
:: Change defaut settings
|
|
:SET_CONFIG
|
|
echo Modify local config
|
|
set "Param=%1"
|
|
if /i "!Param:~0,2!" == "ip" (
|
|
set "ipaddr=!Param:~2!"
|
|
echo Change ip to !ipaddr!
|
|
powershell -Command "$json = Get-Content '%CONF_PATH%\bmcconf.json' | ConvertFrom-Json; $json.ip = '!ipaddr!'; $json | ConvertTo-Json | Set-Content '%CONF_PATH%\bmcconf.json'"
|
|
) else if /i "!Param:~0,4!" == "user" (
|
|
set "inputuser=!Param:~4!"
|
|
echo Change user to !inputuser!
|
|
powershell -Command "$json = Get-Content '%CONF_PATH%\bmcconf.json' | ConvertFrom-Json; $json.user = '!inputuser!'; $json | ConvertTo-Json | Set-Content '%CONF_PATH%\bmcconf.json'"
|
|
) else if /i "!Param:~0,4!" == "pass" (
|
|
set "inputpass=!Param:~4!"
|
|
echo Change password to !inputpass!
|
|
powershell -Command "$json = Get-Content '%CONF_PATH%\bmcconf.json' | ConvertFrom-Json; $json.pass = '!inputpass!'; $json | ConvertTo-Json | Set-Content '%CONF_PATH%\bmcconf.json'"
|
|
) else (
|
|
echo Invalid Param !
|
|
exit /b 1
|
|
)
|
|
|
|
echo Modify Done
|
|
exit /b 0
|
|
|
|
:GET_SENSOR_ELIST
|
|
echo Run Command GET_SENSOR_ELIST
|
|
%IPMI% sensor elist
|
|
exit /b 0
|
|
|
|
:GET_SDR_ELIST
|
|
echo Run Command GET_SDR_ELIST
|
|
%IPMI% sdr elist
|
|
exit /b 0
|
|
|
|
:GET_FRU_INFO
|
|
echo Run Command GET_FRU_INFO
|
|
%IPMI% fru
|
|
exit /b 0
|
|
|
|
:: Help info
|
|
:USAGE
|
|
echo %ScriptName% v%ScriptVersion% Usage
|
|
echo.
|
|
echo Format: %ScriptName% [-i bmcip] [-u username] [-p password] command
|
|
echo.
|
|
echo Param Details
|
|
echo -i, --ip BMC IP Address
|
|
echo -u, --user BMC IPMI User Name
|
|
echo -p, --pass BMC IPMI User Password
|
|
echo -h, --help Show this info
|
|
echo.
|
|
echo Common Command List
|
|
echo info Get BMC System Info
|
|
echo power on Set Power on to BMC
|
|
echo power off Set Power off to BMC
|
|
echo power cycle Set Power cycle to BMC
|
|
echo sensor Show the sensor elist
|
|
echo sdr Show the sdr elist
|
|
echo fru Show the fru info
|
|
echo set ip [ip addr] Set ip addr to default config
|
|
echo set user [name] Set IPMI user to default config
|
|
echo set pass [pw] Set IPMI password to default config
|
|
echo.
|
|
|
|
:: if enable plugin, show the help info here
|
|
if defined PluginBat (
|
|
%PluginBat% --help
|
|
)
|
|
exit /b 1
|
|
|
|
endlocal |