Advertisement
ucomesdag

speedtest

Apr 22nd, 2022
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.19 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Copyright (C) 2016-2018 Uco Mesdag
  4. # Usage:    sh speedtest
  5.  
  6. test_result=$( mktemp )
  7.  
  8. YELLOW='\033[0;33m'
  9. GREEN='\033[0;32m'
  10. BLUE='\033[0;34m'
  11. RED='\033[0;31m'
  12. BOLD='\033[1m'
  13. NC='\033[0m'
  14.  
  15.  
  16. curl -o /tmp/speedtest https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py >/dev/null 2>&1
  17. chmod +x /tmp/speedtest
  18. clear
  19.  
  20. if [ "x$(echo $1 | grep '^[0-9]\{1,4\}$')" == "x" ]; then
  21.     echo " Retrieving server list..."
  22.     result=$(/tmp/speedtest --list 2>/dev/null | head -n10 )
  23.  
  24.     if [ $(echo ${result} | grep -c Error) -ne 0 ]; then
  25.         exit 1
  26.     fi
  27.  
  28.     clear
  29.     echo -e "${result}\n"
  30.  
  31.     read -p " Enter the server id you want to test against: " server_id
  32.     clear
  33. else
  34.     server_id=$1
  35. fi
  36.  
  37. if [ "x$(echo ${server_id} | grep '^[0-9]\{1,4\}$')"  != "x" ]; then
  38.     /tmp/speedtest --server ${server_id} --share --secure | tee ${test_result}
  39. else
  40.     /tmp/speedtest --share --secure | tee ${test_result}
  41. fi
  42.  
  43. rm /tmp/speedtest
  44.  
  45. get_ip_location() {
  46.     result=$(curl -s -X GET http://ip-api.com/line/$1?fields=countryCode,city)
  47.     country_code=$(echo -e "${result}" | sed '1!d')
  48.     city=$(echo -e "${result}" | sed '2!d')
  49.  
  50.     if [ "x${country_code}" != "x" ] && [ "${country_code}" != "Unknown" ]; then
  51.         country_code=" (${country_code})"
  52.     fi
  53.  
  54.     if [ "${city}" == "Unknown" ]; then
  55.         city=""
  56.     fi
  57.  
  58.     echo "${city}${country_code}"
  59. }
  60.  
  61. parse_result() {
  62.     ip=$(cat ${test_result} | grep 'Testing from' | grep -oE "[0-9]+.[0-9]+.[0-9]+.[0-9]+")
  63.     location=$(get_ip_location $ip)
  64.     provider=$(cat ${test_result} | grep 'Testing from' | cut -d ' ' -f 3-| rev | cut -d ' ' -f 2- | rev)
  65.     ping=$(cat ${test_result} | grep ms | rev | cut -d ' ' -f 1-2 | rev)
  66.     download=$(cat ${test_result} | grep Download | rev | cut -d ' ' -f 1-2 | rev)
  67.     upload=$(cat ${test_result} | grep Upload | rev | cut -d ' ' -f 1-2 | rev)
  68.     image_url=$(cat ${test_result} | grep Share | rev | cut -d ' ' -f 1 | rev)
  69.  
  70.     clear
  71.  
  72.     echo -e "${BOLD} ${provider} - ${location}${NC}"
  73.     echo -e "${GREEN}  〇\t${ping}${NC}"
  74.     echo -e "${RED}  ⬇\t${download}${NC}"
  75.     echo -e "${BLUE}  ⬆\t${upload}${NC}"
  76.     echo -e "${YELLOW}\n${image_url}${NC}"
  77. }
  78.  
  79. parse_result
  80.  
  81. rm ${test_result}
  82. exit 0
  83.  
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement