Computing.Net > Forums > Programming > Batch file ping.exe errorlevel?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file ping.exe errorlevel?

Reply to Message Icon

Name: tonysathre
Date: August 21, 2006 at 15:32:28 Pacific
OS: Windows XP Pro SP2
CPU/Ram: P4 2.53 Ghz\ 768 MB
Product: Dell Dimension 4550
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: tonysathre
Date: August 21, 2006 at 17:02:52 Pacific
Reply:

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


0

Response Number 2
Name: Mechanix2Go
Date: August 22, 2006 at 00:00:31 Pacific
Reply:

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



0

Response Number 3
Name: tonysathre
Date: August 22, 2006 at 10:44:57 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file ping.exe errorlevel?

batch files , ping results www.computing.net/answers/programming/batch-files-ping-results/14790.html

Batch file, ping results www.computing.net/answers/programming/batch-file-ping-results/14033.html

Batch File - Ping > Send Command www.computing.net/answers/programming/batch-file-ping-send-command/16434.html