处理"Tracert".IP地址输出(批量) [英] Process "Tracert" output for IP addresses (Batch)
问题描述
我正在使用批处理,我需要处理 tracert
输出以仅获取仅IP地址.我该怎么办?
I'm using batch and I need to process tracert
output to grab only IP Addresses. How can I do this?
我可以说使用了 for
命令,幸运的是,我对 for
有一点经验.
I can say that the for
command is used and thankfully I have a little experience with for
.
因为我们显然要使用定界符,并且由于旁边有IP的 ms
字,所以我不应该这样做
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
因为 ms
是分隔符,所以应该处理IP地址,对吧?
Since ms
is delimiter, the IP addresses should be processed, right?
相反,我得到了这个非常奇怪的输出:
Instead, I get this really weird output:
顺便说一句,这就是我做传统的 tracert
时的样子:
BTW, this is how it looks like when I do the traditional tracert
:
推荐答案
1-为什么所有行?该命令在命令行上执行,并且echo处于打开状态,并且对于for循环的每次执行,都会将包含要执行的命令的行回显到控制台,并且在此行执行echo命令时,也会回显结果.
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-为什么检索到的数据不正确?
2 - Why the data retrieved is not correct?
-
tracert返回的行比具有IP地址的行多,并且您没有对其进行过滤
tracert returns more lines than that with ip addresses, and you are not filtering them
delims子句接受一系列用作分隔符(而不是完整单词)的单个字符.无论如何,它都可以工作,因为所有m和s字符都被视为定界符,但是
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
tracert命令的输出中的ip地址不是输出中的第二个标记,因此对于每一行,您都不会检索被引用的部分和
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
对于每行执行一个set命令,但是
for each line you execute a set command, but
a)您不需要它来将IP地址回显到文件中,并且
a) you do not need it to echo the ip address to a file and
b)应该以相反的顺序设置ip =%a
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)假设所有先前的内容均已得到纠正,它将仍然无法使用. ip
变量的值在试图回显它的同一行/块内更改.但是,到达代码行时,解析器消除了对变量的读取(在开始执行该行之前,所有读取均已替换为变量的值).在这种情况下,需要延迟扩展
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-如何解决?
对于直接的命令行解决方案(如您的问题),这应该可以解决问题
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
仅对其中包含 ms
的行过滤出tracert的结果.仅在ipv4中执行tracert,并且不使用名称解析来在输出中获得一致的列数,选择eigth列/令牌(显示ip地址),然后直接回显变量中的值.全部都发送到文件中,并避免包含执行的命令,使用 @command
(相当于单个命令的 echo off
)
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)
这篇关于处理"Tracert".IP地址输出(批量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!