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: