-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-mirrors
executable file
·105 lines (87 loc) · 2.27 KB
/
check-mirrors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
#region Constantes
URL_MIRRORLIST="https://www.archlinux.org/mirrorlist/?country=all&protocol=http&protocol=https&ip_version=4"
FILE="/tmp/mirrorlist.txt"
TEST_LIST="./test/testlist.txt"
PING_COUNT=5
ERASE="\e[K"
RE_MIRROR="(.*)\\s+(.*)"
#endregion
#region Colores
CLR_ROJO="\e[91m"
CLR_VERD="\e[92m"
CLR_AMAR="\e[93m"
CLR_FIN="\e[m"
#endregion
#region Funciones
_error() {
if [ "$2" ]; then
echo -e "${ERASE}${CLR_ROJO}check-mirrors: $2${CLR_FIN}" >&2
fi
exit $1
}
_success() {
printf "${ERASE}%s: ${CLR_VERD}%s${CLR_FIN}\n" "$1" "$2"
}
#endregion
main() {
obtener_lista || _error 1 "no se pudo obtener la lista de mirrors"
procesar_lista
}
obtener_lista() {
local cols=$(tput cols)
local pad=31
local size=$(($cols - $pad))
echo -en "Obteniendo lista de mirrors ${CLR_AMAR}${URL_MIRRORLIST:0:$size}${CLR_FIN}...\r"
wget "$URL_MIRRORLIST" -O "$FILE" > /dev/null 2>&1
# FILE="$TEST_LIST"
}
procesar_lista() {
local min_avg=400
local min_url=
local min_pais=
IFS=$'\n'
for linea in $(obtener_mirrors); do
if [[ $linea =~ $RE_MIRROR ]]; then
local pais="${BASH_REMATCH[1]}"
local url="${BASH_REMATCH[2]}"
printf "${ERASE}Pingeando ${CLR_AMAR}%s${CLR_FIN} (%s)...\r" "$url" "$pais"
local avg_time=$(probar_mirror "$url")
if [ $avg_time ]; then
if es_menor $avg_time $min_avg; then
min_avg=$avg_time
min_url=$url
min_pais="$pais"
fi
fi
else
_error 2 "error de regex: [$linea]"
fi
done
_success "Mejor mirror" "$min_url"
_success "Pais" "$min_pais"
_success "Tiempo" "$min_avg ms"
}
obtener_mirrors() {
# Obtiene la lista de mirrors, almacenados en el archivo temporal.
cat "$FILE" | awk -F '/' '
$1 ~ /^##/ {
gsub("## ", "")
pais=$0
}
$1 ~ /Server/ {
printf "%s\t%s\n", pais, $3
}
'
}
probar_mirror() {
# Hace ping a la url y se obtiene el tiempo promedio.
# $1: url
ping -c $PING_COUNT "$1" |
tail -1 |
awk -F '/' '{ print $5 }'
}
es_menor() {
[ $(echo "$1 < $2" | bc -l) -eq 1 ]
}
main "$@"