0

I'm using batch and I need to process tracert output to grab only IP Addresses. How can I do this?

I can say that the for command is used and thankfully I have a little experience with for.

Since we're obviously going to use delimiters, and since there's the ms word with the IP next to it, shouldn't I be doing

for /f "tokens=2 delims=ms" %%a in ('tracert google.com') do echo set %%a=ip&echo ip>ips.txt

Since ms is delimiter, the IP addresses should be processed, right?

Instead, I get this really weird output:

img

BTW, this is how it looks like when I do the traditional tracert:

enter image description here

Matthew Woo
  • 1,288
  • 15
  • 28
Voltaire
  • 93
  • 2
  • 9
  • Could you please try to rephrase your question in coding for a batch file since that will make it easier for us to understand and help you. – Monacraft Feb 20 '14 at 11:04

1 Answers1

0

1 - Why all the lines? The command is executed on command line, echo is on and for each execution of the for loop, a line with the command to execute is echoed to console and as this line executes an echo command, the result is also echoed.

2 - Why the data retrieved is not correct?

  • tracert returns more lines than that with ip addresses, and you are not filtering them

  • delims clause accepts a sequence of single characters that are used as delimiters, not complete words. Anyway, it could work, as all the m and s characters are considered delimiters, but

  • the ip address in the output of the tracert command is not the second token in your output, so for each of the lines you are not retrieving the adecuated part and

  • for each line you execute a set command, but

    a) you do not need it to echo the ip address to a file and

    b) the set command should be written in the inverse order set var=value (or maybe i'm not understanding what you are doing, sorry in this case), so it should be set ip=%a

    c) Assuming all the previous is corrected, it will still not work. The value of the ip variable is changed inside a same line/block where you are trying to echo it. But the read of the variable has been eliminated (all reads replaced with values of variables before start execution of the line) by the parser when the line of code was reached. Delayed expansion is needed in this case

3 - How to solve?

For a direct command line solution (as in your question) this should do the trick

(@for /f "tokens=8" %a in ('tracert -4 -d 10.10.10.10^|find "ms"') do @echo %a)>ips.txt

The result of the tracert is filterted for only the lines with ms in them. The tracert is executed only in ipv4 and without name resolution to get a consistent number of columns in the output, the eigth colum/token is selected (where the ip address is shown) and directly the value in the variable is echoed. All is send to a file and to avoid the inclusion of the commands executed @command is used (equivalent for a echo off for a single command)

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Ahhh!! EIGHT tokens? Can you explain why? As I've said to you a while ago while using my Dark Arts, I just understood `for`. Please elaborate? – Voltaire Feb 20 '14 at 11:25
  • @EpicTonic, just splitting on spaces, the lines with valid data on them have `number,time,ms,time,ms,time,ms,ip`, so, the ip field is the eight in the list. – MC ND Feb 20 '14 at 11:28
  • @EpicTonic, the `tokens=8` is not asking for the number (total) of tokens, but is enumerating the token number in the line that should be processed. In this case only one token, the eight one in the line after splitting using the delimiter characters. – MC ND Feb 20 '14 at 11:30
  • Okay, my knowledge about delimiters just shattered. It doesn't process `ms` correctly, or? – Voltaire Feb 20 '14 at 11:31
  • @EpicTonic, as said, `delims=ms` is saying the character `m` is used as delimiter and the character `s` is also used as delimiter. With this clause, the token clause should be `tokens=4` (there are 3 ms strings separating data), so ip is the fourth value. BUT the value in `%a` will also hold the spaces between the `ms` and the ip address unless you also include the spaces in the delims clause ( `... delims=ms "`), but then, you should use `tokens=5` (the data before the first ms is now splitted in two) – MC ND Feb 20 '14 at 11:37