From: http://hi.baidu.com/fancycedar/blog/item/13711c546163821a3b2935f6.html
This book was moved between the lab and the dormitory. One was CERNET, and the other was Nb's Netcom. To install something, we had to manually change the new software source!
It is estimated that you will be worried about moving between different networks!
How can I know which is the fastest software source for my network?
How can I automatically change the software source easily?
How is technology lazy?
Let's look at the answer.
I'm not the original author, but I don't know who it is ...... I changed it to ubuntu8.04, and later versions can be easily implemented by changing the script. Share some good things with you!
First, we need three files: getfastsources,. ubuntu_mirrors,. ubuntu_mirrors.list. Note that there is "." in front of the last two files, which is a hidden file. When the file name can be changed, you can change the file name as you like. Put getfastsources in/bin and have executable permissions. ubuntu_mirrors,
. Ubuntu_mirrors.list is placed under/home/xxxx.
Then let's take a look at the specific content of each file.
1. getfastsources
#! /Bin/bash
#------------------------------------------------------------------------#
Mirrors_list = $ home/. ubuntu_mirrors.list # You can change the file name and storage location to store all the source lists.
Mirrors_scored = $ home/. ubuntu_mirrors # You can change the file name and storage location to store several sources that are faster after testing.
Version = "hard" # When the Ubuntu version changes, change this "hard" to the corresponding version number.
Architecture = "i386"
Timeout = 20
Concurrent_processes = 30
#------------------------------------------------------------------------#
Function cleanup ()
{
If [-n "$1"-a-d "$1"]; then
Rm-RF "$1"
Fi
}
Check_mirror ()
{
Local mirror = "$1"
Local url = "$ mirror/dists/$ version/main/Binary-$ architecture/packages.gz"
Local State = $ (mktemp-P "$ workdir ")
Local curl = "curl-w % {speed_download}-S-F-M $ timeout-n-o $ state"
Local speed = $ ($ curl $ URL)
If [-F "$ state"]; then
Local resp = $ (hexdump $ state | HEAD | grep '8b1f 0008 ')
If ["$ resp "! = ""]; Then
Echo $ mirror $ speed
Fi
Rm-F $ state
Fi
}
Backup_sources ()
{
Echo "backup your sources. List ."
Local-I num = 0
While [-E/etc/APT/sources. List. $ num]; do
Num = $ num + 1;
Done
Sudo mV/etc/APT/sources. LIST/etc/APT/sources. List. $ num
}
Output_sources ()
{
Local comp = "main restricted universe Multiverse"
Local mirrors = "$1"
Local TMP = $ (mktemp)
Local mirror
For mirror in $ mirrors; do
Echo "Deb $ mirror $ version $ comp"> $ TMP
Echo "Deb $ mirror $ {version}-security $ comp"> $ TMP
Echo "Deb $ mirror $ {version}-Updates $ comp"> $ TMP
Echo-n "do you want to include source packages? [Y/n]"
Local YN
Read YN
If ["$ YN" = "Y"]; then
Echo "Deb-Src $ mirror $ version $ comp"> $ TMP
Echo "Deb-Src $ mirror $ {version}-security $ comp"> $ TMP
Echo "Deb-Src $ mirror $ {version}-Updates $ comp"> $ TMP
Fi
Echo> $ TMP
Done
If [-F "$ tmp"]; then
Echo-n "do you want to continue? [Y/n]"
Local YN
Read YN
If ["$ YN "! = "Y"]; then
Exit 0
Fi
Backup_sources
Sudo mv "$ tmp"/etc/APT/sources. List
Echo "your sources has been updated, and maybe you want to run \" sudo apt-Get update \ "now .";
Else
Echo "Some badthing has happen ...."
Exit 1
Fi
If [-F "$ tmp"]; then
Rm-F "$ tmp"
Fi
}
Choose_mirror ()
{
If [! -F "$ mirrors_scored"]; then
Exit 1
Fi
Local mirrors speeds
Mirrors = $ (cat $ mirrors_scored | sed-n "$1 P" | awk '{print $1 }')
Speeds = $ (cat $ mirrors_scored | sed-n "$1 P" | awk '{print $2 }')
If ["$ mirrors "! = ""]; Then
Echo "we have found the top one mirror for you :"
Echo "$ mirrors"
Echo "the speed is :"
Echo "$ speeds"
Output_sources "$ mirrors"
Exit 0
Else
Echo "why I can't find the best one for you? "
Fi
}
Show_help ()
{
Echo "usage 1 :"
Echo "'basename $ 0' [Hard | Breezy]"
Echo "hard is the default version"
Echo "Example :"
Echo "'basename $ 0' breezy"
Echo
Echo "usage 2 :"
Echo "'basename $ 0' N"
Echo "N is a number"
Echo "Example :"
Echo "'basename $ 0' 3"
Echo "which will choose the 3th mirror from your mirrors as your source"
}
# Kill background processes before exit
Function kill_bgpid ()
{
Local PID
For PID in $ {PIDs [@]}; do
Kill $ PID &>/dev/null
Done
}
#------------------------------------------------------------------------
#------------------------------------------------------------------------
# Load mirrors
If [! -F "$ mirrors_list"]; then
Echo "No Ubuntu mirrors list found in $ mirrors_list ."
Exit 1
Fi
Mirrors = $ (Cat "$ mirrors_list ")
Declare-I counts = $ (echo "$ mirrors" | WC-l)
If ($ counts <2); then
Echo "too few mirrors got from $ mirrors_list"
Exit 1
Fi
# Parse command line option
If ($ #> = 1); then
If ["$1" = "Dapper"-o "$1" = "breezy"]; then
Version = $1
Elif ("$1"> 0); then
Choose_mirror $1
Else
Show_help
Exit 1
Fi
Fi
# The mirror site and the download speed were stored here
Workdir = $ (mktemp-D/tmp/'basename $ 0'. xxxxxx)
Timer = $ (mktemp-p $ workdir)
Trap "cleanup $ workdir" Exit
Echo "setting $ version for you ...";
Exec 2>/dev/null # Close standard error output
Declare-A PIDs # store backgound processes IDS
Trap 'echo do some cleaning work...; kill_bgpid; Exit 1 'int
Declare-I begin = 1
Declare-I end = $ concurrent_processes
If [$ n-Le $ concurrent_processes]; then
End = $ counts
Fi
Declare-I num = 0
While ($ begin <= $ counts); do
For mirror in $ (echo "$ mirrors" | sed-n "$ begin, $ end P"); do
(Check_mirror $ mirror >>$ timer )&
PIDs [$ num] = $!
Let "Num ++"
Done
Wait
# Next Loop
Begin = $ ($ end + 1 ))
End = $ ($ end + $ concurrent_processes-1 ))
If ($ end> $ counts); then
End = $ counts
Fi
Done
If [-F "$ timer"]; then
Declare-I num = 0
While [-e $ mirrors_scored. $ num]; do
Num = $ num + 1;
Done
MV $ mirrors_scored. $ num
Cat $ timer | sort-K 2-r-g> $ mirrors_scored
Fi
Choose_mirror 1
Exit 0
2. ubuntu_mirrors.list
Ftp://apt.nc.hcc.edu.tw/pub/ubuntu
Ftp://apt.ubuntu.org.tw/ubuntu
Ftp://archive.ubuntu.cz/ubuntu
Ftp://debian.charite.de/ubuntu
Ftp://debian.nsu.ru/ubuntu
Ftp://debian.tu-bs.de/ubuntu
Ftp://fr.archive.ubuntu.com/ubuntu
Ftp://ftp.belnet.be/pub/mirror/ubuntu.com
Ftp://ftp.belnet.be/pub/mirror/ubuntu.com/ubuntu
Ftp://ftp.bit.nl/ubuntu
Ftp://ftp.blueyonder.co.uk/sites/ubuntu/archive
Ftp://ftp.cs.mun.ca/pub/mirror/ubuntu
Ftp://ftp.ee.ncku.edu.tw/pub/ubuntu
Ftp://ftp.esat.net/mirrors/archive.ubuntu.com
Ftp://ftp.filearena.net/pub/ubuntu
Ftp://ftp.fsn.hu/pub/linux/distributions/ubuntu
Ftp://ftp.fu-berlin.de/linux/ubuntu
Ftp://ftp.heanet.ie/pub/ubuntu
Ftp://ftp.iasi.roedu.net/mirrors/ubuntulinux.org/ubuntu
Ftp://ftp.iinet.net.au/pub/ubuntu
Ftp://ftp.is.co.za/ubuntu
Ftp://ftp.join.uni-muenster.de/pub/mirrors/ftp.ubuntu.com/ubuntu
Ftp://ftp.kfki.hu/pub/linux/ubuntu
Ftp://ftp.leg.uct.ac.za/pub/linux/ubuntu
Ftp://ftp.linux.it/ubuntu
Ftp://ftp.litnet.lt/pub/ubuntu
Ftp://ftp.lug.ro/ubuntu
Ftp://ftp.mirror.ac.uk/mirror/archive.ubuntu.com/ubuntu
Ftp://ftp.mirrorservice.org/sites/archive.ubuntu.com/ubuntu
Ftp://ftp.ntua.gr/pub/linux/ubuntu
Ftp://ftp.oleane.net/pub/ubuntu
Ftp://ftp.osuosl.org/pub/ubuntu
Ftp://ftp.planetmirror.com/pub/ubuntu/archive
Ftp://ftp.polytechnic.edu.na/pub/ubuntulinux
Ftp://ftp.proxad.net/mirrors/ftp.ubuntu.com/ubuntu
Ftp://ftp.rnl.ist.utl.pt/ubuntu
Ftp://ftp.rz.tu-bs.de/pub/mirror/ubuntu-packages
Ftp://ftp.se.linux.org/pub/Linux/distributions/ubuntu
Ftp://ftp.sjtu.edu.cn/sites/archive.ubuntu.com
Ftp://ftp.sun.ac.za/ftp/ubuntu
Ftp://ftp.ubuntu-es.org/ubuntu
Ftp://ftp.um.es/mirror/ubuntu
Ftp://ftp.uni-klu.ac.at/linux/ubuntu
Ftp://ftp.uninett.no/linux/ubuntu
Ftp://ftp.u-picardie.fr/pub/ubuntu/ubuntu
Ftp://ftp.ussg.iu.edu/linux/ubuntu
Ftp://gd.tuwien.ac.at/opsys/linux/ubuntu/archive
Ftp://klid.dk/ubuntu
Ftp://linux.xjtu.edu.cn/mirror/ubuntu/ubuntu
Ftp://mir1.ovh.net/ubuntu/ubuntu
Ftp://mirror.arcticnetwork.ca/pub/ubuntu/packages
Ftp://mirror.clarkson.edu/pub/distributions/ubuntu
Ftp://mirror.cpsc.ucalgary.ca/mirror/ubuntu.com
Ftp://mirror.isp.net.au/pub/ubuntu
Ftp://mirror.letsopen.com/ OS /ubuntu
Ftp://mirror.mcs.anl.gov/pub/ubuntu
Ftp://mirror.pacific.net.au/linux/ubuntu
Ftp://mirrors.cat.pdx.edu/ubuntu
Ftp://mirrors.dk.telia.net/ubuntu
Ftp://mirrors.dotsrc.org/ubuntu
Ftp://mirror.switch.ch/mirror/ubuntu
Ftp://mirrors.xmission.com/ubuntu
Ftp://na.mirror.garr.it/mirrors/ubuntu-archive
Ftp://nl.archive.ubuntu.com/ubuntu
Ftp:// OS .nchc.org.tw/ubuntu
Ftp://ubuntu.inode.at/ubuntu
Ftp://ubuntu.mirror.mmd.net/ubuntu
Ftp://ubuntu.mirrors.skynet.be/pub/ubuntu.com
Ftp://ubuntu.mirrors.tds.net/ubuntu
Ftp://ubuntu.task.gda.pl/ubuntu
Http://apt.nc.hcc.edu.tw/pub/ubuntu
Http://apt.ubuntu.org.tw/ubuntu
Http://archive.ubuntu.cz/ubuntu
Http://archive.ubuntu.org.cn/ubuntu
Http://darkstar.ist.utl.pt/ubuntu/archive
Http://debian.charite.de/ubuntu
Http://debian.cn99.com/ubuntu
Http://debian.nsu.ru/ubuntu
Http://debian.okey.net/ubuntu
Http://debian.tu-bs.de/ubuntu
Http://espelhos.edugraf.ufsc.br/ubuntu
Http://fr.archive.ubuntu.com/ubuntu
Http://ftp.acc.umu.se/mirror/ubuntu
Http://ftp.ale.org/pub/mirrors/ubuntu
Http://ftp.belnet.be/pub/mirror/ubuntu.com
Http://ftp.belnet.be/pub/mirror/ubuntu.com/ubuntu
Http://ftp.bit.nl/ubuntu
Http://ftp.cse.yzu.edu.tw/ftp/Linux/Ubuntu/ubuntu
Http://ftp.cs.umn.edu/pub/ubuntu
Http://ftp.esat.net/mirrors/archive.ubuntu.com
Http://ftp.heanet.ie/pub/ubuntu
Http://ftp.iasi.roedu.net/mirrors/ubuntu
Http://ftp.iasi.roedu.net/mirrors/ubuntulinux.org/ubuntu
Http://ftp.iinet.net.au/pub/ubuntu
Http://ftp.inf.tu-dresden.de/ OS /linux/dists/ubuntu
Http://ftp.interlegis.gov.br/pub/ubuntu/archive
Http://ftp.join.uni-muenster.de/pub/mirrors/ftp.ubuntu.com/ubuntu
Http://ftp.kaist.ac.kr/pub/ubuntu
Http://ftp.kfki.hu/linux/ubuntu
Http://ftp.linux.it/ubuntu
Http://ftp.litnet.lt/pub/ubuntu
Http://ftp.lug.ro/ubuntu
Http://ftp.ntua.gr/pub/linux/ubuntu
Http://ftp.oleane.net/pub/ubuntu
Http://ftp.osuosl.org/pub/ubuntu
Http://ftp.roedu.net/mirrors/ubuntu
Http://ftp.rz.tu-bs.de/pub/mirror/ubuntu-packages
Http://ftp.ucr.ac.cr/ubuntu
Http://ftp.u-picardie.fr/pub/ubuntu/ubuntu
Http://ftp.ussg.iu.edu/linux/ubuntu
Http://gd.tuwien.ac.at/opsys/linux/ubuntu/archive
Http://godel.cs.bilgi.edu.tr/mirror/ubuntu
Http://itanix.rutgers.edu/ubuntu
Http://kambing.vlsm.org/ubuntu
Http://klid.dk/homeftp/ubuntu
Http://komo.vlsm.org/ubuntu
Http://lug.mtu.edu/ubuntu
Http://mir1.ovh.net/ubuntu/ubuntu
Http://mirror.arcticnetwork.ca/pub/ubuntu/packages
Http://mirror.clarkson.edu/pub/distributions/ubuntu
Http://mirror.cpsc.ucalgary.ca/mirror/ubuntu.com
Http://mirror.freax.be/ubuntu/archive.ubuntu.com
Http://mirror.isp.net.au/ftp/pub/ubuntu
Http://mirror.letsopen.com/ OS /ubuntu
Http://mirror.lupaworld.com/ubuntu/archive
Http://mirror.mcs.anl.gov/pub/ubuntu
Http://mirror.optus.net/ubuntu
Http://mirror.pacific.net.au/linux/ubuntu
Http://mirrors.cat.pdx.edu/ubuntu
Http://mirrors.cs.wmich.edu/ubuntu
Http://mirrors.dk.telia.net/ubuntu
Http://mirrors.dotsrc.org/ubuntu
Http://mirror.switch.ch/ftp/mirror/ubuntu
Http://mirrors.xmission.com/ubuntu
Http://mirror.trivini.no/ubuntu
Http://mirror.vmmatrix.net/ubuntu
Http://na.mirror.garr.it/mirrors/ubuntu-archive
Http://nl.archive.ubuntu.com/ubuntu
Http://ubuntu-arch.linux.edu.lv/ubuntu
Http://ubuntu.blueyonder.co.uk/archive
Http://ubuntu.c3sl.ufpr.br/ubuntu
Http://ubuntu.cn99.com/ubuntu
Http://ubuntu.csie.ntu.edu.tw/ubuntu
Http://ubuntu.cs.utah.edu/ubuntu
Http://ubuntu.dcc.fc.up.pt
Http://ubuntu.inode.at/ubuntu
Http://ubuntu.interlegis.gov.br/archive
Http://ubuntulinux.mainseek.com/ubuntu
Http://ubuntu.mirror.mmd.net/ubuntu
Http://ubuntu.mirrors.skynet.be/pub/ubuntu.com
Http://ubuntu.mirrors.skynet.be/pub/ubuntu.com/ubuntu
Http://ubuntu.mirrors.tds.net/ubuntu
Http://ubuntu.mithril-linux.org/archives
Http://ubuntu.odg.cc
Http://ubuntu.secs.oakland.edu
Http://ubuntu.synssans.nl
Http://ubuntu.uni-klu.ac.at/ubuntu
Http://www.artfiles.org/ubuntu.com
Http://www.ftp.uni-erlangen.de/pub/mirrors/ubuntu
Http://www.mirror.ac.uk/mirror/archive.ubuntu.com/ubuntu
Http://www.mirrorservice.org/sites/archive.ubuntu.com/ubuntu
Http://www.opensourcemirrors.org/ubuntu
3. ubuntu_mirrors
Create an empty file.
Finally, make sure that your getfastsources is in/bin, and. ubuntu_mirrors and. ubuntu_mirrors.list are in your home directory, and then you can execute getfastsources!
It takes about 30 seconds to test each source.
If:
Fancy @ fancy :~ $ Getfastsources
Setting hard for you...
We have found the top one mirror for you:
Http://archive.ubuntu.org.cn/ubuntu
The speed is:
266730.000
Do you want to include source packages? [Y/n] y
Do you want to continue? [Y/n] y
Backup your sources. List.
[Sudo] password for fancy:
Your sources has been updated, and maybe you want to run "sudo apt-Get Update" now.
Fancy @ fancy :~ $
Congratulations! The script takes effect!