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

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

Errorlevel always returns 0 in a for loop with ping command

问题

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

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

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

@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
)

if errorlevel 1 (
    echo !hostname! !ip! [!domain!] is OFF
) else (
    echo !hostname! !ip! [!domain!] is ON
)

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.

@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
)

if errorlevel 1 (
    echo !hostname! !ip! [!domain!] is OFF
) else (
    echo !hostname! !ip! [!domain!] is ON
)

pause

答案1

得分: 0

我有另一种方法来使用这种ping,因为我的机器是法语。

例如,这个批处理脚本会对一系列`URLs`进行ping,并用不同的颜色显示它们的状态(开启/关闭)。

`URLs` 在`"URLS"`变量中定义。

脚本会循环遍历这些`URLs`,使用`StringFormat`函数对其进行格式化,使用`"ping"`命令获取每个URL的`IP`地址,然后利用`PSColor`函数使用颜色显示每个URL的状态(开启或关闭)。

@echo off
Title Multi-Ping hosts with colors
Set URLS="non-existent-hostname","https://www.google.com","Nothingtotest","https://www.yahoo.com","www.reddit.com",^
"http://www.wikipedia.com","www.stackoverflow.com","www.bing.com","NoBodyHere.com"

setlocal ENABLEDELAYEDEXPANSION
for  %%a in (%URLS%) do (
    Call :StringFormat "%%~a"
    set "ip="
    for /f "tokens=2 delims=[]" %%b in ('ping -n 1 !URL!') do set "ip=%%b"
    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
)
pause & Exit
::---------------------------------------------------------------------------------
:StringFormat <URL>
(   
    echo Function StringReplace(Str^)
    echo     Str = Replace(Str,"http://",""^)
    echo Str = Replace(Str,"https://",""^)
    echo     StringReplace = str
    echo End Function
    echo wscript.echo StringReplace("%~1"^)
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do ( set "URL=%%~a" )
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::---------------------------------------------------------------------------------
:PSColor <String> <Color> <NewLine>
If /I [%3] EQU [\n] (
    Powershell Write-Host "`0%~1" -ForegroundColor %2
) Else (
    Powershell Write-Host "`0%~1" -ForegroundColor %2 -NoNewLine
)
Exit /B
 ::---------------------------------------------------------------------------------
英文:

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 -->

@echo off
Title Multi-Ping hosts with colors
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;,^
&quot;http://www.wikipedia.com&quot;,&quot;www.stackoverflow.com&quot;,&quot;www.bing.com&quot;,&quot;NoBodyHere.com&quot;

setlocal ENABLEDELAYEDEXPANSION
for  %%a in (%URLS%) do (
	Call :StringFormat &quot;%%~a&quot;
	set &quot;ip=&quot;
	for /f &quot;tokens=2 delims=[]&quot; %%b in (&#39;ping -n 1 !URL!&#39;) do set &quot;ip=%%b&quot;
		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
)
pause &amp; Exit
::---------------------------------------------------------------------------------
:StringFormat &lt;URL&gt;
(	
	echo Function StringReplace(Str^)
	echo 	Str = Replace(Str,&quot;http://&quot;,&quot;&quot;^)
	echo	Str = Replace(Str,&quot;https://&quot;,&quot;&quot;^)
	echo 	StringReplace = str
	echo End Function
	echo wscript.echo StringReplace(&quot;%~1&quot;^)
)&gt;&quot;%tmp%\%~n0.vbs&quot;
for /f &quot;delims=&quot; %%a in (&#39;Cscript /nologo &quot;%tmp%\%~n0.vbs&quot;&#39;) do ( set &quot;URL=%%~a&quot; )
If Exist &quot;%tmp%\%~n0.vbs&quot; Del &quot;%tmp%\%~n0.vbs&quot;
exit /b
::---------------------------------------------------------------------------------
:PSColor &lt;String&gt; &lt;Color&gt; &lt;NewLine&gt;
If /I [%3] EQU [\n] (
	Powershell Write-Host &quot;`0%~1&quot; -ForegroundColor %2
) Else (
	Powershell Write-Host &quot;`0%~1&quot; -ForegroundColor %2 -NoNewLine
)
Exit /B
 ::---------------------------------------------------------------------------------

答案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.

@echo off
setlocal ENABLEDELAYEDEXPANSION

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

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

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

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:

确定