How to set variable in dos batch file that show only result - batch-file

how to display only status of ping result like this- here is "My serverx is...fixed only change the online / offline status.
** My server1 is .... online
** My server2 is .... online
** My server3 is .... offline
i was try to this..but fail
#echo off
ping My server1|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 echo Success
IF ERRORLEVEL 1 echo Fail
ping My server2|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 echo Success
IF ERRORLEVEL 1 echo Fail
ping My server3|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 echo Success
IF ERRORLEVEL 1 echo Fail
echo My server1 is ....%ver%
echo My server2 is ....%ver%
echo My server3 is ....%ver%
how to set variable here to do like this.
thanks.

You can do it with one line per server this way:
ping -n 1 server1 | find "TTL=" >NUL && echo server1 is online. || echo server1 is offline.
ping -n 1 server2 | find "TTL=" >NUL && echo server2 is online. || echo server2 is offline.
ping -n 1 server3 | find "TTL=" >NUL && echo server3 is online. || echo server3 is offline.
It's basically shorthand for
ping -n 1 server1 | find "TTL=" >NUL
if not errorlevel 1 (
echo server1 is online.
) else (
echo server1 is offline.
)
... etc. If your hosts you're pinging are sequential, you could use perhaps even less code by employing a for /L loop or similar, something like this:
for /L %%I in (1,1,254) do (
ping -n 1 192.168.0.%%I | find "TTL=" >NUL && echo 192.168.0.%%I is online. || echo 192.168.0.%%I is offline.
)
Type help for in a cmd console for more information about for /L.
Edit: See MC ND's comment immediately below regarding trusting ping to set the errorlevel. Even the example in the question above looking for Reply from can result in a false success. Using find "TTL=" to set errorlevel is more reliable.
For more info on conditional execution, read this.

Related

Ping Batch file with color indicator and log to file

i have script
echo off & cls
TITLE = PING KE google.com
:top
ping -n 1 google.com | FIND "TTL="
IF ERRORLEVEL 1 (SET OUT=4F & echo Request timed out.) ELSE (SET OUT=2F)
color %OUT%
ping -n 3 -w 500 127.0.0.1 >nul
GoTo top
i waant to add log file. any body help me? how to add log file in my script?
thank's for help
If I am understaning correctly, this is what you want?
Just change which lines you want to log.
echo off & cls
TITLE = PING KE google.com
:top
ping -n 1 google.com | FIND "TTL=" >>Logfile.txt
IF ERRORLEVEL 1 (SET OUT=4F & echo Request timed out.) ELSE (SET OUT=2F)
color %OUT%
echo Color set to %OUT% >>Logfile.txt
ping -n 3 -w 500 127.0.0.1 >>Logfile.txt
GoTo top

Automated Ping Test error

I am trying to create an automated ping test using a .cmd file that pulls the ip addresses from a file called computers. It looks like it works for the most part but all the ping test after the first come back negative or dead.
#echo off
set fnm=C:\Users\jelliott\Desktop\computers.txt
set lnm=C:\Users\jelliott\Desktop\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%
I don't know if this is the source of the problem but the errorlevel after a ping request is not reliable: it is not always set on 0 after a successful ping. The most reliable way to check a successful ping is to search for "TTL=" in the output of the ping command. The output of a successful ping always contains the TTL of the ping answer. In other cases, the output won't conain "TTL=".
Check the errorlevel of this command instead:
ping -n 1 %1 | find "TTL="
find allows to search for a literal string in some text. It will set the errorlevel on 0 if it is found and 1 otherwise. You can also use findstr instead of find.
EDIT: This is only in case the addresses you're pinging to are IPv4. For IPv6 addresses, the errorlevel is set correctly. For IPv6 no TTL is printed so the errorlevel must be used to check if the ping succeeded or not. [Here] you can find a general way to check if the ping was successful or not (either it is IPv4 or IPv6). Thank you MC ND for the remark and the link. If you ever want to force IPv4 or IPv6, you can use respectively the -4 or the -6 flags of ping.

