我想请求一些关于windows中tracert输出的帮助,即我有这个输出:
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的跟踪行。即:
X.X.X.X
X.X.X.X
X.X.X.X
我通过批处理文件尝试了这个:
for /f "tokens=8" %%a in ('tracert -4 -d 8.8.8.8^|find "ms"') do (
@echo %%a >D:\panagos\desktop\ips.txt
)
但不是所需的输出我只得到:
Y.Y.Y.Y
我也曾尝试从cygwin调用二进制文件来执行此操作,即:
D:\path\to\slash\bin\awk '{ print $8 }' filein > fileout
但这也不起作用。有人可以帮忙吗?提前谢谢。
答案 0 :(得分:3)
阅读Redirection:
command > filename Redirect command output to a file command >> filename APPEND into a file
使用
type NUL >D:\panagos\desktop\ips.txt
for /f "tokens=8" %%a in ('tracert -4 -d 8.8.8.8^|find "ms"') do (
@echo %%a >>D:\panagos\desktop\ips.txt
)
或(更好)
@echo OFF
>D:\panagos\desktop\ips.txt (
for /f "tokens=8" %%a in ('tracert -4 -d 8.8.8.8^|find "ms"') do (
echo %%a
)
)
另请注意,您的脚本会隐藏无法访问的主机,请参阅下一个示例中的第12跳只有7个令牌:
==> tracert -d 8.8.8.8
Tracing route to 8.8.8.8 over a maximum of 30 hops
…
11 36 ms 44 ms 36 ms 108.170.234.149
12 * * * Request timed out.
13 36 ms 36 ms 35 ms 8.8.8.8
Trace complete.
答案 1 :(得分:3)
使用以下批处理文件:
<强> 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