Guest User

Untitled

a guest
Sep 6th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.66 KB | Software | 0 0
  1. #!/usr/bin/env bash
  2. # find-fastest-ubuntu-mirrors.sh
  3. set -u
  4.  
  5. SHORTLIST=50      # shortlist this many lowest-ping mirrors
  6. TOP_N=20          # output this many fastest mirrors
  7. PARALLEL_JOBS=8   # number of mirrors to test in parallel
  8. TEST_SIZE=$((5*1024*1024 - 1)) # 5 MB range request
  9. MIRRORS_PAGE="https://launchpad.net/ubuntu/+archivemirrors"
  10. TMPDIR=$(mktemp -d)
  11. ALL="$TMPDIR/all_mirrors.txt"
  12. SHORT="$TMPDIR/shortlist_mirrors.txt"
  13. SPEEDS="$TMPDIR/mirror_speeds.txt"
  14. OUT="fastest-ubuntu-mirrors.txt"
  15. TEST_FILE="ls-lR.gz"
  16.  
  17. trap 'rm -rf "$TMPDIR"' EXIT
  18.  
  19. echo "Fetching mirror list..."
  20. wget -q -O- "$MIRRORS_PAGE" \
  21.   | grep -P -B8 "statusUP" \
  22.   | grep -o -P "(f|ht)tp://[^\"]*" > "$ALL"
  23.  
  24. if [ ! -s "$ALL" ]; then
  25.   echo "Failed to fetch mirror list." >&2
  26.   exit 1
  27. fi
  28.  
  29. echo "Selecting $SHORTLIST lowest-latency mirrors with netselect..."
  30. if sudo netselect -s"$SHORTLIST" -t50 $(cat "$ALL") 2>/dev/null \
  31.    | awk '{
  32.       for(i=1;i<=NF;i++) if ($i ~ /^(http|ftp)/) print $i
  33.     }' > "$SHORT"; then
  34.   :
  35. else
  36.   echo "netselect failed or returned no output — falling back to first $SHORTLIST mirrors"
  37.   head -n "$SHORTLIST" "$ALL" > "$SHORT"
  38. fi
  39.  
  40. test_mirror() {
  41.   mirror="$1"
  42.   mirror="${mirror%/}"
  43.   url="$mirror/$TEST_FILE"
  44.  
  45.   # Primary test: 5 MB range request
  46.   speed_bytes=$(curl -s -L --connect-timeout 5 -m 30 \
  47.       --range 0-"$TEST_SIZE" -o /dev/null -w '%{speed_download}' "$url" 2>/dev/null || echo 0)
  48.  
  49.   # fallback without range (in case server doesn’t support it)
  50.   if [ -z "$speed_bytes" ] || [ "$speed_bytes" = "0" ]; then
  51.     speed_bytes=$(curl -s -L --connect-timeout 5 -m 30 \
  52.         -o /dev/null -w '%{speed_download}' "$url" 2>/dev/null || echo 0)
  53.   fi
  54.  
  55.   if [ -z "$speed_bytes" ] || [ "$speed_bytes" = "0" ]; then
  56.     echo "0 $mirror" >> "$SPEEDS"
  57.     echo "FAILED $mirror"
  58.   else
  59.     speed_int=$(awk -v s="$speed_bytes" 'BEGIN{printf "%d", s}')
  60.     echo "$speed_int $mirror" >> "$SPEEDS"
  61.  
  62.     if command -v numfmt >/dev/null 2>&1; then
  63.       hr=$(numfmt --to=iec --suffix=B/s "$speed_int")
  64.     else
  65.       hr=$(awk -v b="$speed_int" 'BEGIN{printf "%.1f KB/s", b/1024}')
  66.     fi
  67.     echo "OK     $mirror -> $hr"
  68.   fi
  69. }
  70.  
  71. export -f test_mirror
  72. export SPEEDS TEST_FILE TEST_SIZE
  73.  
  74. echo "Testing throughput (~5 MB per mirror) with $PARALLEL_JOBS jobs..."
  75. : > "$SPEEDS"
  76. cat "$SHORT" | xargs -n1 -P"$PARALLEL_JOBS" bash -c 'test_mirror "$@"' _
  77.  
  78. echo
  79. echo "Picking top $TOP_N mirrors..."
  80. sort -nr "$SPEEDS" | head -n "$TOP_N" > "$OUT"
  81.  
  82. echo "Top $TOP_N mirrors saved to: $OUT"
  83. nl -ba -w2 -s'. ' <(awk '{if (command="numfmt --to=iec --suffix=B/s " $1 | getline hr) {print hr, $2} else {printf "%.1f KB/s %s\n", $1/1024, $2}}' "$OUT")
Advertisement
Add Comment
Please, Sign In to add comment