Skip to content

Instantly share code, notes, and snippets.

@netwjx
Last active October 27, 2023 18:13
Show Gist options
  • Save netwjx/52670b4e68b475649638 to your computer and use it in GitHub Desktop.
Save netwjx/52670b4e68b475649638 to your computer and use it in GitHub Desktop.
使用ping批量测试服务器速度

支持一个输入参数: ip地址文件列表

所有不是ip地址的行都会忽略,所以可以这样

1.2.3.4

my comment text

每个ip会ping5次,结果以平均响应时间从小到大排序,最快的在开头.

#!/bin/bash
awk -F '.' '
$1>0 && $1<255 &&
$2>0 && $2<255 &&
$3>0 && $3<255 &&
$4>0 && $4<255 {
system("ping -c 5 " $0)
}' $1 | awk -F '[ /]' '
$1 == "PING" {
gsub(/[()]/, "", $3)
ip = $3
}
$1 == "rtt" {
min = $7
avg = $8
max = $9
mdev = $10
print "avg:", avg, "min:", min, "max:", max, "mdev:", mdev, "ip:", ip
}' | sort -hk 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment