batch-file - 需要批处理命令获取 mac 地址并比较 txt 文件吗?

标签 batch-file

如果mac不在list.txt中,如何获取pc mac地址并重新启动PC?,我只有这个getting 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%

最佳答案

  • 您使用 getmac并将结果通过 findstr过滤所需的网络适配器。
  • 您将结果存储到变量 ThisPCMAC
  • 您使用 type命令获取 list.txt 的内容通过管道传输的文件 findstr过滤 ThisPCMAC .
  • 您将结果存储到变量 FoundMAC .
  • 如果 FoundMAC定义你goto :norestart
  • 如果 FoundMAC没有定义你goto :restart
  • :restart ,您调用shutdown /r带有所需的附加参数
  • 如果有误可以调用shutdown /a在规定的时间内(此处为 10 分钟,见 /t 600)。
  • 如需更多帮助,请参阅 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 . . .
    

    关于batch-file - 需要批处理命令获取 mac 地址并比较 txt 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52940358/

    相关文章:

    java - 为什么我的 Windows 批处理文件在 Java 执行时不被处理?

    windows - 批处理 : files in directory to variable

    如果任何列为空,Windows 批处理文件将删除 csv 文件中的那些行

    batch-file - 批处理文件 - 搜索一个单词并提取变量中的下一个单词

    windows - 使用 forfiles 将输出重定向到文件

    java - 如何使用批处理文件删除文本文件中的前 n 行(在 Windows 上)

    batch-file - 将文件存储在批处理文件中?

    windows - 通过另一个 Windows 服务器上的批处理文件在远程 Windows 服务器上执行批处理文件

    linux - 是否有我可以在 Linux 上编写和运行的 Linux 命令或脚本来调用 Windows DOS 命令提示符?如果是这样,怎么办?

    Windows 批处理 FOR 命令制表符分隔符