1

I need to run ping with -t parameter and process every line in bat script. I need to do this in real time.

This code explains my problem. Commant 'ping -t 8.8.8.8' prints nothing to for loop so nothing is printed to console. If I use 'ping -n 10 8.8.8.8' all lines are printed at the same time when ping exits.

How can I get printed lines in real into for loop?

@echo off
Setlocal EnableDelayedExpansion
FOR /f "TOKENS=*" %%i in ('ping -t 8.8.8.8') do (
 set LINE=%%i
 echo !LINE!
)
KK Ari
  • 11
  • 1
  • 3
  • I don't know enough about batch file scripting but I'm guessing you'll need to use `ping -n 1` to send and handle one ping at a time. – DerfK Jul 27 '16 at 15:17
  • I think I'm going to replace default ping.exe with another ping tool which displays jitter. With -n 1 jitter calculation won't work and would need additional timer for pingin every 1000 milliseconds. – KK Ari Jul 27 '16 at 15:30
  • Why does it have to be batch? What are you actually trying to accomplish with this script? – Ryan Bolger Jul 27 '16 at 17:28
  • Batch is what I though would be best because I want to run it in Windows. I wanna calculate all sort of things from ping output. I think it could be good enough to run 'ping -n 30 8.8.8.8' over and over again and use some sort of 'delay.exe 1000ms'. Not what I wanted but almost real time. – KK Ari Jul 27 '16 at 18:30

1 Answers1

1

As specif iced in the comments; the FOR command can not process ongoing output; it handles a set of data once the process terminates.

You will need to produce only one set of output per execution of ping to be able to capture each result. You also need to limit the result set to just the ping result and ignore the other lines.

The find command will only return results that contain the address pinged (need to use an IP here)

The SKIP option will bypass the first line returned by Ping with a reference to the IP address.

In the body of the For command you will store the tokens into temp variables for processing outside of the For loop.

Goto :l_Break_Loop will for the loop to only accept the one line of output from the Ping command and jump to the data processing section.

Afterwards, Goto :l_Start_Loop will direct the batch file to the top of the loop for another row of ping data.

@echo off
Setlocal EnableDelayedExpansion
:l_Start_Loop
FOR /f "SKIP=1 TOKENS=*" %%i in ('ping -n 1 8.8.8.8 2^>&1 ^| Find /i "8.8.8.8"') do (
 set LINE=%%i
 echo !LINE!
 Goto :l_Break_Loop
)
:l_Break_Loop
REM Start processing data retrieved from ping result

REM End processing data retrieved from ping result
Goto :l_Start_Loop

You can use timer functions to impose a delay between ping attempts, and with some cleaver manipulation of the %TIME% variable you can even get the ping command to run at regular intervals as long as the delay is longer than the time to ping and process the results.

%TIME:~-8,2% returns minutes
%TIME:~-5,2% returns seconds
%TIME:~-2% returns hundredths of a second

CoveGeek
  • 186
  • 1
  • 7