#!/bin/bash trap "cleanup; exit 1" INT if [ $(id -u) -ne 0 ]; then echo "You must be root user to run this program." exit 1 fi cleanup() { [ -f /tmp/sortmirrors ] && rm -f /tmp/sortmirrors [ -f /tmp/sorted ] && rm -f /tmp/sorted } cleanup mirror_file="/etc/pacman.d/current" ct=1 max_num=$(wc -l $mirror_file |awk {'print $1'}) while [ $ct -le $max_num ]; do mirror=$(sed -n "$ct"p $mirror_file) d_candidate=$(echo $mirror |grep ^Server |awk -F= {'print $2'} |sed 's|s||') candidate=$(echo $d_candidate |awk -F "//" {'print $2'} |awk -F/ {'print $1'}) if [ "$candidate" != "" ]; then echo -e "Testing $candidate: \c" testit="ping -c6 "$(echo $candidate)"" result=$($testit |tail -n1 |awk -F/ {'print $6'}) echo $d_candidate%$result >>/tmp/sortmirrors right=$(sed -n '$p' /tmp/sortmirrors |awk -F% {'print $2'}) echo $right"ms" fi ct=$(($ct+1)) done # sort mirrors times=$(cat /tmp/sortmirrors |awk -F% {'print $2'} |sort -n) ct=0 for t in $times; do sorted[$ct]=$(grep $t /tmp/sortmirrors) ct=$(($ct+1)) done echo -e "#n[current]n#" >/tmp/sorted for newentry in ${sorted[@]}; do echo Server = $newentry >>/tmp/sorted done # get rid of appended calculated times sed 's|%.*||g' /tmp/sorted -i /tmp/sorted cp -f /tmp/sorted /etc/pacman.d/current.sorted cp -f /tmp/sorted /etc/pacman.d/extra.sorted sed -e 's|current|extra|g' /etc/pacman.d/extra.sorted -i /etc/pacman.d/extra.sorted cp -f /tmp/sorted /etc/pacman.d/community.sorted sed -e 's|current|community|g' /etc/pacman.d/community.sorted -i /etc/pacman.d/community.sorted cp -f /tmp/sorted /etc/pacman.d/testing.sorted sed -e 's|current|testing|g' /etc/pacman.d/testing.sorted -i /etc/pacman.d/testing.sorted cp -f /tmp/sorted /etc/pacman.d/unstable.sorted sed -e 's|current|unstable|g' /etc/pacman.d/unstable.sorted -i /etc/pacman.d/unstable.sorted cp -f /tmp/sorted /etc/pacman.d/release.sorted sed -e 's|current|release|g' /etc/pacman.d/release.sorted -i /etc/pacman.d/release.sorted echo -e "n==> Append ".sorted" to your Include = line in /etc/pacman.conf to use the sorted mirrors." cleanup