home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / netcat / examples / contrib / ncmeter
Encoding:
Text File  |  2006-06-19  |  2.0 KB  |  83 lines

  1. #! /bin/bash
  2.  
  3. # script to measure the speed of netcat.
  4. # start with one argument for usage information
  5. #
  6. # Tools that are used by this script are:
  7. # nc, bc, wc, sed, awk
  8. #
  9. # Author: Karsten Priegnitz (koem@petoria.de)
  10.  
  11. NCPORT=23457
  12. WAIT=1
  13.  
  14. # determine the programme's name
  15. me=`echo $0 | sed 's+.*/++'`
  16.  
  17. # check number of arguments provided
  18. if [ $# -ne 0 -a $# -ne 2 ]; then
  19.     echo "Usage:"
  20.     echo
  21.     echo "  On the transmitter side:"
  22.     echo "    $me <receivers ip-address> <amount of data>"
  23.     echo 
  24.     echo "  The <amount of data> is to be given in byte but you"
  25.     echo "  also can supply M or K for MegaByte and KiloByte."
  26.     echo "  Example: $me 10.1.1.3 20M"
  27.     echo
  28.     echo "  On the receiver side:"
  29.     echo "    $me"
  30.     echo 
  31.     echo "  Start $me on the receiver side before starting it"
  32.     echo "  on the transmitter side. Stop the receiver by pressing"
  33.     echo "  and holding Ctrl-C."
  34.     exit 1
  35. fi
  36.  
  37. # are we the receiver?
  38. if [ $# -eq 0 ]; then
  39.     # yes, we are
  40.     while true; do
  41.         echo "waiting to receive data... (quit: press and hold Ctrl-C)"
  42.         
  43.         # wait for data and count bytes
  44.         AMOUNT=`nc -v -w 120 -l -p $NCPORT | wc -c | awk '{print $1}'`
  45.         
  46.         # display amount of data received
  47.         echo $AMOUNT byte of data received
  48.         echo
  49.  
  50.         # sleep, so that the loop can be
  51.         # interrupted by pressing Ctrl-C
  52.         sleep 1
  53.     done
  54. fi
  55.  
  56. # we are the sender
  57. echo "sending data..."
  58.  
  59. # calculate the amount of data to be sent
  60. AMOUNT=`echo $2|sed s/[mM]/\*1048576/g | sed s/[kK]/\*1024/g | bc`
  61.  
  62. # send data and measure the time spent
  63. TEMP=/tmp/$me.tx
  64. ( time -p dd if=/dev/zero bs=$AMOUNT count=1 2>/dev/null | nc -v -w $WAIT $1 $NCPORT ) 2>"$TEMP" || cat "$TEMP" 
  65.  
  66. # read the time needed
  67. REAL=`grep "^real" "$TEMP" | awk '{print $2}'`
  68. rm "$TEMP"
  69. # subtract the wait times
  70. DOUBLEWAIT=$(($WAIT * 2))
  71. NEEDED=`echo $REAL - $DOUBLEWAIT|bc`
  72.  
  73. # calculate and print speed
  74. BPS=`echo "scale=3;$AMOUNT / $NEEDED"|bc`
  75. KBPS=`echo "scale=3;$AMOUNT / $NEEDED / 1024"|bc`
  76. MBPS=`echo "scale=3;$AMOUNT / $NEEDED / 1048576"|bc`
  77.  
  78. echo "time needed:      ${NEEDED}s"
  79. echo "byte per second:  $BPS"
  80. echo "KByte per second: $KBPS"
  81. echo "MByte per second: $MBPS"
  82.  
  83.