bash 解析来自 traceroute 命令的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1183625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:04:58  来源:igfitidea点击:

Parsing data from traceroute command

bashshelltraceroute

提问by John Kugelman

I am trying to parse the "number of hops" value from the traceroutecommand and output it with a bash script.

我试图从traceroute命令中解析“跳数”值并用 bash 脚本输出它。

Any hints? Very new so thanks.

任何提示?很新,谢谢。

My script looks like this so far:

到目前为止,我的脚本如下所示:

#!/bin/bash
#parse traceroute for hops and output to stdout.
echo -n "Enter a host name(like www.google.com):  "
read hostname
traceroute 2>&1 $hostname|grep "hops"
echo "number of hops are "

回答by John Kugelman

Are you looking for the number of hops it took, or the "64 hops max" value? If it's the former, use tailto get the last line of output, then awkto print the first column:

您是在寻找所用的跳数,还是“最大 64 跳”值?如果是前者,使用tail获取输出的最后一行,然后awk打印第一列:

traceroute "$hostname" 2>/dev/null | tail -1 | awk '{print }'

回答by Alex Martelli

The only occurrence of "hops" in traceroute output (stderr, actually) is towards the top, in the stderr "header" line:

traceroute 输出(实际上是标准错误)中唯一出现的“跳跃”是在标准错误“标题”行中的顶部:

traceroute to foo.bar.com (123.12.1.23), 30 hops max, 40 byte packets

or the like -- if that's the one line you want to grab, you might grep for "hops max,"or the like in order to reduce the risk of undesired hits (should some intermediate host just happen to have "hops" in their DNS).

或类似的——如果这是您想要获取的一行,您可能会使用 grep"hops max,"或类似的内容来降低意外命中的风险(如果某些中间主机碰巧在其 DNS 中具有“跃点”)。

Is this what you mean? Is your problem grabbing this 30, the maximum number of hops, into a bash variable? If that's so, then

你是这个意思吗?您的问题是将这 30 个(最大跃点数)抓取到 bash 变量中吗?如果是这样,那么

maxhops=`traceroute www.yahoo.com 2>&1 >/dev/null | grep "hops max" | cut -d' ' -f5`

should help -- you can use $maxhopsafter this (and it will be 30 in the above example).

应该有帮助——你可以$maxhops在这之后使用(在上面的例子中它是 30)。

But I suspect you mean something completely different -- clarify, maybe...?

但我怀疑你的意思完全不同——澄清一下,也许......?

Edit: the OP has clarified they want, not maxhops, but the actual number of hops as measured by traceroute; for that purpose,

编辑:OP 已经澄清他们想要的不是 maxhops,而是由 traceroute 测量的实际跳数;为了这个目的,

numhops=`traceroute www.yahoo.com 2>/dev/null | tail -1 | cut -d' ' -f2`

or a simpler

或者更简单的

numhops=`traceroute www.yahoo.com | wc -l`

should work fine!

应该工作正常!

If you do want anything fancier (or more than one result, etc) then awk as suggested in other answers is perfectly fine, but a pipeline mix of grep, tail, and cut, is quite a traditional unix-y approach, and still quite workable for simple cases (and some complex ones, but awk or perl may be more appropriate for those;-).

如果您确实想要更高级的东西(或多个结果等),那么其他答案中建议的 awk 非常好,但是 grep、tail 和 cut 的管道混合是一种非常传统的 unix-y 方法,并且仍然非常适用于简单的情况(和一些复杂的情况,但 awk 或 perl 可能更适合那些;-)。

回答by ghostdog74

traceroute www.google.com 2>/dev/null | awk 'NR==1{print ;exit}'

回答by ghostdog74

another way, where those hops not reachable (for lack of better word) are not counted

另一种方式,那些不可达的跳数(因为没有更好的词)不计算在内

# traceroute www.yahoo.com 2>/dev/null | awk 'NR>1 && !/* * */{c++}END{print c}'
  16