Batch script to ping remote computer every 5 seconds?

I'm trying to create a script that pings my remote computer like every 5 or 10 seconds to see if it is back online after rebooting.
I have this code that seems to work but spams like crazy, and I only need it to check every 5 seconds or so.
A bonus to it would be if it stops the loop once it gets a successful connection.
#echo off
:loop
echo Checking connection...
ping -n 1 xx.xxx.xxx.xx >nul
if errorlevel 1 (
cls
echo Computer is offline
goto loop>nul
)
cls
echo Computer is online
goto loop>nul
#echo off
:loop
ping -n 1 -w 500 -4 xx.xx.xx.xx 2>nul|find "TTL=" >nul||(echo offline & ping -w 1000 -n 6 localhost >nul & goto loop)
echo online
Below would be the fine solution for your issue, i guess. try this script and let me know if tat works.
#!/bin/ksh
pingSuccess=0
while [ 1 -eq 1 ]
do
ping -c 1 $1 >/dev/null 2>&1 && pingSuccess=1
[ ${pingSuccess} -eq 1 ] && echo "got the successful ping. " && break
echo "ping failed. sleeping for 5 secs" && sleep 5
done
echo "exitting.."; exit 0
Did not test, but this should work:
#echo off
SET IP=xxx.xxx.xxx.xxx
SET TIMEOUT=5000
:loop
echo Checking connection...
ping -n 1 %IP% >nul
if errorlevel not 0 (
cls
echo Computer at %IP% is offline, waiting %TIMEOUT%ms before retrying
ping -n 1 1.0.0.1 -w %TIMEOUT% >nul
goto loop>nul
)
echo Computer at %IP% is online
you can change
SET IP=xxx.xxx.xxx.xxx
to:
SET IP=%1
and give the IP address as argument to the batch file.
Also, make sure IP 1.0.0.1 is not reachable, it is used to create the timeout.
Here is how I would do it.
#echo off
setlocal
:pingagain
set /a inc+=5
set "ip=xx.xx.xx.xx"
Call :IsPingable %ip% && (
Echo %ip% is online
) || (
Echo %ip% is not online yet.
ping 127.0.0.1 -n 1 -w 5000>nul
goto :pingagain)
exit /b
:IsPingable <comp>
ping -n 1 -w 3000 -4 -l 8 "%~1" | Find "TTL=">nul
exit /b

Batch Script - Ping Address - Write to file if failure

I want to ping an IP address every 5 seconds.
If the ping fails, write the date and time to a file.
Here is my non-working attempt... the loop works as intended, but I can't get it to write to a file if the ping fails.
#ECHO OFF
set IPADDRESS=172.30.1.36
set INTERVAL=5
:PINGINTERVAL
ping %IPADDRESS% -n 1
if errorlevel 1 echo %date% %time% >> failurelog.txt
timeout %INTERVAL%
GOTO PINGINTERVAL
In ipv4, ping command only raises errorlevel if there are packets lost. But in you are pinging a machine in your same subnet, you get no packets lost.
The easier way to test for ping success is to test for the "TTL=" string in the output of the ping
ping -n 1 %ipaddress% | find "TTL=" > nul
if errorlevel 1 echo %date% %time% >> failurelog.txt
This will ping with no intervals until connection is lost. When it does, it records the failed ping "time and date" on a file to the Desktop. Then, it tries to ping again in 1 second and so on until connection is reestablished, at which point it stops, until next ping is missed. Copy code, change the "set IPADDRESS", and save as a .bat file. Good luck
#ECHO OFF
set IPADDRESS=172.16.100.30
set INTERVAL=1
:PINGINTERVAL
ping -n 1 %ipaddress% | find "TTL=" >nul
if errorlevel 1 (
echo %date% %time% >> C:\Users\%username%\Desktop\failping.txt
echo %date% %time%
timeout %INTERVAL% >nul
)
GOTO PINGINTERVAL

