相关shell脚本如下:
# bionic is code of Ubuntu 18.04 if your ubuntu is not 18.04 please change
VERSION="bionic"
SOURCES_MIRRORS_FILE="/etc/apt/sources_mirrors.list"
MIRRORS_SPEED_FILE="/tmp/mirrors_speed.list"
#return average ping $1 time
function get_ping_speed() {
local speed=$(ping -W1 -c1 $1 2>/dev/null | grep "^rtt" | cut -d '/' -f5)
echo $speed
}
function test_mirror_speed() {
if [ -f "$MIRRORS_SPEED_FILE" ]; then
rm $MIRRORS_SPEED_FILE > /dev/null
fi
touch $MIRRORS_SPEED_FILE
cat $SOURCES_MIRRORS_FILE | while read mirror; do
if [ "$mirror" != "" ]; then
echo -e "Ping $mirror"
#change mirror_url to host
local mirror_host=$(echo $mirror | cut -d '/' -f3)
local speed=$(get_ping_speed $mirror_host)
if [ "$speed" != "" ]; then
echo "Time is $speed $mirror"
echo "$mirror $speed" >>$MIRRORS_SPEED_FILE