Shell script implementation detects cygwin the fastest mirror site _linux shell

Source: Internet
Author: User
Tags curl

This is a shell script, so first you need to install a basic Cygwin environment and, of course, curl.

The principle is very simple, first download the latest MIRRORS.LST mirror list from cygwin.com, after simple processing, using curl to detect the connection speed of each site, and record the results, and finally arrange a sequence, showing the fastest several sites.

In the use of the process, I found that the fastest mirror, in fact, the use of speed is not necessarily the fastest, this may be related to the server, after all, curl detection time is only to read the mirror home page time. However, each mirror generally has two sets of servers--http & FTP, if one of the speed is not good, then you can choose another try.

Copy Code code as follows:

#!/bin/sh

# cygwin-mirrors.sh
# This script is used to find the fastest mirror image for Cygwin

Timeout=5 # timeout time
Mirrors_count=5 # shows the fastest number of mirrors
Prog= ' basename $ ' # program name

# # Display Usage
_usage () {
echo "Usage: ${prog} [-t <timeout>] [-P <mirrors_count>] [-H]"
Exit
}

# # Check parameters and assign values
_assign () {
If ["$" = = "Timeout"-o "$" = = "Mirrors_count"]; Then
if [["$" =~ ^[[:d igit:]]+$]]; Then
Let $1=$2
Else
echo "$ should be a number"
Exit 1
Fi
Fi
}

# # Processing Parameters
While getopts ": t:p:h-:" Optval
Todo
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)

# # Check if the user has Curl installed
Curl= ' which CURL 2>/dev/null '
[Z ' $CURL] && (echo "Need to install the CURL package."; exit 1)

# # Read Mirror site
mirrors= ' Curl--silent http://cygwin.com/mirrors.lst | Cut-d '; '-f1 '

# # Use CURL to detect time in sequence
Results= '
for Mirror in $mirrors; Todo
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 Test Result:"
ECHO-E $results | Sort-n | Sed ' 1d ' | head-$mirrors _count

# Vim:set Expandtab tabstop=4 shiftwidth=4:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.