Reputation: 3511
I have batch file in which i am restarting a machine and want to check if machine is back online then execute the code block else after particular time inform the user the machine is not online I want to check something like:
pingresult = ping \\machinename
if (pingresult == true)
{
execute some task
}
else
{
keep pinging for say 5min. if after 5mins machine is not up show message to user
}
Upvotes: 0
Views: 2873
Reputation: 47614
This will try pinging %machinename% for 100 times with a default timeout of 3 seconds: 3s * 100 = 300s, i.e. 5 minutes.
for /L %%N IN (1, 1, 100) DO (
ping -n 1 %machinename%
if not ERRORLEVEL 1 (
set pingresult=true
goto done
)
)
set pingresult=false
:done
if %pingresult% == true (
echo "ping ok, doing something..."
) else (
echo "no reply after 5 mins, error!"
)
Upvotes: 2