栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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[31mfaile[0m"
    else
        echo -e "e[32m$timee[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:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/37534.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号