ALIVE OR DEAD

This scrip does everything i need it to, but the alive or dead is not specific enough.

@echo off
set fnm=c:\test\ipaddresses.txt
set lnm=c:\scripts\results.txt

if exist %fnm% goto Label1

echo.
echo Cannot find %fnm%
echo.
Pause
goto :eof

:Label1
echo PingTest STARTED on %date% at %time% > %lnm%
echo ================================================= >> %lnm%
echo.
for /f %%i in (%fnm%) do call :Sub %%i
echo.
echo ================================================= >> %lnm%
echo PingTest ENDED on %date% at %time% >> %lnm%
echo ... now exiting
goto :eof

:Sub
echo Testing %1
set state=alive
ping -n 1 %1
if errorlevel 1 set state=dead
echo %1 is %state% >> %lnm%

How it is set now if the reply from the ping is "reply from (ipaddress): destination host unreachable.". It puts this as alive when it needs to be dead. How can i fix this? I have self taught most of the scripting I know so if this is a stupid question I'm sorry. Any help is greatly appreciated.
August 25th, 2015 2:21pm

Don't waste your time learning batch files.  They are obsolete:

$outfile='c:\scripts\results.txt'
$ips=Get-Content c:\test\ipaddresses.txt
"PingTest STARTED on $($datetime]::Now)" | Out-File $outfile
foreach($ip in $ips){
    if(Test-Conenction $ip -count 1 -quiet){
        "$ip is ALIVE" | Out-File $outfile -append
    }else{
        "$ip is DEAD" | Out-File $outfile -append
    }
}
"PingTest ENDED on  $($datetime]::Now)" | Out-File $outfile -append


Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 3:00pm

ping -n 1 %1

if errorlevel 1 set state=dead

Your test is flawed. To make it reliable you must use this code:

ping  %1 | find /i "bytes=" || set state=dead

  • Marked as answer by Ghost762 11 hours 54 minutes ago
  • Unmarked as answer by Ghost762 11 hours 48 minutes ago
August 25th, 2015 3:05pm

Thanks much and I'm starting to see why. Have a good one. Thanks for the speedy reply to all.
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 3:22pm

I actually asked my coworker about it and he didn't know. Then 10 min later he was like OMG use this. Just completely out of nowhere. It was pretty funny. This is what i am using now.

ping -n 1 %1 | find "Reply from %1" || set state=dead
echo %1 is %state% >> %lnm%

Thanks a lot again for the speedy replies guys.

  • Marked as answer by Ghost762 11 hours 48 minutes ago
August 25th, 2015 3:29pm

ping -n 1 %1

if errorlevel 1 set state=dead

Your test is flawed. To make it reliable you must use this code:

ping  %1 | find /i "bytes=" || set state=dead

  • Marked as answer by Ghost762 Tuesday, August 25, 2015 7:17 PM
  • Unmarked as answer by Ghost762 Tuesday, August 25, 2015 7:23 PM
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 6:59pm

I actually asked my coworker about it and he didn't know. Then 10 min later he was like OMG use this. Just completely out of nowhere. It was pretty funny. This is what i am using now.

ping -n 1 %1 | find "Reply from %1" || set state=dead
echo %1 is %state% >> %lnm%

Thanks a lot again for the speedy replies guys.

  • Marked as answer by Ghost762 Tuesday, August 25, 2015 7:23 PM
August 25th, 2015 7:22pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics