Skip to content

Commit

Permalink
Merge pull request #6 from deajan/2.3
Browse files Browse the repository at this point in the history
Version 2.3 release
  • Loading branch information
deajan authored Oct 3, 2019
2 parents 851d662 + b02437f commit db1f2dd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 54 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# tcpping
# tcpping v2.x

## fork of elder tcpping v1.8 script running with newer traceroute binary

This script is a fork of Richard Van de Berg's tcpping script, supporiting newer traceroute binaries.
This script is a fork of Richard Van de Berg's tcpping script, original version found [here](https://github.com/deajan/tcpping/tree/original-1.8), supporting newer traceroute binaries.
Usage is the same and should work out of the box for smokeping.

It comes with various improvements like language agonstic functionality, better error detection, debug, and removed tcptraceroute dependancy.

This is still a basic work and has only been tested on CentOS 7, FreeBSD 11 and smokeping 2.7.2 so far.
On CentOS 7, traceroute 2.0.22 has been tested.
On FreeBSD, traceroute 1.4a12 has been tested.
So far, tests have been done with:
- CentOS 7: v2.0-2.3 tested (with traceroute 2.0.22 and bash 4.2.46(2)-release)
- CentOS 8: v2.3 tested (with traceroute 2.1.0 and bash 4.4.19(1)-release)
- FreeBSD 11.2: v2.0-2.3 tested (with traceroute 1.4a12 and tcsh 6.20.00)
Also, smokeping 2.7.2 and 2.7.3 have been tested.

## Installation

Expand Down
126 changes: 77 additions & 49 deletions tcpping
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#
# uses recent versions of traceroute supporting TCP sessions
#
# (c) 2002-2018 Richard van den Berg <[email protected]> under the GPL
# (c) 2002-2019 Richard van den Berg <[email protected]> under the GPL
# http://www.gnu.org/copyleft/gpl.html
# Orsiris de Jong <[email protected]>
#
#
# 2002/12/20 v1.0 initial version
# 2003/01/25 v1.1 added -c and -r options
# now accepting all other tcptraceroute options
Expand All @@ -35,9 +35,14 @@
# added -Z parameter for sudo
# added -M parameter for protocol
# removed bc dependancy
# fixed bogus -w parameter

ver="v2.2"
# fixed bogus -w parameter
# 2019/09/25 v2.3 allow -w parameter to take floats under linux
# meantime between checks is now lowered if -w parameter is less than 1 second on linux
# added -o parameter, which outputs statistics similar to ping
# Simpify main loop
# fix ambiguous output redirect in csh for preflight check

ver="v2.3"
format="%Y%m%d%H%M%S"
d="no"
c="no"
Expand All @@ -53,9 +58,8 @@ SUDO_COMMAND=""
_DEBUG=false
LOCAL_OS=
METHOD=tcp
#WIP remove
protoOptions="-T"
proto=tcp
summary="no"
x=0

