BASH   47

tcpping

Guest on 29th August 2021 07:06:13 AM

  1. #!/bin/sh
  2. #
  3. # tcpping: test response times using TCP SYN packets
  4. #          URL: http://www.vdberg.org/~richard/tcpping.html
  5. #
  6. # uses tcptraceroute from http://michael.toren.net/code/tcptraceroute/
  7. #
  8. # (c) 2002-2005 Richard van den Berg <richard@vdberg.org> under the GPL
  9. #               http://www.gnu.org/copyleft/gpl.html
  10. #
  11. # 2002/12/20 v1.0 initial version
  12. # 2003/01/25 v1.1 added -c and -r options
  13. #                 now accepting all other tcptraceroute options
  14. # 2003/01/30 v1.2 removed double quotes around backquotes
  15. # 2003/03/25 v1.3 added -x option, courtesy of Alvin Austin <alvin@crlogic.com>
  16. # 2005/03/31 v1.4 added -C option, courtesy of Norman Rasmussen <norman@rasmussen.org>
  17. # 2007/01/11 v1.5 catch bad destination addresses
  18. # 2007/01/19 v1.6 catch non-root tcptraceroute
  19. # 2008/02/10 v1.7 make -C work when reverse lookup fails, courtesy of Fabrice Le Dorze <Fabrice.LeDorze@apx.fr>
  20.  
  21.  
  22. ver="v1.7"
  23. format="%Y%m%d%H%M%S"
  24. d="no"
  25. c="no"
  26. C="no"
  27. ttl=255
  28. seq=0
  29. q=1
  30. r=1
  31. w=3
  32. topts=""
  33.  
  34. usage () {
  35.         name=`basename $0`
  36.         echo "tcpping $ver Richard van den Berg <richard@vdberg.org>"
  37.         echo
  38.         echo "Usage: $name [-d] [-c] [-C] [-w sec] [-q num] [-x count] ipaddress [port]"
  39.         echo
  40.         echo "        -d   print timestamp before every result"
  41.         echo "        -c   print a columned result line"
  42.         echo "        -C   print in the same format as fping's -C option"
  43.         echo "        -w   wait time in seconds (defaults to 3)"
  44.         echo "        -r   repeat every n seconds (defaults to 1)"
  45.         echo "        -x   repeat n times (defaults to unlimited)"
  46.         echo
  47.         echo "See also: man tcptraceroute"
  48.         echo
  49. }
  50.  
  51. _checksite() {
  52.         ttr=`tcptraceroute -f ${ttl} -m ${ttl} -q ${q} -w ${w} $* 2>&1`
  53.         if echo "${ttr}" | egrep -i "(bad destination|got roo)" >/dev/null 2>&1; then
  54.                 echo "${ttr}"
  55.                 exit
  56.         fi
  57. }
  58.        
  59. _testsite() {
  60.         myseq="${1}"
  61.         shift
  62.         [ "${c}" = "yes" ] && nows=`date +${format}`
  63.         [ "${d}" = "yes" ] && nowd=`date`
  64.         ttr=`tcptraceroute -f ${ttl} -m ${ttl} -q ${q} -w ${w} $* 2>/dev/null`
  65.         host=`echo "${ttr}" | awk '{print $2 " " $3}'`
  66.         rtt=`echo "${ttr}" | sed 's/.*] //' | awk '{print $1}'`
  67.         not=`echo "${rtt}" | tr -d ".0123456789"`
  68.         [ "${d}" = "yes" ] && echo "$nowd"
  69.         if [ "${c}" = "yes" ]; then
  70.                 if [ "x${rtt}" != "x" -a "x${not}" = "x" ]; then
  71.                         echo "$myseq $nows $rtt $host"
  72.                 else
  73.                         echo "$myseq $nows $max $host"
  74.                 fi
  75.         elif [ "${C}" = "yes" ]; then
  76.                 if [ "$myseq" = "0" ]; then
  77.                         echo -n "$1 :"
  78.                 fi
  79.                 if [ "x${rtt}" != "x" -a "x${not}" = "x" ]; then
  80.                         echo -n " $rtt"
  81.                 else
  82.                         echo -n " -"
  83.                 fi
  84.                 if [ "$x" = "1" ]; then
  85.                         echo
  86.                 fi
  87.         else
  88.                 echo "${ttr}" | sed -e "s/^.*\*.*$/seq $myseq: no response (timeout)/" -e "s/^$ttl /seq $myseq: tcp response from/"
  89.         fi
  90. #       echo "${ttr}"
  91. }
  92.  
  93. while getopts dhq:w:cr:nNFSAEi:f:l:m:p:s:x:C opt ; do
  94.         case "$opt" in
  95.                 d|c|C) eval $opt="yes" ;;
  96.                 q|w|r|x) eval $opt="$OPTARG" ;;
  97.                 n|N|F|S|A|E) topt="$topt -$opt" ;;
  98.                 i|l|p|s) topt="$topt -$opt $OPTARG" ;;
  99.                 f|m) ttl="$OPTARG" ;;
  100.                 ?) usage; exit ;;
  101.         esac
  102. done
  103.  
  104. shift `expr $OPTIND - 1`
  105.  
  106. if [ "x$1" = "x" ]; then
  107.         usage
  108.         exit
  109. fi
  110.  
  111. max=`echo "${w} * 1000" | bc`
  112.  
  113. if [ `date +%s` != "%s" ]; then
  114.         format="%s"
  115. fi
  116.  
  117. _checksite ${topt} $*
  118.  
  119. if [ "$x" = "" ]; then
  120.         while [ 1 ] ; do
  121.                 _testsite ${seq} ${topt} $* &
  122.                 pid=$!
  123.                 if [ "${C}" = "yes" ]; then
  124.                         wait $pid
  125.                 fi
  126.                 seq=`expr $seq + 1`
  127.                 sleep ${r}
  128.         done
  129. else
  130.         while [ "$x" -gt 0 ] ; do
  131.                 _testsite ${seq} ${topt} $* &
  132.                 pid=$!
  133.                 if [ "${C}" = "yes" ]; then
  134.                         wait $pid
  135.                 fi
  136.                 seq=`expr $seq + 1`
  137.                 x=`expr $x - 1`
  138.                 if [ "$x" -gt 0 ]; then
  139.                         sleep ${r}
  140.                 fi
  141.         done
  142. fi
  143.  
  144. exit

Raw Paste


Login or Register to edit or fork this paste. It's free.