I'm writing a program that uses the "tracert" command to find traceroutes for IP addresses, which I'll then use to do some things with.
How I had planned to do this is, is by dumping the output for all IP addresses into a text file by appending with >>output.txt
and then reading it in with Python or something and turn it into something usable.
My issue with this is that tracert also shows in its output some things that are of no use to me, such as the first line that says where it's tracing to and what the maximum number of hops is, or the last line that only says: "Trace complete".
The thing is that I'm going to be tracing 3500+ IP addresses, and that number is only going to grow, so that is a whole lot of text that I'm printing in a file that is absolutely useless.
That's why my question is: Is there any way that I can modify the output of the command before saving it, in a batch file?
The command I'm running:
tracert -h 30 -w 500 XX.XXX.XXX.XX >>cmd_output.txt
The output it gives:
Tracing route to XYZ.net
[XX.XXX.XXX.XX]
over a maximum of 30 hops:
1 41 ms 18 ms 2 ms text.net [XXX.XXX.XXX.X]
2 2 ms 1 ms 1 ms text.net [XX.XXX.XXX.XXX]
3 1 ms 1 ms 1 ms text.net [XX.XXX.XXX.XXX]
4 5 ms 5 ms 5 ms text.net [XXX.XX.XX.XXX]
5 5 ms 5 ms 5 ms text.net [XXX.XX.XX.X]
6 25 ms 25 ms 25 ms XYZ.net [XX.XXX.XXX.XX]
Trace complete.
Using your replies and some more searching, I have come up with this solution:
This yields the following response:
In each line, before the first comma is always the hop-number. Then the IP address may be after the first, second or third comma. Since tracert sends three requests per hop one, two or three of those requests can fail.
If a request fails, the result is "*", instead of "X ms" and because I'm delimiting with spaces, if I would always take the 8th token, it might not contain the IP address, or anything for that matter.
This code will always contain the IP address, but will still contain some superfluous data if fewer then two requests failed, because then token 6 and or 7 will contain some part of "X ms".
In the response, you'll see all requests were successful, except for hop 10, where two requests failed and hop 12, where three requests failed and thus tracert failed to return an IP address.
If anyone has any bright ideas on how to remove these last bits of superfluous data, it would be appreciated. Otherwise this is a perfectly workable output for me. So thanks for your help! :)