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
Related
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
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.
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
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 . . .
I want to make a batch file that waits for a few minutes, then executes a command. How would I do this? Specifically, I want it to end another program in 3 minutes after opening the file.
Use timeout. That will let you wait for a number of seconds given in it's /t parameter. timeout /t 180 will sleep for 3 minutes (180 seconds).
TIMEOUT [/T] timeout [/NOBREAK]
Description:
This utility accepts a timeout parameter to wait for the specified
time period (in seconds) or until any key is pressed. It also
accepts a parameter to ignore the key press.
Parameter List:
/T timeout Specifies the number of seconds to wait.
Valid range is -1 to 99999 seconds.
/NOBREAK Ignore key presses and wait specified time.
/? Displays this help message.
NOTE: A timeout value of -1 means to wait indefinitely for a key press.
Examples:
TIMEOUT /?
TIMEOUT /T 10
TIMEOUT /T 300 /NOBREAK
TIMEOUT /T -1
Another method is to ping an invalid IP address for a certain amount of time:
PING 1.1.1.1 -n 1 -w 60000 >NUL
60000 = milliseconds
hi i love stockoverflow but sometimes the answers are too concise... so heres more than you asked for... the timer and many other snipits
#ECHO off
MODE CON:COLS=25 LINES=11
cls
color 0B
:taskkill
echo.
echo Program To Shutdown
echo.
set /p taskkill=
if "%taskkill%" == "%taskkill%" goto taskkillconfirm
exit
:taskkillconfirm
color 0B
:image
set image=/im
if "%image%" == "%image%" goto imageconfirm
exit
:imageconfirm
color 0B
:forced
set forced=/f
if "%forced%" == "%forced%" goto forcedconfirm
exit
:forcedconfirm
cls
:hour
color 0B
echo.
echo. Hours?
echo.
set /p hour=
:min
color 0B
cls
echo.
echo Minutes?
echo.
set /p min=
:sec
color 0B
cls
echo.
echo Seconds?
echo.
set /p sec=
:continue
color 0B
cls
echo %taskkill%
echo Program Shutdown in
echo %hour% Hours
echo %min% Minutes
echo %sec% Seconds
set /a sec="%sec%-1"
if %sec%==-1 set /a min="%min%-1"
if %sec%==-1 set /a sec="59"
if %min%==-1 set /a hour="%hour%-1"
if %min%==-1 set /a min="59"
if %hour%==-1 goto done
ping -n 2 127.0.0.1 >NUL
goto continue
:done
color 0B
cls
taskkill %image% %taskkill% %forced%
exit
hope you really enjoy this answer
Simple, try this
#echo off
echo Do you want to read word file or pdf file? Hit any key for options...
pause >nul
echo w for word
echo p for pdf
::set input =
set /p input = Choose your option
if %input% == w goto w
if %input% == p goto p
:w
echo Reading Word file...
::start /your/path/to/your/Office/winword.exe
/path/to/your/doc/file/filename.doc
echo Timer starts
pause >nul
echo 10
ping localhost -n 2 >nul
cls
echo 9
ping localhost -n 2 >nul
cls
echo 8
ping localhost -n 2 >nul
cls
echo 7
ping localhost -n 2 >nul
cls
echo 6
ping localhost -n 2 >nul
cls
echo 5
ping localhost -n 2 >nul
cls
echo 4
ping localhost -n 2 >nul
cls
echo 3
ping localhost -n 2 >nul
cls
echo 2
ping localhost -n 2 >nul
cls
echo 1
ping localhost -n 2 >nul
cls
echo Time's up. Starting the next document...
::you can copy/paste the above commands to start another word file
:p
echo Reading Pdf file...
echo Timer starts
::start /your/path/to/your/Acrobat/acrord32.exe
/path/to/your/pdf/file/filename.pdf
pause >nul
echo 10
ping localhost -n 2 >nul
cls
echo 9
ping localhost -n 2 >nul
cls
echo 8
ping localhost -n 2 >nul
cls
echo 7
ping localhost -n 2 >nul
cls
echo 6
ping localhost -n 2 >nul
cls
echo 5
ping localhost -n 2 >nul
cls
echo 4
ping localhost -n 2 >nul
cls
echo 3
ping localhost -n 2 >nul
cls
echo 2
ping localhost -n 2 >nul
cls
echo 1
ping localhost -n 2 >nul
cls
echo Times up. Starting the next document...
::you can copy/paste the above commands to start another Pdf file