-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathubuntuYUAN.sh
executable file
·202 lines (200 loc) · 5.66 KB
/
ubuntuYUAN.sh
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
#------------------------------------------------------------------------#
MIRRORS_LIST=./.ubuntu_mirrors.list #文件名和存储位置都可以改,存放所有的源列表
MIRRORS_SCORED=./.ubuntu_mirrors #文件名和存储位置都可以改,存放测试后速度快的几个源
VERSION=`lsb_release -a | grep 'Codename' | awk '{print $2}'` #ubuntu版本发生变化时,就更改这个"hardy"为相应的版本号
if [ `getconf LONG_BIT` == 32 ]; then
ARCHITECTURE="i386"
else
ARCHITECTURE="amd64"
fi
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 [ -z $yn ]; then
yn="y"
fi
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 [ -z $yn ]; then
yn="y"
fi
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. ";
echo -n "sudo apt-get update now? [Y/n]"
read yn
if [ -z $yn ]; then
yn="y"
fi
if [ "$yn" == "y" ]; then
echo "sudo apt-get update"
sudo apt-get update
fi
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` [hardy|breezy]"
echo "hardy 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 $MIRRORS_SCORED.$num
cat $timer | sort -k 2 -r -g > $MIRRORS_SCORED
fi
choose_mirror 1
exit 0