Errorlevel在使用ping命令的for循环中始终返回0。

huangapple go评论96阅读模式
英文:

Errorlevel always returns 0 in a for loop with ping command

问题

我正在尝试创建一个批处理脚本,该脚本将通过主机名ping一台计算机,将IP和域保存到变量中,并显示状态(ON或OFF)。

如何修改此代码以便正确显示状态?

我的代码总是返回ON,即使计算机实际上是OFF。

  1. @echo off
  2. setlocal ENABLEDELAYEDEXPANSION
  3. set hostname=non-existent-hostname
  4. set domain=UNRESOLVED
  5. set ip=UNRESOLVED
  6. for /f "tokens=2,3 delims= " %%b in ('ping -a -4 -n 1 !hostname! ^| find "Pinging"') do (
  7. set domain=%%b
  8. set ip=%%c
  9. )
  10. if errorlevel 1 (
  11. echo !hostname! !ip! [!domain!] is OFF
  12. ) else (
  13. echo !hostname! !ip! [!domain!] is ON
  14. )
  15. pause

注意:以上是您提供的批处理脚本的翻译部分。

英文:

I am trying to create a batch script that will ping a machine by hostname, save ip and domain to a variable and display the state (ON or OFF).

How can i fix this code so it will display the status correctly?

My code always returns ON even if pc is actually OFF.

  1. @echo off
  2. setlocal ENABLEDELAYEDEXPANSION
  3. set hostname=non-existent-hostname
  4. set domain=UNRESOLVED
  5. set ip=UNRESOLVED
  6. for /f "tokens=2,3 delims= " %%b in ('ping -a -4 -n 1 !hostname! ^| find "Pinging"') do (
  7. set domain=%%b
  8. set ip=%%c
  9. )
  10. if errorlevel 1 (
  11. echo !hostname! !ip! [!domain!] is OFF
  12. ) else (
  13. echo !hostname! !ip! [!domain!] is ON
  14. )
  15. pause

答案1

得分: 0

  1. 我有另一种方法来使用这种ping,因为我的机器是法语。
  2. 例如,这个批处理脚本会对一系列`URLs`进行ping,并用不同的颜色显示它们的状态(开启/关闭)。
  3. `URLs` `"URLS"`变量中定义。
  4. 脚本会循环遍历这些`URLs`,使用`StringFormat`函数对其进行格式化,使用`"ping"`命令获取每个URL`IP`地址,然后利用`PSColor`函数使用颜色显示每个URL的状态(开启或关闭)。
  5. @echo off
  6. Title Multi-Ping hosts with colors
  7. Set URLS="non-existent-hostname","https://www.google.com","Nothingtotest","https://www.yahoo.com","www.reddit.com",^
  8. "http://www.wikipedia.com","www.stackoverflow.com","www.bing.com","NoBodyHere.com"
  9. setlocal ENABLEDELAYEDEXPANSION
  10. for %%a in (%URLS%) do (
  11. Call :StringFormat "%%~a"
  12. set "ip="
  13. for /f "tokens=2 delims=[]" %%b in ('ping -n 1 !URL!') do set "ip=%%b"
  14. ping -n 1 !URL!>nul && set "msg=!URL! - !ip! ON" && Call :PSColor "!msg!" Green \n || set "msg=!URL! - !ip! OFF" && Call :PSColor "!msg!" Red \n
  15. )
  16. pause & Exit
  17. ::---------------------------------------------------------------------------------
  18. :StringFormat <URL>
  19. (
  20. echo Function StringReplace(Str^)
  21. echo Str = Replace(Str,"http://",""^)
  22. echo Str = Replace(Str,"https://",""^)
  23. echo StringReplace = str
  24. echo End Function
  25. echo wscript.echo StringReplace("%~1"^)
  26. )>"%tmp%\%~n0.vbs"
  27. for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do ( set "URL=%%~a" )
  28. If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
  29. exit /b
  30. ::---------------------------------------------------------------------------------
  31. :PSColor <String> <Color> <NewLine>
  32. If /I [%3] EQU [\n] (
  33. Powershell Write-Host "`0%~1" -ForegroundColor %2
  34. ) Else (
  35. Powershell Write-Host "`0%~1" -ForegroundColor %2 -NoNewLine
  36. )
  37. Exit /B
  38. ::---------------------------------------------------------------------------------
英文:

I have another approach using this kind of ping, because my machine is French.

For example, this batch script pings a list of URLs and displays their status (ON/OFF) with different colors.

