Advertisement
fennectech

Untitled

Feb 22nd, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.06 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #              bash 4.1.5(1)     Linux Ubuntu 10.04               Date : 2013-07-11
  3. #
  4. # _______________|  netspeed : check download speed via command line.
  5. #
  6. #           Usage:  netspeed [tokyo, london, usw, use, east, west, URL]
  7. #                                            ^default U.S. west coast.
  8. #                            [ -speed_KB/sec ]
  9. #                              ^negation activates the Mbps converter.
  10. #
  11. #                            First try it out without any arguments.
  12. #                                            
  13. #    Dependencies:  wget
  14. #                   (rate displayed in Kilobytes per second,
  15. #                    whereas speed tests usually adopt Megabits.)
  16. #
  17. #   Example units:  Download Speed: 659 kbps     (82.4 KB/sec transfer rate)
  18. #                     Upload Speed: 317 kbps     (39.6 KB/sec transfer rate)
  19. #                                       kb=kilobits    KB=Kilobytes
  20. #                   So  0.007813 Mb =    8 kilobits =   1 Kilobyte
  21. #                   since      1 Mb = 1024 kilobits = 128 Kilobytes
  22. #
  23. #           Units:  b stands for bit and B stands for byte. In the context of
  24. #                   data rate units, one byte refers to 8 bits. For example,
  25. #                   when a 1 Mbps connection is advertised, it usually means
  26. #                   that the maximum achievable download bandwidth is
  27. #                   1  megabit/s (million bits per second), which is actually
  28. #                   0.125   MB/s (megabyte per second), or about
  29. #                   0.1192 MiB/s (mebibyte per second).
  30. #                   http://en.wikipedia.org/wiki/Mbit/s#megabit_per_second
  31.  
  32.  
  33. #  CHANGE LOG  get LATEST version from https://bitbucket.org/rsvp/gists/src
  34. #
  35. #  2013-07-11  Modify linode.com domains which have test files,
  36. #                 see https://www.linode.com/speedtest/
  37. #  2011-10-09  Add rate conversion to Mbps using negated first argument.
  38. #  2011-10-08  Add URL as possible arg. Clarify rate units.
  39. #                 Public as gist to https://gist.github.com/1272488
  40. #  2011-10-07  First version based on wget, though curl also works:
  41. #                 http://stackoverflow.com/questions/426272
  42. #                 Command line alternative to http://www.speedtest.net
  43.  
  44. #           _____ Prelims
  45. set -u
  46. #   ^ unbound (i.e. unassigned) variables shall be errors.
  47. set -e
  48. #   ^ error checking :: Highly Recommended (caveat:  you can't check $? later).  
  49. #
  50. # _______________     ::  BEGIN  Script ::::::::::::::::::::::::::::::::::::::::
  51.  
  52. site=${1:-'usw'}
  53.  
  54.  
  55. case $site in
  56.      'london') test='http://speedtest.london.linode.com/100MB-london.bin'    ;;
  57.      'tokyo')  test='http://speedtest.tokyo.linode.com/100MB-tokyo.bin'      ;;
  58.      'usw')    test='http://speedtest.fremont.linode.com/100MB-fremont.bin'  ;;
  59.                #  US West:  Fremont, California
  60.      'use')    test='http://speedtest.newark.linode.com/100MB-newark.bin'    ;;
  61.                #  US East:  Newark, New Jersey
  62.      'east')   test='http://speedtest.wdc01.softlayer.com/downloads/test500.zip'                 ;;
  63.                #  US East:  Washington, D.C.
  64.      'west')   test='http://speedtest.sjc01.softlayer.com/speedtest/speedtest/random500x500.jpg' ;;
  65.                #  US West:  San Jose, California
  66.      *)        test=$1  ;;
  67.                #  valid URL assumed, else wget will give any error message.
  68. esac
  69.      #  Sources:
  70.      #  2013-07-11  Linode renames sub-domains for testing.
  71.      #  2011-10-07  http://www.linode.com/speedtest/      for file URL.
  72.      #              http://speedtest.wdc01.softlayer.com  for actual testing.
  73.  
  74.  
  75.  
  76. if [ $test = -${test#-} ] ; then
  77.      #     ^check for negative sign in the first argument.
  78.      echo "`awk "BEGIN { print ($test * -0.007813) }"` Mbps"
  79.      #      awk as nice floating-point calculator!
  80. else
  81.      echo ' ::  Rate in Kilobytes/sec; industry usually adopts megabits/sec:       '
  82.      echo ' ::        1 Kilobytes/sec (K/s)   =       0.007813 megabits/sec (Mbps).'
  83.      echo ' ::        To CONVERT, use negated rate as first argument to netspeed.  '
  84.      echo ' ::  [Terminate netspeed by control-C]     [ cf. http://speedtest.net ] '
  85.      echo ' ::  '
  86.      trap 'echo -e "\n\n ::  Exit successful on designed interrupt."; exit 0' SIGINT
  87.      #      MAIN
  88.      wget  --output-document=/dev/null  $test
  89. fi
  90.  
  91. #       If --output-file for wget was used to log results...
  92. #
  93. #  --2011-10-07 21:38:27--  
  94. #  Resolving speedtest.sjc01.softlayer.com... 50.23.64.58
  95. #  Connecting to speedtest.sjc01.softlayer.com|50.23.64.58|:80... connected.
  96. #  HTTP request sent, awaiting response... 200 OK
  97. #  Length: 505544 (494K) [image/jpeg]
  98. #  Saving to: `/dev/null'
  99. #  
  100. #       0K .......... .......... .......... .......... .......... 10% 80.9K 5s
  101. #      50K .......... .......... .......... .......... .......... 20% 78.5K 5s
  102. #     100K .......... .......... .......... .......... .......... 30% 80.8K 4s
  103. #     150K .......... .......... .......... .......... .......... 40% 78.8K 4s
  104. #     200K .......... .......... .......... .......... .......... 50% 80.8K 3s
  105. #     250K .......... .......... .......... .......... .......... 60% 78.5K 2s
  106. #     300K .......... .......... .......... .......... .......... 70% 78.8K 2s
  107. #     350K .......... .......... .......... .......... .......... 81% 80.8K 1s
  108. #     400K .......... .......... .......... .......... .......... 91% 78.5K 1s
  109. #     450K .......... .......... .......... .......... ...       100% 81.9K=6.2s
  110. #  
  111. #  2011-10-07 21:38:33 (79.8 KB/s) - `/dev/null' saved [505544/505544]
  112.  
  113.  
  114. exit 0
  115. # _______________ EOS ::  END of Script ::::::::::::::::::::::::::::::::::::::::
  116.  
  117.  
  118. #  #  Alternative to wget...
  119. #  curl -o /dev/null  $test
  120. #  #  curl is less verbose than wget. Speed is displayed as bytes per second:
  121. #  #
  122. #  #    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
  123. #  #                                   Dload  Upload   Total   Spent    Left  Speed
  124. #  #    0  500M    0 4931k    0     0  80993      0  1:47:53  0:01:02  1:46:51 81520
  125.  
  126.  
  127. #  vim: set fileencoding=utf-8 ff=unix tw=78 ai syn=sh :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement