I'm making a simple batch script to ping a host and check their connection. This is my code:
@ECHO OFF
@powershell -ExecutionPolicy UnRestricted -Command "(Add-Type -memberDefinition \"[DllImport(\"\"user32.dll\"\")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x,int y,int cx, int xy, uint flagsw);\" -name \"Win32SetWindowPos\" -passThru )::SetWindowPos((Add-Type -memberDefinition \"[DllImport(\"\"Kernel32.dll\"\")] public static extern IntPtr GetConsoleWindow();\" -name \"Win32GetConsoleWindow\" -passThru )::GetConsoleWindow(),-1,0,0,0,0,67)"
title Ping tool
mode con: cols=50 lines=25
:PING
cls & set /p address= [*] Host to ping:
@echo [*] Started pinging at: %time%
title Pinging %address%
if %ERRORLEVEL% == 1 echo Host didn't respond.
ping %address% -t & echo. & pause. & goto :PING
I'm trying to make it so that when I receive an errorcode (Request Timed out) that it'd print "Host didn't respond" instead of the usual. However it does not work.
Normally my ping would output this:
[*] Host to ping: 8.8.8.8
[*] Started pinging at: 13:44:29.05
Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=28ms TTL=52
Reply from 8.8.8.8: bytes=32 time=16ms TTL=52
Request timed out.
Request timed out.
etc.
But I want it to look like this:
[*] Host to ping: 8.8.8.8
[*] Started pinging at: 13:44:29.05
Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=28ms TTL=52
Reply from 8.8.8.8: bytes=32 time=16ms TTL=52
Host didn't respond.
Host didn't respond.
How would I go on doing this?
P.S the second line of the script is a command that I found here, which makes the CMD window be always on top. Very useful!