# Make sure traceroute output is language agnostic
export LANG=C
Expand All @@ -69,7 +73,7 @@ usage () {
echo " -d print timestamp before every result"
echo " -c print a columned result line"
echo " -C print in the same format as fping's -C option"
echo " -w wait time in seconds (defaults to 3)"
echo " -w wait time in seconds (defaults to 3). Linux supports float values, ie '.2'"
echo " -r repeat every n seconds (defaults to 1)"
echo " -x repeat n times (defaults to unlimited)"
echo " -f first ttl (defaults to 255), see traceroute man"
Expand All @@ -79,6 +83,7 @@ usage () {
echo " --sport define source port, see traceroute man"
echo " -z show what command is actually sent to traceroute (debug)"
echo " -Z run traceroute with sudo"
echo " -o Output ping like statistics"
echo
echo "Default port is 80"
echo "See also: man traceroute"
Expand All @@ -98,11 +103,11 @@ getLocalOS() {
if grep -i Microsoft /proc/sys/kernel/osrelease > /dev/null 2>&1; then
localOsVar="Microsoft"
else
localOsVar="$(uname -spior 2>&1)"
localOsVar="`uname -spior 2>&1`"
if [ $? != 0 ]; then
localOsVar="$(uname -v 2>&1)"
localOsVar="`uname -v 2>&1`"
if [ $? != 0 ]; then
localOsVar="$(uname)"
localOsVar="`uname`"
fi
fi
fi
Expand Down Expand Up @@ -141,10 +146,15 @@ getLocalOS() {
}

checkEnvironment() {
if ! type traceroute > /dev/null 2>&1 ; then
if ! type traceroute > /dev/null; then
echo >&2 "traceroute binary not found. Please install it first"
exit 1
fi

if ! type awk > /dev/null; then
echo >&2 "awk binary not found. Please install it first"
exit 2
fi
}

# Check if site can be reached via TCP SYN
Expand All @@ -171,7 +181,7 @@ _checkSite() {

traceRoute=`$traceRouteCommand 2>&1`
if [ $? -ne 0 ]; then
if [ "$(id -u)" -ne 0 ]; then
if [ "`id -u`" -ne 0 ]; then
echo >&2 "Unable to run '$traceRouteCommand' command. Please try run $0 with -Z parameter or 'sudo $0'"
return 20
else
Expand Down Expand Up @@ -238,7 +248,7 @@ _testSite() {
if [ "x${rtt}" != "x" -a "x${not}" = "x" ]; then
echo "$myseq $nows $rtt $host"
else
echo "$myseq $nows $max $host"
echo "$myseq $nows $maxRtt $host"
fi
elif [ "${C}" = "yes" ]; then
if [ "$myseq" = "0" ]; then
Expand All @@ -259,11 +269,20 @@ _testSite() {
else
echo "${traceRoute}" | sed -e "s/^.*\*.*$/seq $myseq: no response (timeout)/" -e "s/^$ttl /seq $myseq: tcp response from/"
fi

echo "${traceRoute}" | awk '($1 == "*" || $2 == "*"){ exit 1 }'
return $?
}

_quit() {
echo ""
echo "${pingCount} packets transmitted, `expr ${pingCount} - ${pingFailCount}` received."
exit
}

checkEnvironment

while getopts Zdhzq:w:cr:nNFSAEi:f:l:m:p:s:x:CM: opt ; do
while getopts Zdhzq:w:cr:nNFSAEi:f:l:m:p:s:x:CM:o opt ; do
case "$opt" in
d|c|C) eval $opt="yes" ;;
q|r|x) eval $opt="$OPTARG" ;;
Expand All @@ -277,13 +296,14 @@ while getopts Zdhzq:w:cr:nNFSAEi:f:l:m:p:s:x:CM: opt ; do
icmp) ;;
tcp) ;;
udp) ;;
*)
*)
echo >&2 "Unsupported method. Falling back to TCP"
METHOD=tcp
;;
esac ;;
Z) SUDO_COMMAND=sudo ;;
z) _DEBUG=true ;;
o) summary="yes" ;;
?) usage; exit ;;
esac
done
Expand All @@ -296,15 +316,19 @@ if [ "x$1" = "x" ]; then
exit
fi

max=$((${timeToWait} * 1000))
if [ "${summary}" = "yes" ]; then
trap _quit SIGQUIT INT
fi

# Use awk to multiply possible float in timeToWait
maxRtt=`awk 'BEGIN{printf "%.2f\n", ('${timeToWait}'*1000)}'`

if [ `date +%s` != "%s" ]; then
format="%s"
fi

getLocalOS

#WIP check here
if [ "$LOCAL_OS" = "BSD" ] || [ "$LOCAL_OS" = "MacOSX" ]; then
METHOD_PARAMETER="-P"
else
Expand Down Expand Up @@ -338,37 +362,41 @@ err=$?

[ ${err} -gt 0 ] && exit ${err}

if [ "$x" = "" ]; then
while [ true ] ; do
_testSite "${host}" "${port}" ${seq} ${topt} &
pid=$!
# if timeToWait is lower than repeatWaitTime (1 sec), repeatWaitTime should be equal to timeToWait in order to speed up pings after timeout
if ! awk 'BEGIN { exit "'$repeatWaitTime'">"'$timeToWait'" }'; then
repeatWaitTime=$timeToWait
fi

if [ "${C}" = "yes" ]; then
wait $pid
fi
seq=`expr $seq + 1`
if [ $seq -gt 0 ]; then
_DEBUG=false
fi
sleep ${repeatWaitTime}
done
else
while [ "$x" -gt 0 ] ; do
_testSite "${host}" "${port}" ${seq} ${topt} &
pid=$!
pingCount=0
pingFailCount=0

if [ "${C}" = "yes" ]; then
wait $pid
fi
seq=`expr $seq + 1`
if [ $seq -gt 0 ]; then
_DEBUG=false
fi
x=`expr $x - 1`
if [ "$x" -gt 0 ]; then
sleep ${repeatWaitTime}
fi
done
fi
while [ $pingCount -lt $x ] || [ $x -eq 0 ]; do
result=""
_testSite "${host}" "${port}" ${seq} ${topt} &
pid=$!

if [ "${C}" = "yes" ]; then
wait $pid
result=$?
fi
seq=`expr $seq + 1`
if [ $seq -gt 0 ]; then
_DEBUG=false
fi
if [ "$result" = "" ]; then
wait $pid > /dev/null 2>&1
result=$?
fi

pingCount=`expr $pingCount + 1`
if [ $result -ne 0 ]; then
pingFailCount=`expr $pingFailCount + 1`
fi
if [ $pingCount -lt $x ] || [ $x -eq 0 ]; then
sleep ${repeatWaitTime}
fi
done
if [ "$summary" = "yes" ]; then
_quit
fi
exit

0 comments on commit db1f2dd

Please sign in to comment.