Batch file - loop ping - output to file hosts that are up -
i make .bat file loop below:
@echo off /l %%g in (1, 1, 69) ( ping -n 1 192.168.%%g.3 ping -n 1 192.168.%%g.4 )
then through output , send ips replied ping txt file. possible cmd batch file?
@echo off set output=%userprofile%\output.txt if exist "%output%" del "%output%" /l %%g in (1, 1, 69) ( call :ping 192.168.%%g.3 call :ping 192.168.%%g.4 ) goto :eof :ping ping -n 1 %1 >nul && echo %1>>"%output%"
basically, use &&
add command executed if previous command (the 1 before &&
) completed (technically speaking, returned exit code of 0
).
there's similar approach opposite case. if want perform actions on unsuccessful result of command, put ||
after , command implementing action.
edit
one note ping
. notification router host not accessible. in case ping
still exits 0
code ('successful'), because reply, if it's router , not actual host.
if can case hosts , don't want have such false positives in output file, you'll have parse output of ping
keywords indicating whether pinging successful indeed. far can rely on lines showing aggregate stats: appear if reply intended host.
so, here's alternative approach:
@echo off set output=%userprofile%\output.txt if exist "%output%" del "%output%" /l %%g in (1, 1, 69) ( call :ping 192.168.%%g.3 call :ping 192.168.%%g.4 ) goto :eof :ping ping -n 1 %1 | find "approximate round trip" >nul && echo %1>>"%output%"
edit 2
changed both solutions use subroutine call in order avoid premature expansion of %ip%
inside for
loop. (could fixed enabling delayed expansion instead.)
also quoted %output%
everywhere.
Comments
Post a Comment