You are not logged in.

#1 2009-03-07 17:45:23

dimigon
Member
Registered: 2009-03-07
Posts: 139
Website

bash script to select fast mirror

I've written this basic script which can be used to select a mirror from pacman's mirrorlist file which minimizes the response time between you and the mirror. This often leads to satisfactory download speeds. You might have to change some of the variables at the beginning of the script to suit your needs.

#!/bin/bash
#
# mirrorize v0.1 - Fast mirror selector for pacman
# Copyright (C) 2009 sin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

if [ ! "$UID" == "0" ]; then
    echo "[+] you cannot perform this operation unless you are root." 1>&2
    exit
fi

MIRROR_PATH="/etc/pacman.d/mirrorlist.pacorig"
BEST_MIRROR=""
BEST_TIME=300 # ms
TMP=$(mktemp)

function cleanup() {
    rm -f $TMP
}

trap cleanup SIGINT SIGTERM

cp $MIRROR_PATH $MIRROR_PATH.backup

for mirror in `cat $MIRROR_PATH | sed 's/#//g' | grep ':' | sed 's/Server = //g' | sed 's/ftp:\/\///g' | sed 's/http:\/\///g' | sed 's/\/.*//g'`; do
    M=$(ping $mirror -B -w 2 -n -c 4 2>/dev/null | grep rtt | awk -F '/' '{print $5}')
    if [ ! -z "$M" ]; then
        M=${M/.*};
        if [ "$M" -lt "$BEST_TIME" ]; then
            BEST_TIME=$M
            BEST_MIRROR=$mirror
            echo "[+] found a faster mirror [$BEST_MIRROR] => response time = $BEST_TIME ms"
        fi
    fi
done

if [ ! -z "$BEST_MIRROR" -a ! "$BEST_MIRROR" = "ftp.archlinux.org" -a ! -z "$BEST_TIME" ]; then
    sed 's/^Server/#Server/g' $MIRROR_PATH > $TMP; cat $TMP > $MIRROR_PATH
    cat $TMP > $MIRROR_PATH
    line=$(egrep -in $BEST_MIRROR $MIRROR_PATH | uniq | sed 's/:.*//g' | head -1)
    extension="ftp"
    cat $MIRROR_PATH | head -$line | tail -1 | egrep -i "ftp:" 1>/dev/null
    [ $? -eq 0 ] || extension="http"
    sed -e "${line}s/^#Server = .*${BEST_MIRROR}/Server = ${extension}:\/\/${BEST_MIRROR}/g" $MIRROR_PATH > $TMP
    cat $TMP > $MIRROR_PATH
fi

cleanup

pacman -Syy

Last edited by dimigon (2009-03-07 23:36:46)

Offline

#2 2009-03-07 22:38:27

Heema
Member
From: Egypt
Registered: 2008-08-12
Posts: 62

Re: bash script to select fast mirror

nice script, but i think it will be more accurate if you made the ping line to

M=$(ping $mirror -B -w 2 -n -c 4 2>/dev/null | grep rtt | awk -F '/' '{print $5}')

Offline

#3 2009-03-07 22:57:48

Hrod beraht
Member
Registered: 2008-09-30
Posts: 186

Re: bash script to select fast mirror

Is this different from rankmirrors?

Bob

Offline

#4 2009-03-07 23:16:46

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

Re: bash script to select fast mirror

Hrod beraht wrote:

Is this different from rankmirrors?

Bob

Lol, I was just going to say: "Doesn't rank mirrors do this for you?"


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

#5 2009-03-07 23:18:43

dimigon
Member
Registered: 2009-03-07
Posts: 139
Website

Re: bash script to select fast mirror

Heema wrote:

nice script, but i think it will be more accurate if you made the ping line to

M=$(ping $mirror -B -w 2 -n -c 4 2>/dev/null | grep rtt | awk -F '/' '{print $5}')

thanks for the remark :-)

Offline

#6 2009-03-08 07:22:10

brando56894
Member
From: NYC
Registered: 2008-08-03
Posts: 681

Re: bash script to select fast mirror

haxit wrote:
Hrod beraht wrote:

Is this different from rankmirrors?

