需要批处理命令获取mac地址并在txt文件上进行比较吗?

时间:2018-10-23 02:35:04

标签: batch-file

如果mac不在list.txt上,如何获取PC的Mac地址并重新启动PC?我只有这个get mac命令,

for /f "tokens=3 delims=," %%a in ('"getmac /v /fo csv | findstr Ethernet"') do set MAC=%%a 
echo MAC address of this computer is %MAC%

1 个答案:

答案 0 :(得分:0)

  1. 您使用getmac,并将结果通过findstr传递给所需的网络适配器进行过滤。
    • 您将结果存储到变量ThisPCMAC
  2. 您使用type命令来获取通过list.txt传递的findstr文件的内容,以对ThisPCMAC进行过滤。
    • 您将结果存储到变量FoundMAC中。
  3. 如果定义了FoundMAC,则您goto :norestart
  4. 如果未定义FoundMAC,则您goto :restart
  5. :restart中,您以必需的附加参数调用shutdown /r
  6. 如果输入有误,您可以在指定的时间(此处为10分钟,请参阅shutdown /a)致电/t 600
  7. 有关更多帮助,请参见shutdown /?

这两个文件应位于同一目录中。 list.txt的示例内容:

FF-AA-BB-CC-DD-FA
FF-AA-BB-CC-DD-FB
FF-AA-BB-CC-DD-FC

RestartIfThisPCMACnotInList.bat的内容:

@echo off

set ScriptPath=%~dp0
set ThisPCMAC=
set FoundMAC=

echo.
echo ScriptPath = %ScriptPath%


for /f "tokens=3 delims=," %%a in ('"getmac /v /fo csv | findstr Ethernet"') do set ThisPCMAC=%%a
echo.
echo MAC address of this computer is %ThisPCMAC%

for /F "usebackq delims==" %%b in (`"type %ScriptPath%list.txt | findstr %ThisPCMAC%"`) do set FoundMAC=%%b

if DEFINED FoundMAC (
   goto :norestart
) else (
   goto :restart
)


:norestart
echo.
echo Found %FoundMAC% in %ScriptPath%list.txt: Nothing to do.
goto :end


:restart
echo.
echo %ThisPCMAC% not found in %ScriptPath%list.txt: Restarting...
echo.
echo shutdown /r /f /t 600 /d p:00:00
shutdown /r /f /t 600 /d p:00:00
echo.
echo Cancel restart with the following command:
echo    shutdown /a
goto :end


:end
echo.
echo %~fp0 ended.
pause

:norestart的示例输出:

C:\test\>RestartIfThisPCMACnotInList.bat

ScriptPath = C:\test\

MAC address of this computer is "FF-AA-BB-CC-DD-FA"

Found FF-AA-BB-CC-DD-FA in C:\test\list.txt: Nothing to do.

C:\test\RestartIfThisPCMACnotInList.bat ended.
Press any key to continue . . .

:restart的示例输出:

C:\test\>RestartIfThisPCMACnotInList.bat

ScriptPath = C:\test\

MAC address of this computer is "FF-AA-BB-CC-DD-FD"

"FF-AA-BB-CC-DD-FD" not found in C:\test\list.txt: Restarting...

shutdown /r /f /t 600 /d p:00:00

Cancel restart with the following command:
   shutdown /a

C:\test\RestartIfThisPCMACnotInList.bat ended.
Press any key to continue . . .