麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Linux Shell > 正文

Shell腳本實現自動檢測修改最快的Ubuntu軟件源

2020-07-27 19:13:12
字體:
來源:轉載
供稿:網友

每次裝好Ubuntu,對于大多數用戶來說,首先要做的事就是手動修改/etc/apt/sources.list文件,將里面的官方軟件源地址更換為自己學校或者公司的軟件源。當我們更換一個工作環境后,可能伴隨著又要替換舊的軟件源地址。

筆者覺得這樣每次手動更改軟件源是一件及其麻煩重復的勞動,于是編寫了一個自動更新最快軟件源的腳本,從此一勞永逸。

原理

最直觀的想法就是:對各個軟件源進行測速,選出最快的那個,之后將其替換為新的軟件源。

那么如何對各個軟件源測速呢?有兩種方法:

一、用ping命令 測量其平均響應時間 選出響應時間最短的那個

二、用wget命令 測量下載一個文件的總時間 選出耗時最少的那個

那么這兩種方法有什么區別呢?我們該用哪個呢?

前者選出的是響應時間最優的,后者選出的是下載速度最快的。我們都知道軟件源的作用是供客戶端下載更新軟件,所以當然是后者的方法更為準確,但筆者最終選擇了前者作為測速方案,因為前者的用戶體驗更好且代碼簡單易懂。設想,如果我們采用后者,那么需要從每個軟件源下載一個文件,并且這個文件不能太小,否則無法區分他們的速度,那么一個顯而易見的情況是腳本需要運行較長的時間。

雖然存在某些軟件源可能響應時間很短,而下載速度卻很慢的情況,但經過筆者的多次實驗,發現這樣的情況并不常見。

實現

首先測試用戶網絡狀態

利用

復制代碼 代碼如下:

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

取出其平均響應時間 如果speed == “” 則說明網絡不通,提示用戶,且退出程序。 否則,說明網絡正常,繼續執行。

檢測軟件源列表文件是否存在

復制代碼 代碼如下:

test -f $SOURCES_MIRRORS_FILE

若不存在,提示用戶,且退出程序。

對每個軟件源地址進行測速

在測速之前清空上次運行的測速結果文件,之后將每個軟件源的測速結果(源地址 平均響應時間)寫入測速結果文件

對測速結果進行排序

復制代碼 代碼如下:

sort -k 2 -n -o $MIRRORS_SPEED_FILE $MIRRORS_SPEED_FILE

對每行記錄 按照平均響應時間升序排列

選出最快的軟件源

復制代碼 代碼如下:

head -n 1 $MIRRORS_SPEED_FILE | cut -d ' ' -f1 `

通過取已排序列表中的第一條,選出最快的軟件源

詢問用戶是否要使用該軟件源

用戶確認后,先對用戶之前的軟件源進行備份,然后再替換。

getfastmirror.sh腳本源代碼:

復制代碼 代碼如下:

#!/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 +%F-%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? [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源碼:

復制代碼 代碼如下:

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/

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美成人小视频 | 欧洲成人一区二区 | 亚洲成人久久精品 | 国产精品毛片va一区二区三区 | 久久成人动漫 | 99r国产精品 | 欧美不卡 | 国产精品久久久久国产精品三级 | 国产午夜精品久久久久久久蜜臀 | 91精品国产91久久久久久 | 国产乱一区二区三区视频 | 国产精品久久久久久久久久三级 | 成人三级电影网站 | 久久影城 | 操你啦免费视频 | 一区二区三区欧美在线观看 | 国产精品欧美久久久久一区二区 | 久久精品中文字幕一区二区三区 | 激情福利视频 | 久草免费资源视频 | 成人mm视频在线观看 | 国产99视频精品免视看9 | 精品久久久久久久久久久下田 | japan护士性xxxⅹhd | 欧美一级黑人 | 毛片在线播放视频 | 久久人人爽人人爽人人片av高清 | 日本一区二区视频在线观看 | 国产一区精品视频 | 亚洲一区二区 | av免费在线播放网址 | 色播视频在线播放 | 日韩1区 | 黄色网址在线免费 | 一本色道久久综合狠狠躁篇适合什么人看 | 大片毛片| 国产中出在线观看 | 看国产精品 | 一区二区三区视频在线观看 | 热re91久久精品国产99热 | 久久久久亚洲a |