SHARE
TWEET

speedtest-csv

a guest Jun 3rd, 2016 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2. ### Usage:
  3. ###  speedtest-csv [options]
  4. ###
  5. ### Options:
  6. ###  --debug        Output extra debug information
  7. ###  --header       Display field names (only)
  8. ###  --help         This help
  9. ###  --last         Use most recent stats, iff available
  10. ###  --quote <str>  Use <str> as quotation mark
  11. ###  --sep <str>    Use <str> as field separator
  12. ###  --standardize  Standardize units and number formats
  13. ###  --version      Display version
  14. ###
  15. ### Example:
  16. ###  speedtest-csv --standardize --sep '\t'
  17. ###
  18. ### Copyright: 2014-2016 Henrik Bengtsson
  19. ### License: GPL (>= 2.1) [http://www.gnu.org/licenses/gpl.html]
  20. call="$0 $*"
  21.  
  22. # Temporary file holding speedtest-cli output
  23. user=$USER
  24. if test -z $user; then
  25.     user=$USERNAME
  26. fi
  27. log=/tmp/$user/speedtest-csv.log
  28.  
  29. # Local functions
  30. function mecho() { echo "$@" 1>&2; }
  31. function mdebug() {
  32.     if [[ $debug == TRUE ]]; then
  33.         mecho "[DEBUG] $@";
  34.     fi
  35. }
  36. function str_extract() {
  37.     pattern=$1
  38.     # Extract
  39.     res=`grep "$pattern" $log | sed "s/$pattern//g"`
  40.     # Drop trailing ...
  41.     res=`echo $res | sed 's/[.][.][.]//g'`
  42.     # Trim
  43.     res=`echo $res | sed 's/^ *//g' | sed 's/ *$//g'`
  44.     echo $res
  45. }
  46.  
  47. debug=FALSE
  48. header=FALSE
  49. standardize=FALSE
  50. last=FALSE
  51. version=FALSE
  52. help=FALSE
  53.  
  54. # Character for separating values
  55. sep=";"
  56. quote=""
  57.  
  58. # Parse command-line options
  59. while [[ $# > 0 ]]; do
  60.     opt=$1
  61.     if test "$opt" == "--header"; then
  62.         header=TRUE
  63.     elif test "$opt" == "--quote"; then
  64.         quote=$2
  65.         shift
  66.     elif test "$opt" == "--sep"; then
  67.         sep=$2
  68.         shift
  69.     elif test "$1" == "--standardize"; then
  70.         standardize=TRUE
  71.     elif test "$1" == "--last"; then
  72.         last=TRUE
  73.     elif test "$1" == "--debug"; then
  74.         debug=TRUE
  75.     elif test "$1" == "--version"; then
  76.         version=TRUE
  77.     elif test "$1" == "--help"; then
  78.         help=TRUE
  79.     fi
  80.     shift
  81. done
  82.  
  83. if [[ -n "$SPEEDTEST_CSV_SKIP" ]]; then
  84.     mdebug "\$SPEEDTEST_CSV_SKIP => --last"
  85.     last=TRUE
  86. fi
  87.  
  88. if [[ $debug == TRUE ]]; then
  89.     mdebug "call: $call"
  90.     mdebug "header: $header"
  91.     mdebug "standardize: $standardize"
  92.     mdebug "last: $last"
  93.     mdebug "version: $version"
  94.     mdebug "field delimeter: '$sep'"
  95.     mdebug "quotation mark: $quote"
  96. fi
  97.  
  98. if [[ $version == TRUE ]]; then
  99.     echo "1.1.1"
  100.     exit 0
  101. fi
  102.  
  103. if [[ $help == TRUE ]]; then
  104.     grep "^###" $0 | grep -v "^####" | cut -b 5-
  105.     exit 0
  106. fi
  107.  
  108. # Display header?
  109. if [[ $header == TRUE ]]; then
  110.     mdebug "Generating header"
  111.     start="start"
  112.     stop="stop"
  113.     from="from"
  114.     from_ip="from_ip"
  115.     server="server"
  116.     server_dist="server_dist"
  117.     server_ping="server_ping"
  118.     download="download"
  119.     upload="upload"
  120.     share_url="share_url"
  121. else
  122.     mkdir -p $(dirname $log)
  123.  
  124.     start=$(date +"%Y-%m-%d %H:%M:%S")
  125.  
  126.     if [[ $last == TRUE && -f "$log" ]]; then
  127.         # Reuse existing results (useful for debugging)
  128.         mdebug "Reusing existing results: $log"
  129.     else
  130.         # Query Speedtest
  131.         mdebug "Querying Speedtest using 'speedtest-cli --share'"
  132.         speedtest-cli --share > $log
  133.     fi
  134.    
  135.     stop=$(date +"%Y-%m-%d %H:%M:%S")
  136.    
  137.     # Parse
  138.     mdebug "Parsing results"
  139.     from=$(str_extract "Testing from ")
  140.     from_ip=$(echo $from | sed 's/.*(//g' | sed 's/).*//g')
  141.     from=$(echo $from | sed 's/ (.*//g')
  142.     server=$(str_extract "Hosted by ")
  143.     server_ping=$(echo $server | sed 's/.*: //g')
  144.     server=$(echo $server | sed 's/: .*//g')
  145.     server_dist=$(echo $server | sed 's/.*\[//g' | sed 's/\].*//g')
  146.     server=$(echo $server | sed 's/ \[.*//g')
  147.    
  148.     download=$(str_extract "Download: ")
  149.     upload=$(str_extract "Upload: ")
  150.     share_url=$(str_extract "Share results: ")
  151.  
  152.     # Standardize units?
  153.     if [[ $standardize == TRUE ]]; then
  154.         ## Mbits/s -> Mbit/s
  155.         mdebug "Standardize to Mbit/s"
  156.         download=$(echo $download | sed 's/Mbits/Mbit/')
  157.         upload=$(echo $upload | sed 's/Mbits/Mbit/')
  158.  
  159.         ## commas to periods
  160.         mdebug "Standardize to periods (not commas)"
  161.         download=$(echo $download | sed 's/,/./')
  162.         upload=$(echo $upload | sed 's/,/./')
  163.         server_dist=$(echo $server_dist | sed 's/,/./')
  164.         server_ping=$(echo $server_ping | sed 's/,/./')
  165.     fi
  166.  
  167.     mdebug "start: '$start'"
  168.     mdebug "stop: '$stop'"
  169.     mdebug "from: '$from'"
  170.     mdebug "from_ip: '$from_ip'"
  171.     mdebug "server: '$server'"
  172.     mdebug "server_dist: '$server_dist'"
  173.     mdebug "server_ping: '$server_ping'"
  174.     mdebug "download: '$download'"
  175.     mdebug "upload: '$upload'"
  176.     mdebug "share_url: '$share_url'"
  177. fi
  178.  
  179.  
  180. # Output CSV results
  181. sep="$quote$sep$quote"
  182. #printf "$quote$start$sep$stop$sep$from$sep$from_ip$sep$server$sep$server_dist$sep$server_ping$sep$download$sep$upload$sep\n"
  183.  
  184. echo $start "Upload:" $upload "Download" $download
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top