Batch file ping.exe errorlevel?

Dell Dimension 4550
August 21, 2006 at 15:32:28
Specs: Windows XP Pro SP2, P4 2.53 Ghz\ 768 MB

I am updating out DHCP config and wrote this script to get the MAC address of a remote computer on the network. It works fine if I leave the errorlevel part out of it, but I want the errorlevel included so it ECHO's that the host was not found, if it wasn't found. I just can't seem to get it to work. Does ping.exe have errorlevels?

Here's the script:

:start
cls
@echo off
title getMAC

echo Clearing ARP Cache . . .
netsh interface ip delete arpcache

echo Enter IP address of remote computer:
set /p ip=

for %%i in (%ip%) do ping -n 1 %%i
cls
if %errorlevel% == 1 (
goto fail
) else (
arp -a | findstr "%%i"
)

:fail
echo Host not found, or not alive . . .

pause
goto start
cls


No matter what, it always ECHO's Host not found, or not alive . . . and doesn't output anything else. The MAC and IP address should be displayed on stdout.

In the script below it

:start
cls
@echo off

title getMAC

echo Clearing ARP Cache . . .
netsh interface ip delete arpcache

echo Enter IP address of remote computer:
set /p ip=

for %%i in (%ip%) do (ping -n 1 %%i
cls
arp -a | findstr "%%i"
)

pause
goto start
cls

The above script works exactly as it is suppossed to.

What's wrong with my first script? Or does ping.exe not have errorlevels at all?

UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus.


See More: Batch file ping.exe errorlevel?

Report •


#1
August 21, 2006 at 17:02:52

I've gotten it to work if the host is found (errorlevel 0), but not if the host isn't found (errorlevel 1).

Here's the revised script:

:start
cls
@echo off

title getMAC

echo Clearing ARP Cache . . .
netsh interface ip delete arpcache

echo Enter IP address of remote computer:
set /p ip=

for %%i in (%ip%) do (ping -n 1 %%i
if %errorlevel% == 1 (
goto fail
) else (
cls
arp -a | findstr "%%i"
)
)

pause
goto start
cls

:fail
echo Host not found, or not alive . . .
goto start


When the errorlevel == 1, it doesn't ECHO Host not found, or not alive . . . It just goes to :start.

Any suggestions?

Thanks,

Tony

login: yes
password: dont have one

password is incorrect

login: yes
password: incorrect


Report •

#2
August 22, 2006 at 00:00:31

Hi Tony,

Try this:

[I dunno arp, but I think you'll get the idea.]

::== checkIP.bat
@echo off
:newIP
set /p ip= ip num ?
if %ip%'==0' goto :eof
ping -n 1 %ip% | find "Reply" > nul
if errorlevel 1 echo no reply && goto :newIP
goto :newIP
:: DONE
Enetring 0 gives you a way out.



=====================================
If at first you don't succeed, you're about average.

M2



Report •

#3
August 22, 2006 at 10:44:57

Alright, I'll give it a try and post back.

Thanks alot,

Tony

login: yes
password: dont have one

password is incorrect

login: yes
password: incorrect


Report •

Related Solutions


Ask Question