Linux 从 ping -c 中提取平均时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9634915/
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-08-06 05:03:41  来源:igfitidea点击:

extract average time from ping -c

linuxbashawkping

提问by creativeDev

I want to extract from the command ping -c 4 www.stackoverflow.com | tail -1| awk '{print $4}' the average time.

我想从命令中提取ping -c 4 www.stackoverflow.com | tail -1| awk '{print $4}' 平均时间。

107.921/108.929/110.394/0.905 ms

Output should be: 108.929

输出应该是: 108.929

采纳答案by Buggabill

One way is to just add a cut to what you have there.

一种方法是在您拥有的内容上添加一个切口。

ping -c 4 www.stackoverflow.com | tail -1| awk '{print }' | cut -d '/' -f 2

回答by raj

ping -c 4 www.stackoverflow.com | tail -1| awk -F '/' '{print $5}'would work fine.

ping -c 4 www.stackoverflow.com | tail -1| awk -F '/' '{print $5}'会工作得很好。

"-F" option is used to specify the field separator.

“-F”选项用于指定字段分隔符。

回答by potong

This might work for you:

这可能对你有用:

ping -c 4 www.stackoverflow.com | sed '$!d;s|.*/\([0-9.]*\)/.*||'

回答by xebeche

The following solution uses Bash only (requires Bash 3):

以下解决方案仅使用 Bash(需要 Bash 3):

[[ $(ping -q -c 4 www.example.com) =~ \ =\ [^/]*/([0-9]+\.[0-9]+).*ms ]] \
&& echo ${BASH_REMATCH[1]}

For the regular expression it's easier to read (and handle) if it is stored in a variable:

对于正则表达式,如果将其存储在变量中,则更容易阅读(和处理):

regex='= [^/]*/([0-9]+\.[0-9]+).*ms'
[[ $(ping -q -c 4 www.example.com) =~ $regex ]] && echo ${BASH_REMATCH[1]}

回答by msanford

Promoting luissquall's very elegent commentto an answer:

宣传luissquall对答案的非常优雅的评论

 ping -c 4 www.stackoverflow.com | awk -F '/' 'END {print }'

回答by xerostomus

Direct extract mean time from ping command:

直接从 ping 命令中提取平均时间:

ping -w 4 -q www.duckduckgo.com  | cut -d "/" -s -f5

Options:

选项:

-w time out 4 seconds
-q quite mode
-d delimiter 
-s skip line without delimiter
-f No. of field - depends on your system - sometimes 5th, sometimes 4th

I personly use is this way:

我个人是这样使用的:

if [ $(ping -w 2 -q www.duckduckgo.com | cut -d "/" -s -f4 | cut -d "." -f1) -lt 20 ]; then
 echo "good response time"
else 
 echo "bad response time"
fi

回答by allanlaal

Use these to get current ping as a single number:

使用这些将当前 ping 作为单个数字获取:

123.456: ping -w1 -c1 8.8.8.8 | tail -1| cut -d '=' -f 2 | cut -d '/' -f 2

123.456: ping -w1 -c1 8.8.8.8 | tail -1| cut -d '=' -f 2 | cut -d '/' -f 2

123: ping -w1 -c1 8.8.8.8 | tail -1| cut -d '=' -f 2 | cut -d '/' -f 2 | cut -d '.' -f 1

123: ping -w1 -c1 8.8.8.8 | tail -1| cut -d '=' -f 2 | cut -d '/' -f 2 | cut -d '.' -f 1

Note that this displays the average of only 1 ping (-c1), you can increase the sample size by increasing this number (i.e. -c1337)

请注意,这仅显示 1 ping ( -c1)的平均值,您可以通过增加此数字来增加样本大小 (即-c1337)

This avoids using awk (like @Buggabill posted), which doesn't play nice in bash aliases + takes a nanosecond longer

这避免了使用 awk(如@Buggaill 发布的),它在 bash 别名中效果不佳 + 需要更长的纳秒时间