Ping Test Using Bat File - Trouble with errorlevel

I am working on setting up a LAN ping test using a batch file. The code i have works great for websites but it acts strange for local IPs. I am running the ping test on 3 computers that i know the IPs of. No matter which one i unplug, when i run the code below, the %errorlevel% is always 0 on all three computers. It never equals to 1 like it does on a website. How can i resolve this?
#echo off
cls
Set IPaddress=www.google.com
PING %IPaddress% -n 1
call :PingTest
Set IPaddress=www.yahoo.com
PING %IPaddress% -n 1
call :PingTest
Set IPaddress=www.unabletoping.com
PING %IPaddress% -n 1
call :PingTest
pause > null
exit
:PingTest
IF %errorlevel% EQU 1 (echo "Server is Offline") else (GOTO:EOF)
When you ping an non accesible address in your subnet, you get an "unreachable" answer, with 1 packet sent, 1 packed received, 0 packets lost. Errorlevel is not set.
When you ping an non accesible address out of your subnet, you get a "timeout" answer, with 1 packet sent, 0 packet received, 1 packet lost. Errorlevel is set.
And, you can ping an active machine, lost packets and get an errorlevel
And, you can ping an active/inactive machine, get TTL expired and get no errorlevel
Better, check for content of ping response.
ping -n 1 192.168.1.1 | find "TTL=" >nul
if errorlevel 1 (
echo host not reachable
) else (
echo host reachable
)
While I cannot replicate your issue, I do have a few recommendations for your script. (See my comment for questions regarding the issue)
When creating variables encapsulate the scope. setlocal and endlocal
When exiting a script, use the /b flag as to not kill the caller's command prompt.
nul not null.
Example ():
#echo off
setlocal
cls
set "IPaddress=www.google.com"
call :PingVerbose "%IPaddress%"
call :PingVerbose "www.yahoo.com"
call :PingVerbose "www.microsoft.com"
pause>nul
endlocal
exit /b 0
:Ping <Address>
ping "%~1" -n 1 >nul
exit /b %ErrorLevel%
:PingVerbose <Address>
call :Ping %1 && echo %~1 is Online || echo %~1 is Offline
exit /b %ErrorLevel%
Though I also cannot replicate your issue, and too have a suggestion to better your script -
#echo off & cls
set addresses=10.1.1.666 10.124.412.14 10.7.254.1
for %%a in (%addresses%) do ping %%a -n 1 > nul || echo %%a is offline
Note that the command after || will only be executed if an error level is set by the ping.
Taking what others have mentioned, I wanted to show how one may need to do everything shown above plus the use of modified variables such as a counter in a loop.
Note: using "setlocal enabledelayedexpansion" allows the use of modified variables in a loop etc..
#echo off
setlocal enabledelayedexpansion
REM List of systems to check
set list=www.google.com www.yahoo.com www.notasite.com
set /a failed=0
set /a passed=0
set /a count=0
echo PingTest Servers on %list% :
(for %%a in (%list%) do (
set /a "count+=1"
call :PingVerbose %%a && set /a "passed=!passed!+1" || set /a "failed=!failed!+1"
))
echo ------------
echo Result: %passed% of %count% systems are pingable
pause
endlocal
exit /b 0
:Ping <Address>
ping "%~1" -n 1 >NUL
exit /b %ErrorLevel%
:PingVerbose <Address>
call :Ping %1 && echo %~1 - [ONLINE] || echo %~1 - [OFFLINE]
exit /b %ErrorLevel%
Output:
PingTest Servers on www.google.com www.yahoo.com www.notasite.com :
www.google.com - [ONLINE]
www.yahoo.com - [ONLINE]
www.notasite.com - [OFFLINE]
------------
Result: 2 of 3 systems are pingable
Press any key to continue . . .

Resources