The URLs are defined in the "URLS" variable.

The script loops through the URLs, formats them using a StringFormat function, gets the IP address of each URL using the "ping" command, and then displays the status of each URL (ON or OFF) with a color using a PSColor function.


<!-- language: lang-bat -->

  1. @echo off
  2. Title Multi-Ping hosts with colors
  3. Set URLS=&quot;non-existent-hostname&quot;,&quot;https://www.google.com&quot;,&quot;Nothingtotest&quot;,&quot;https://www.yahoo.com&quot;,&quot;www.reddit.com&quot;,^
  4. &quot;http://www.wikipedia.com&quot;,&quot;www.stackoverflow.com&quot;,&quot;www.bing.com&quot;,&quot;NoBodyHere.com&quot;
  5. setlocal ENABLEDELAYEDEXPANSION
  6. for %%a in (%URLS%) do (
  7. Call :StringFormat &quot;%%~a&quot;
  8. set &quot;ip=&quot;
  9. for /f &quot;tokens=2 delims=[]&quot; %%b in (&#39;ping -n 1 !URL!&#39;) do set &quot;ip=%%b&quot;
  10. ping -n 1 !URL!&gt;nul &amp;&amp; set &quot;msg=!URL! - !ip! ON&quot; &amp;&amp; Call :PSColor &quot;!msg!&quot; Green \n || set &quot;msg=!URL! - !ip! OFF&quot; &amp;&amp; Call :PSColor &quot;!msg!&quot; Red \n
  11. )
  12. pause &amp; Exit
  13. ::---------------------------------------------------------------------------------
  14. :StringFormat &lt;URL&gt;
  15. (
  16. echo Function StringReplace(Str^)
  17. echo Str = Replace(Str,&quot;http://&quot;,&quot;&quot;^)
  18. echo Str = Replace(Str,&quot;https://&quot;,&quot;&quot;^)
  19. echo StringReplace = str
  20. echo End Function
  21. echo wscript.echo StringReplace(&quot;%~1&quot;^)
  22. )&gt;&quot;%tmp%\%~n0.vbs&quot;
  23. for /f &quot;delims=&quot; %%a in (&#39;Cscript /nologo &quot;%tmp%\%~n0.vbs&quot;&#39;) do ( set &quot;URL=%%~a&quot; )
  24. If Exist &quot;%tmp%\%~n0.vbs&quot; Del &quot;%tmp%\%~n0.vbs&quot;
  25. exit /b
  26. ::---------------------------------------------------------------------------------
  27. :PSColor &lt;String&gt; &lt;Color&gt; &lt;NewLine&gt;
  28. If /I [%3] EQU [\n] (
  29. Powershell Write-Host &quot;`0%~1&quot; -ForegroundColor %2
  30. ) Else (
  31. Powershell Write-Host &quot;`0%~1&quot; -ForegroundColor %2 -NoNewLine
  32. )
  33. Exit /B
  34. ::---------------------------------------------------------------------------------

答案2

得分: 0

我弄清楚了,谢谢大家的提示。

@echo off
setlocal ENABLEDELAYEDEXPANSION

set hostname=non-existent-hostname
set domain=UNRESOLVED
set ip=UNRESOLVED

for /f "tokens=2,3 delims= " %%b in ('ping -a -4 -n 1 !hostname! ^| find "Pinging"') do (
set domain=%%b
set ip=%%c
)

ping -n 1 %hostname% >nul
if %errorlevel% == 0 (
echo %hostname% %ip% [%domain%] is ON
) else (
echo %hostname% %ip% [%domain%] is OFF
)

pause

英文:

I figured it out, thanks everyone for hints.

  1. @echo off
  2. setlocal ENABLEDELAYEDEXPANSION
  3. set hostname=non-existent-hostname
  4. set domain=UNRESOLVED
  5. set ip=UNRESOLVED
  6. for /f &quot;tokens=2,3 delims= &quot; %%b in (&#39;ping -a -4 -n 1 !hostname! ^| find &quot;Pinging&quot;&#39;) do (
  7. set domain=%%b
  8. set ip=%%c
  9. )
  10. ping -n 1 %hostname% &gt;nul
  11. if %errorlevel% == 0 (
  12. echo %hostname% %ip% [%domain%] is ON
  13. ) else (
  14. echo %hostname% %ip% [%domain%] is OFF
  15. )
  16. pause

huangapple
  • 本文由 发表于 2023年2月14日 22:11:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定