Bob

Lol, I was just going to say: "Doesn't rank mirrors do this for you?"

exactly what I was thinking also! but rankmirrors needs python so i guess if you needed a stand alone script this would be perfect.

Offline

#7 2009-03-09 22:27:11

Heema
Member
From: Egypt
Registered: 2008-08-12
Posts: 62

Re: bash script to select fast mirror

made a little update for it so it doesnt requir root acount
it also now adds last sync times to the results


#!/bin/bash
#
# mirrorize v0.1 - Fast mirror selector for pacman
# Copyright (C) 2009 sin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

MIRRORLIST=$(mktemp)
cat /etc/pacman.d/mirrorlist > $MIRRORLIST

MIRROR_PATH="$MIRRORLIST"
BEST_MIRROR=""
BEST_TIME=300
TMP=$(mktemp)
LIST=$(mktemp)

wget -q "http://users.archlinux.de/~gerbra/mirrorcheck.html" -O /tmp/mirrorcheck.html

function cleanup() {
    rm -f $TMP
    rm -f $LIST
    rm /tmp/mirrorcheck.html
}

trap cleanup SIGINT SIGTERM

echo "Pinging Servers..."

for mirror in `cat $MIRROR_PATH | sed 's/#//g' | grep ':' | sed 's/Server = //g' | sed 's/ftp:\/\///g' | sed 's/http:\/\///g' | sed 's/\/.*//g'`; do
    M=$(ping $mirror -B -w 2 -n -c 4 2>/dev/null | grep rtt | awk -F '/' '{print $5}')
    
    if [ ! -z "$M" ]; then
        M=${M/.*};
        if [ "$M" -lt "$BEST_TIME" ]; then
            BEST_MIRROR=$mirror
        LASTSYNC=$(cat /tmp/mirrorcheck.html | grep -A 13 "$BEST_MIRROR" | tail -n 1 | sed 's/<td>//g' | sed 's/()<\/td>//g' | cut -c 7-)
            echo "$M $mirror   Not synced since: $LASTSYNC" >> $LIST
    fi
    fi
done

cat $LIST | sort -n

cleanup

Offline

#8 2009-03-18 00:26:48

dimigon
Member
Registered: 2009-03-07
Posts: 139
Website

Re: bash script to select fast mirror

Heema wrote:

made a little update for it so it doesnt requir root acount
it also now adds last sync times to the results


#!/bin/bash
#
# mirrorize v0.1 - Fast mirror selector for pacman
# Copyright (C) 2009 sin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

MIRRORLIST=$(mktemp)
cat /etc/pacman.d/mirrorlist > $MIRRORLIST

MIRROR_PATH="$MIRRORLIST"
BEST_MIRROR=""
BEST_TIME=300
TMP=$(mktemp)
LIST=$(mktemp)

wget -q "http://users.archlinux.de/~gerbra/mirrorcheck.html" -O /tmp/mirrorcheck.html

function cleanup() {
    rm -f $TMP
    rm -f $LIST
    rm /tmp/mirrorcheck.html
}

trap cleanup SIGINT SIGTERM

echo "Pinging Servers..."

for mirror in `cat $MIRROR_PATH | sed 's/#//g' | grep ':' | sed 's/Server = //g' | sed 's/ftp:\/\///g' | sed 's/http:\/\///g' | sed 's/\/.*//g'`; do
    M=$(ping $mirror -B -w 2 -n -c 4 2>/dev/null | grep rtt | awk -F '/' '{print $5}')
    
    if [ ! -z "$M" ]; then
        M=${M/.*};
        if [ "$M" -lt "$BEST_TIME" ]; then
            BEST_MIRROR=$mirror
        LASTSYNC=$(cat /tmp/mirrorcheck.html | grep -A 13 "$BEST_MIRROR" | tail -n 1 | sed 's/<td>//g' | sed 's/()<\/td>//g' | cut -c 7-)
            echo "$M $mirror   Not synced since: $LASTSYNC" >> $LIST
    fi
    fi
done

cat $LIST | sort -n

cleanup

Cheers! Nice one

Offline

Board footer

Powered by FluxBB