Install Ubuntu every time, for most users, the first thing to do is to manually modify the/etc/apt/sources.list file, the official software source address will be replaced by their own school or the company's software source. When we replace a working environment, it may be accompanied by the replacement of the old software source address.
The author thinks that every time I manually change the source of the software is a duplication of labor, and then wrote a automatic update the fastest software source script, and once and for all.
Principle
The most intuitive idea is: to the various software sources to speed, select the fastest one, and then replace it with a new software source.
So how to each software source speed? There are two ways of doing this:
First, the ping command to measure its average response time to choose the shortest response time
Second, using wget command to measure the total time to download a file select the least time-consuming
So what's the difference between these two methods? Which one should we use?
The former chooses the best response time, and the latter picks the fastest download speed. We all know that the role of the software source is for the client to download the update software, so of course the latter method is more accurate, but the author finally chose the former as a speed scheme, because the former user experience is better and the code is easy to understand. Assuming that if we adopt the latter, we need to download a file from each software source, and the file cannot be too small to distinguish their speed, then one obvious scenario is that the script needs to run for a long time.
Although there are some software sources may be very short response time, but the download speed is very slow, but after the author of many experiments, found that such a situation is not common.
Realize
Test user Network Status first
Use
Copy Code code as follows:
Local speed= ' ping-w1-c1 www.baidu.com 2>/dev/null | grep "^rtt" | Cut-d '/'-f5 '
Take out its average response time if speed = = "" Then the network is not access, prompt the user, and exit the program. Otherwise, it indicates that the network is normal and continues to execute.
Detect if a software source list file exists
Copy Code code as follows:
Test-f $SOURCES _mirrors_file
If it does not exist, prompt the user and exit the program.
The speed of each software source address
Before the speed of the measurement of the last run of the speed of the results file, then the results of each software source (the source address average response time) to write the speed of the results file
To sort the results of the Velocimetry
Copy Code code as follows:
Sort-k 2-n-o $MIRRORS _speed_file $MIRRORS _speed_file
Arrange each line record in ascending order of average response time
Choose the fastest source of software
Copy Code code as follows:
Head-n 1 $MIRRORS _speed_file | Cut-d '-f1 '
Select the fastest software source by taking the first article in the ordered list
Ask users if they want to use the software source
After the user confirms, the user's previous software source is backed up before being replaced.
getfastmirror.sh Script Source code:
Copy Code code as follows:
#!/bin/bash
#Program:
# This program gets the fastest Ubuntu software sources from Sources_mirrors_file
# and Backup && update/etc/apt/sources.list
#Author: Kjlmfe www.freepanda.me
#History:
# 2012/12/6 KJLMFE
version= "Precise" # precise is code to Ubuntu 12.04 if your Ubuntu is not 12.04
Test_netconnect_host= "Www.baidu.com"
Sources_mirrors_file= "Sources_mirrors.list"
Mirrors_speed_file= "Mirrors_speed.list"
function Get_ping_speed () #return average ping time
{
Local speed= ' ping-w1-c1 $2>/dev/null | grep "^rtt" | Cut-d '/'-f5 '
Echo $speed
}
function Test_mirror_speed () #
{
RM $MIRRORS _speed_file 2>/dev/null; Touch $MIRRORS _speed_file
Cat $SOURCES _mirrors_file | While Read mirror
Todo
If ["$mirror" "!="]; Then
Echo-e "Ping $mirror C"
Local mirror_host= ' echo $mirror | Cut-d '/'-f3 ' #change Mirror_url to host
Local speed=$ (get_ping_speed $mirror _host)
If ["$speed" "!="]; Then
echo "Time is $speed"
echo "$mirror $speed" >> $MIRRORS _speed_file
Else
echo "Connected failed."
Fi
Fi
Done
}
function Get_fast_mirror ()
{
Sort-k 2-n-o $MIRRORS _speed_file $MIRRORS _speed_file
Local fast_mirror= ' head-n 1 $MIRRORS _speed_file | Cut-d '-f1 '
Echo $fast _mirror
}
function Backup_sources ()
{
Echo-e "Backup Your SOURCES.LIST.N"
sudo mv/etc/apt/sources.list/etc/apt/sources.list. ' Date +%f-%r:%s '
}
function Update_sources ()
{
Local comp= "Main restricted universe Multiverse"
Local mirror= "$"
Local tmp=$ (mktemp)
echo "Deb $mirror $VERSION $COMP" >> $tmp
echo "Deb $mirror $VERSION-updates $COMP" >> $tmp
echo "Deb $mirror $VERSION-backports $COMP" >> $tmp
echo "Deb $mirror $VERSION-security $COMP" >> $tmp
echo "Deb $mirror $VERSION-proposed $COMP" >> $tmp
echo "Deb-src $mirror $VERSION $COMP" >> $tmp
echo "Deb-src $mirror $VERSION-updates $COMP" >> $tmp
echo "Deb-src $mirror $VERSION-backports $COMP" >> $tmp
echo "Deb-src $mirror $VERSION-security $COMP" >> $tmp
echo "Deb-src $mirror $VERSION-proposed $COMP" >> $tmp
sudo mv "$tmp"/etc/apt/sources.list
Echo-e "Your sources has been updated, and maybe you want to run" sudo apt-get update "NOW.N";
}
ECHO-E "ntesting the network connection.nplease wait ... c"
If ["$ (get_ping_speed $TEST _netconnect_host)" = ""]; Then
ECHO-E "Network is bad.nplease check your network."; Exit 1
Else
ECHO-E "Network is GOOD.N"
Test-f $SOURCES _mirrors_file
If ["$?"!= "0"]; Then
Echo-e "$SOURCES _mirrors_file is not EXIST.N"; Exit 2
Else
Test_mirror_speed
fast_mirror=$ (Get_fast_mirror)
If ["$fast _mirror" = ""]; Then
Echo-e "Can" t find the fastest software sources. Please check your $SOURCES _mirrors_filen "
Exit 0
Fi
Echo-e "N$fast_mirror is the fastest software sources. Do your want to use it? [y/n] C "
Read choice
If ["$choice"!= "Y"]; Then
Exit 0
Fi
Backup_sources
Update_sources $fast _mirror
Fi
Fi
Exit 0
Sources_mirrors.list Source:
Copy Code code as follows:
http://cn.archive.ubuntu.com/ubuntu/
http://run.hit.edu.cn/ubuntu/
http://mirrors.sohu.com/ubuntu/
http://mirrors.163.com/ubuntu/
http://mirrors.tuna.tsinghua.edu.cn/ubuntu/
http://mirrors.ustc.edu.cn/ubuntu/
http://mirrors.yun-idc.com/ubuntu/
http://ubuntu.cn99.com/ubuntu/