Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Shell script to automatically detect the fastest modified Ubuntu software source

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to write Shell scripts to automatically detect and modify the fastest Ubuntu software source", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to write Shell scripts to automatically detect and modify the fastest Ubuntu software source"!

Principle

The most intuitive idea is to measure the speed of each software source, select the fastest one, and then replace it with a new software source.

So how to measure the speed of each software source? There are two ways:

First, use the ping command to measure the average response time and select the one with the shortest response time.

Use the wget command to measure the total time it takes to download a file and choose the one that takes the least time.

So what's the difference between the two methods? Which one should we use?

The former selects the best response time, while the latter selects the fastest download speed. We all know that the function of the software source is for the client to download the updated software, so of course the method of the latter is more accurate, but the author finally chooses the former as the speed measurement scheme, because the user experience of the former is better and the code is easy to understand. Imagine 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 it is obvious that the script takes a long time to run.

Although there are some software sources that the response time may be very short, but the download speed is very slow, but after many experiments, the author found that such a situation is not common.

Realize

First, test the user's network status.

Utilization

The code is as follows:

Local speed= `ping-W1-C1 www.baidu.com 2 > / dev/null | grep "^ rtt" | cut-d'/'- f5`

Take out the average response time if speed = "" indicates that the network is not available, prompt the user, and exit the program. Otherwise, the network is normal and continue to execute.

Check whether the software source list file exists

The code is as follows:

Test-f $SOURCES_MIRRORS_FILE

If it does not exist, prompt the user and exit the program.

Measure the speed of each software source address

Clear the last running speed measurement result file before the speed measurement, and then write the speed measurement result (the average response time of the source address) of each software source to the speed measurement result file.

Sort the speed measurement results

The code is as follows:

Sort-k 2-n-o $MIRRORS_SPEED_FILE $MIRRORS_SPEED_FILE

Arrange each row of records in ascending order of average response time

Select the fastest software source

The code is as follows:

Head-n 1 $MIRRORS_SPEED_FILE | cut-d'- F1 `

Select the fastest software source by taking the first item in the sorted list

Ask the user if they want to use the software source

After the user confirms, first back up the user's previous software source, and then replace it.

Getfastmirror.sh script source code:

The code is 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 First release

VERSION= "precise" # precise is code of Ubuntu 12.04 if your ubuntu is not 12.04 please change

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 $1 time

{

Local speed= `ping-W1-C1 $1 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

Do

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 +% Fmuri% R:% s`

}

Function update_sources ()

{

Local COMP= "main restricted universe multiverse"

Local mirror= "$1"

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 you want to use it? [YBO] c"

Read choice

If ["$choice"! = "y"]; then

Exit 0

Fi

Backup_sources

Update_sources $fast_mirror

Fi

Fi

Exit 0

Sources_mirrors.list source code:

The code is 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/

Thank you for your reading, the above is "how to write Shell scripts to automatically detect and modify the fastest Ubuntu software source", after the study of this article, I believe you on how to write Shell scripts to achieve automatic detection and modification of the fastest Ubuntu software source this problem has a more profound experience, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report