日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Linux检查镜像,Shell脚本实现检测Cygwin最快的镜像站点

發布時間:2025/3/20 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux检查镜像,Shell脚本实现检测Cygwin最快的镜像站点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這是一個 shell 腳本,所以首先你需要安裝一個基本的 Cygwin 環境,當然還有 curl。

原理很簡單,先從 cygwin.com 下載最新的 mirrors.lst 鏡像列表,簡單處理一下后,利用 curl 以此檢測每個站點的連接速度,并將結果記錄下來,最后再排個序,顯示出最快的幾個站點。

在使用的過程中,我發現檢測到的最快的 mirror,實際上使用速度并不一定是最快的,這可能和服務器有關系,畢竟 curl 檢測的時間只是讀取 mirror 首頁的時間。不過每個 mirror 一般都有兩組服務器——http & ftp,如果其中一個速度不怎么樣,那么可以選擇另外一個試試看。

#!/bin/sh

# cygwin-mirrors.sh

# 該腳本用于查找 Cygwin 的最快鏡像

timeout=5?????????? # 超時時間

mirrors_count=5???? # 顯示最快的幾個鏡像

PROG=`basename $0`? # 程序名稱

## 顯示 usage

_usage() {

echo "Usage: ${PROG} [-t ] [-p ] [-h]"

exit

}

## 檢查參數并賦值

_assign() {

if [ "$1" == "timeout" -o "$1" == "mirrors_count" ]; then

if [[ "$2" =~ ^[[:digit:]]+$ ]]; then

let $1=$2

else

echo "$1 should be a number"

exit 1

fi

fi

}

## 處理參數

while getopts ":t:p:h-:" optval

do

case "$optval" in

t)?? _assign timeout ${OPTARG} ;;

p)?? _assign mirrors_count ${OPTARG} ;;

h)?? _usage ;;

"-") echo "Unknown option: '--${OPTARG}'" >&2;??????????? _usage ;;

":") echo "Option '-${OPTARG}' requires an argument" >&2; _usage ;;

"?") echo "Unknown option: '-${OPTARG}'" >&2;???????????? _usage ;;

## Should not occur

*)?? echo "Unknown error while processing options" >&2;?? _usage ;;

esac

done

shift $(expr ${OPTIND} - 1)

## 檢查用戶是否安裝了 curl

CURL=`which curl 2> /dev/null`

[ -z "$CURL" ] && (echo "Need to install the curl package."; exit 1)

## 讀取鏡像站點

mirrors=`curl --silent http://cygwin.com/mirrors.lst | cut -d';' -f1`

## 使用 CURL 依次檢測時間

results=''

for mirror in $mirrors; do

echo -n "Checking ${mirror} ... "

time=`curl -m $timeout -s -o /dev/null -w %{time_total} $mirror`

if [ "$time" = "0.000" ]; then

echo -e "\e[31mfail\e[0m"

else

echo -e "\e[32m$time\e[0m"

results="${results}\e[32m${time}\e[0m - ${mirror}\n"

fi

done

echo -e "\n檢測結果:"

echo -e $results | sort -n | sed '1d' | head -$mirrors_count

# vim: set expandtab tabstop=4 shiftwidth=4:

總結

以上是生活随笔為你收集整理的Linux检查镜像,Shell脚本实现检测Cygwin最快的镜像站点的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。