批处理-解析Tracert的输出 [英] Batch - Parsing the output of Tracert
本文介绍了批处理-解析Tracert的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想向Windows中的tracert输出寻求帮助,即我有以下输出:
i would like to ask for some help with the output of tracert in windows, ie i have this output :
Tracing route to Y.Y.Y.Y over a maximum of 30 hops
1 1 ms 1 ms 1 ms X.X.X.X
2 103 ms 71 ms 22 ms X.X.X.X
3 35 ms 51 ms 35 ms X.X.X.X
....
并且我想生成一个仅包含X.X.X.X的文件,或者作为到达该文件的中间步骤,而仅包含实际包含IP的示踪剂行.即:
and i would like to produce a file that contains only the X.X.X.X or as an intermediate step to get there, only the lines of tracer that actually contain IPs. Ie :
X.X.X.X
X.X.X.X
X.X.X.X
我已经通过批处理文件尝试过此操作:
I ve tried this through a batch file :
for /f "tokens=8" %%a in ('tracert -4 -d 8.8.8.8^|find "ms"') do (
@echo %%a >D:\panagos\desktop\ips.txt
)
但是我得到的不是期望的输出:
but instead of the desired output i get only :
Y.Y.Y.Y
我也尝试从cygwin调用二进制文件来执行此操作,即:
I ve also tried calling on binaries from cygwin to do this, ie :
D:\path\to\slash\bin\awk '{ print $8 }' filein > fileout
但是那也不起作用.有人可以帮忙吗?预先感谢.
but that doesnt work either. Can anyone help? Thanks in advance.
推荐答案
使用以下批处理文件:
GetIPs.cmd:
@echo off
rem skip 2 header lines
rem ip address is the 8th token
for /f "skip=2 tokens=8" %%d in ('tracert -4 -d 8.8.8.8') do (
echo %%d
)>>ips.txt
endlocal
示例:
F:\test>tracert -4 -d 8.8.8.8
Tracing route to 8.8.8.8 over a maximum of 30 hops
1 <1 ms <1 ms <1 ms 192.168.42.129
2 * * * Request timed out.
3 53 ms 48 ms 48 ms 10.248.29.129
4 46 ms 48 ms 48 ms 10.247.82.25
5 55 ms 48 ms 48 ms 10.247.82.6
6 55 ms 48 ms 48 ms 10.247.82.9
7 46 ms 48 ms 48 ms 10.247.82.18
8 55 ms 48 ms 48 ms 87.237.20.236
9 56 ms 59 ms 48 ms 87.237.20.85
10 56 ms 58 ms 47 ms 74.125.52.216
11 55 ms 48 ms 51 ms 216.239.41.179
12 58 ms 48 ms 59 ms 216.239.57.83
13 58 ms 59 ms 48 ms 8.8.8.8
Trace complete.
F:\test>GetIPs
F:\test>type ips.txt
192.168.42.129
10.248.29.129
10.247.82.25
10.247.82.6
10.247.82.9
10.247.82.18
87.237.20.236
87.237.20.85
74.125.52.216
216.239.41.179
216.239.57.83
8.8.8.8
进一步阅读
- Windows CMD命令行的AZ索引-与Windows cmd行相关的所有内容的绝佳参考.
- 用于/f -根据另一个命令的结果循环命令.
- 重定向-重定向运算符.
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- for /f - Loop command against the results of another command.
- redirection - Redirection operators.
Further Reading
这篇关于批处理-解析Tracert的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文