shell脚本为ubuntu 22.04/20.04/18.04自动选择最快的apt源

ubuntu 22.04/20.04/18.04直接运行

1
wget  https://ip.332b.cn/ubuntu_apt/select_apt_sources.xsh  &&  chmod +x select_apt_sources.xsh  &&  sudo bash select_apt_sources.xsh

代码如下

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
#!/bin/bash

# curl https://ip.332b.cn/ubuntu_apt/ubuntu22.04.txt sed 's/cn.archive.ubuntu.com/mirrors.aliyun.com/g' cp - /etc/apt/sources.list

# curl -Lo /dev/null -skw "%{speed_download}\n" --connect-timeout 3 -m 3 http://mirrors.163.com/ubuntu/ls-lR.gz

# 检查是否有root执行权限
current_user=$(whoami)
if [ "$current_user" != "root" ]; then
echo "error,No root permission,running this script requires root permission"
exit 1
fi

# 检查curl是否安装
dpkg -s curl bc &> /dev/null

echo "Checking bc and curl modules..."
if [ $? -ne 0 ];then
apt update && apt install curl bc -y

fi

echo "Backing up the sources.list file to /etc/opt/resources.list.bk directory..."
# 检查文件是否存在,备份sources.list
if [ ! -f /etc/apt/sources.list.bk/sourecs.list ];then
if [ ! -d /etc/apt/sources.list.bk ];then
mkdir /etc/apt/sources.list.bk
fi
cp /etc/apt/sourecs.list /etc/apt/sources.list.bk/sourecs.list

elif [ -f /etc/apt/sources.list.bk/sourecs.list.10 ];then
cp /etc/apt/sourecs.list /etc/apt/sources.list.bk/sourecs.list.10

else
for apt_id in {0..10}
do
if [ ! -f /etc/apt/sources.list.bk/sourecs.list.$apt_id ];then
cp /etc/apt/sourecs.list /etc/apt/sources.list.bk/sourecs.list.$apt_id
break
fi
done
fi

#测试
# test_apt_speed(){
# apt_list=`curl https://ip.332b.cn/ubuntu_apt/apt_list.txt`
# curl https://ip.332b.cn/ubuntu_apt/apt_list.txt while read line
# do
# apt_speed=`curl -Lo /tmp/apt.tmp -skw "%{speed_download}\n" --connect-timeout 3 -m 3 ${line}ls-lR.gz`
# s1=`echo ${line}`
# echo $apt_speed,$s1
# done
# for apt_id1 in `curl https://ip.332b.cn/ubuntu_apt/apt_list.txt`
# do
# apt_speed=`curl -Lo /tmp/apt.tmp -skw "%{speed_download}\n" --connect-timeout 3 -m 3 ${apt_id1}ls-lR.gz`
# echo $apt_id1
# echo $apt_speed
# done

# }

test_apt_speed(){
read fast_site fast_speed <<< "ip.332b.cn 0"
declare -A dict_list
echo 'Testing APT site download speed...'
for apt_id1 in `curl -s https://ip.332b.cn/ubuntu_apt/apt_list.txt`
do
# apt_id1=`echo "$apt_id1" grep -Eo '[0-9.]+'"
# echo $apt_id1 grep -Eo '[a-z.]+'`
apt_id2=`echo $apt_id1 grep -Eo '[a-z0-9.:_/c]+'`
apt_speed=`curl -Lo /dev/null -skw "%{speed_download}\n" --referer "http://www.baidu.com" --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.48" --connect-timeout 3 -m 3 ${apt_id2}dists/jammy/Contents-amd64.gz`
# echo $apt_id1 $apt_speed
# echo "1111",'11' $((1+2))
# `echo "scale=2; $num1/$num2" bc`
# apt_speed=`echo $apt_speed grep -Eo '^[0-9]+'`
echo $apt_id2,`echo "scale=2; $apt_speed/1024/1024" bc awk '{printf "%.2f", $0}'`mb/s
# dict_list[$apt_id2]=$apt_speed
if [ $fast_speed -lt $apt_speed ];then
fast_speed="$apt_speed"
fast_site="$apt_id2"
fi
done
# ehco
# for akey in "${!dict_list[@]}"; do
# # echo $akey,${dict_list[$akey]}
# echo $akey,`echo "scale=2; $apt_speed/1024/1024" bc awk '{printf "%.2f", $0}'`mb/s
# done
echo -e "\nThe fastest website is $fast_site",`echo "scale=2; $apt_speed/1024/1024" bc awk '{printf "%.2f", $0}'`mb/s
# return $fast_site
}

ubuntu_list=("22.04" "18.04" "20.04")

# 获取ubuntu版本
system_version=`cat /etc/os-release grep VERSION_ID grep -Eo '[0-9.]+'`

for (( i=0;i<${#ubuntu_list[@]};i++ ))
do
if [ $system_version == ${ubuntu_list[$i]} ];then
# echo ubuntu_list[$i]
test_apt_speed
curl -s "https://ip.332b.cn/ubuntu_apt/ubuntu${ubuntu_list[$i]}.txt" sed "s#http://cn.archive.ubuntu.com/ubuntu/#${fast_site}#g" sed "s#http://cn.archive.ubuntu.com/ubuntu/#${fast_site}#g" tee /etc/apt/sources.list 1>/dev/null
echo "Successfully replaced APT source for you"
break
# cat /etc/apt/sources.list
# curl -s https://ip.332b.cn/ubuntu_apt/ubuntu${system_version}.txt sed "s/cn.archive.ubuntu.com/${fast_site}/g" cp - /etc/apt/sources.list
# echo $fast_site
fi
done