summarylogtreecommitdiffstats
path: root/tcpping
blob: 4e8d26e781d263c715994c4e108802d01806a0d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/bin/sh
#
# tcpping: test response times using TCP SYN packets
#          URL: https://github.com/deajan/tcpping
#          Former URL: http://www.vdberg.org/~richard/tcpping.html
#
# uses recent versions of traceroute supporting TCP sessions
#
# (c) 2002-2020 Richard van den Berg <richard@vdberg.org> under the GPL
#               http://www.gnu.org/copyleft/gpl.html
#               Orsiris de Jong <ozy@netpower.fr>
#
# 2002/12/20 v1.0 initial version
# 2003/01/25 v1.1 added -c and -r options
#                 now accepting all other tcptraceroute options
# 2003/01/30 v1.2 removed double quotes around backquotes
# 2003/03/25 v1.3 added -x option, courtesy of Alvin Austin <alvin@crlogic.com>
# 2005/03/31 v1.4 added -C option, courtesy of Norman Rasmussen <norman@rasmussen.org>
# 2007/01/11 v1.5 catch bad destination addresses
# 2007/01/19 v1.6 catch non-root tcptraceroute
# 2008/02/10 v1.7 make -C work when reverse lookup fails, courtesy of Fabrice Le Dorze <Fabrice.LeDorze@apx.fr>
# 2010/06/04 v1.8 make -C work when ipaddress doesn't reply, courtesy of Yann Beulque
# From v2.0 upwards, maintainer is Orsiris de Jong <ozy@netpower.fr>
# 2018/04/25 v2.0 make tcpping work with recent traceroute binary (tested with version 2.0.22)
#                 added language agonstic traceroute support
#                 added FreeBSD 11 traceroute and csh support
#                 added support for other ttl than 255
#                 added -z debug parameter which shows what is actually sent to traceroute
#                 drop tcptraceroute prerequisite
#                 removed elder options (-l, -p which is defined as ending optional argument)
#                 multiple small improvements (local variables, better readability)
# 2018/07/11 v2.1 added preflight checks for traceroute and bc binaries
#                 comparaisons are now strict postix, thanks to https://github.com/agail
# 2018/11/26 v2.2 added checks for root privileges, courtesy of Jim Conner <jimconn91343@gmail.com>
#                 added -Z parameter for sudo
#                 added -M parameter for protocol
#                 removed bc dependancy
#                 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
# 2020/04/19 v2.4 fixed -r parameter, which also accepts floats now, and added -r auto option, which calculates
#                 a wait interval based on timeToWait (-w) parameter
#                 Do not wait additional interval time when ping fails before running next try
#                 Remove _checkSite function (merged into _testSite) to avoid code duplicartion, and lower execution time
#                 Set default wait interval (-r) parameter to auto
#                 Set default timeToWait parameter to 1 second
# v2.5-dev        Fixed set -o pipefail only exists on bash

ver="v2.5dev"
format="%Y%m%d%H%M%S"
d="no"
c="no"
C="no"
f_ttl=255
m_ttl=255
seq=0
numberOfQueries=1
interval=auto
timeToWait=1
topts=""
SUDO_COMMAND=""
_DEBUG=false
LOCAL_OS=
METHOD=tcp
summary="no"
x=0

# Make sure traceroute output is language agnostic
export LANG=C

# Make sure we get exit codes from piped commands
[ -n "$BASH_VERSION" ] && set -o pipefail

usage () {
	name=`basename $0`
	echo "tcpping $ver Richard van den Berg <richard@vdberg.org>, Orsiris de Jong <ozy@netpower.fr>"
	echo
	echo "Usage: $name [-d] [-c] [-C] [-w sec] [-q num] [-x count] ipaddress [port]"
	echo
	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). Linux supports float values, ie '.2'"
	echo "        -r            repeat every n seconds (defaults to 1). Linux supports float values, ie '.2'"
	echo "                      also accepts 'auto' value which works with -x in order to make total execution time lower than interval * repeats"
	echo "        -x            repeat n times (defaults to unlimited)"
	echo "        -f            first ttl (defaults to 255), see traceroute man"
	echo "        -m            max ttl (defaults to 255), see traceroute man"
	echo "        -nNFASEisfm   see traceroute man"
	echo "        -M            method (tcp, udp, icmp...), see traceroute man"
	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"
	echo
}

getLocalOS() {
	local localOsVar
	local localOsName
	local localOsVer

	# There is no good way to tell if currently running in BusyBox shell. Using sluggish way.
	if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then
		localOsVar="BusyBox"
	else
		# Detecting the special ubuntu userland in Windows 10 bash
		if grep -i Microsoft /proc/sys/kernel/osrelease > /dev/null 2>&1; then
			localOsVar="Microsoft"
		else
			localOsVar="`uname -spior 2>&1`"
			if [ $? != 0 ]; then
				localOsVar="`uname -v 2>&1`"
				if [ $? != 0 ]; then
					localOsVar="`uname`"
				fi
			fi
		fi
	fi

	case $localOsVar in
		# Android uname contains both linux and android, keep it before linux entry
		*"Android"*)
		LOCAL_OS="Android"
		;;
		*"Linux"*)
		LOCAL_OS="Linux"
		;;
		*"BSD"*)
		LOCAL_OS="BSD"
		;;
		*"MINGW32"*|*"MINGW64"*|*"MSYS"*)
		LOCAL_OS="msys"
		;;
		*"CYGWIN"*)
		LOCAL_OS="Cygwin"
		;;
		*"Microsoft"*)
		LOCAL_OS="WinNT10"
		;;
		*"Darwin"*)
		LOCAL_OS="MacOSX"
		;;
		*"BusyBox"*)
		LOCAL_OS="BusyBox"
		;;
		*)
		echo >&2 "Running on unknown local OS [$localOsVar]."
		;;
	esac
}

checkEnvironment() {
	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

# Measure latency via TCP SYN
_testSite() {
	local host="${1}"
	local port="${2:-80}"
	local myseq="${3}"

	local args=
	local i=1
	for givenArgs in "${@}"; do
		if [ $i -gt 3 ]; then
			args="$args $givenArgs"
		fi
		i=`expr $i + 1`
	done

	local traceRoute=
	local traceRouteCommand=
	local foundHost=
	local rtt=

	shift
	[ "${c}" = "yes" ] && nows=`date +${format}`
	[ "${d}" = "yes" ] && nowd=`date`

	# Remove first line from result
	traceRouteCommand="$SUDO_COMMAND traceroute ${METHOD_PARAMETER} ${METHOD} -f ${f_ttl} -m ${m_ttl} -q ${numberOfQueries} -w ${timeToWait} ${args} -p ${port} ${host}"
	if [ $_DEBUG = true ]; then
		echo "$traceRouteCommand"
	fi

	# BSD traceroute outputs header line to stderr while outputting results to stdout, whereas linux versions output everything to stdout
	if [ "$LOCAL_OS" = "BSD" ] || [ "$LOCAL_OS" = "MacOSX" ]; then
		traceRoute=`$traceRouteCommand 2>/dev/null`
	else
		traceRoute=`$traceRouteCommand 2>/dev/null | awk 'NR>1'`
	fi
	result=$?

	if [ $myseq -eq 0 ]; then
		if [ $result -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'"
				exit 20
			else
				echo >&2 "Unable to run '$traceRouteCommand' command. Please submit an issue in 'https://github.com/deajan/tcpping/issues' with the output of the command."
				exit 21
			fi
		fi

		if echo "${traceRoute}" | egrep -i "(bad destination|got roo|not known|cannot handle)" >/dev/null 2>&1; then
			echo >&2 "${traceRoute}"
			exit 22
		fi
	fi

	rtt=`echo "${traceRoute}" | awk '{print $4}'`
	not=`echo "${rtt}" | tr -d ".0123456789"`

	[ "${d}" = "yes" ] && echo "$nowd"
	if [ "${c}" = "yes" ]; then
		if [ "x${rtt}" != "x" -a "x${not}" = "x" ]; then
			echo "$myseq $nows $rtt $host"
		else
			echo "$myseq $nows $maxRtt $host"
		fi
	elif [ "${C}" = "yes" ]; then
		if [ "$myseq" = "0" ]; then
			echo -n "$host :"
		fi
		if [ "x${rtt}" != "x" -a "x${not}" = "x" ]; then
			if [ $rtt != "255" ]; then
				echo -n " $rtt"
			else
				echo -n " -"
			fi
		else
			echo -n " -"
		fi
		if [ "$x" = "1" ]; then
			echo
		fi
	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:o opt ; do
	case "$opt" in
		d|c|C) eval $opt="yes" ;;
		q|x) eval $opt="$OPTARG" ;;
		r) interval="$OPTARG" ;;
		n|N|F|S|A|E) topt="$topt -$opt" ;;
		i|s) topt="$topt -$opt $OPTARG" ;;
		w) timeToWait="$OPTARG" ;;
		f) f_ttl="$OPTARG" ;;
		m) m_ttl="$OPTARG" ;;
		M) METHOD="$OPTARG" ;;
		Z) SUDO_COMMAND=sudo ;;
		z) _DEBUG=true ;;
		o) summary="yes" ;;
		?) usage; exit ;;
	esac
done


shift `expr $OPTIND - 1`

if [ "x$1" = "x" ]; then
	usage
	exit
fi

if [ "${summary}" = "yes" ]; then
	trap _quit QUIT 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

if [ "$LOCAL_OS" = "BSD" ] || [ "$LOCAL_OS" = "MacOSX" ]; then
	METHOD_PARAMETER="-P"
else
	METHOD_PARAMETER="-M"
fi

i=1
for args in "${@}"; do
	if [ $i -eq $# ]; then
		lastArg=$args
	elif [ $i -eq `expr $# - 1` ]; then
		beforeLastArg=$args
	fi
	i=`expr $i + 1`
done

case $lastArg in
	''|*[!0-9]*)
	# Last argument is not numeric, assuming it's an FQDN or IP
	host=$lastArg
	;;
	*)
	# Last argument is numeric, assuming it's a port number
	host=$beforeLastArg
	port=$lastArg
	;;
esac

# Try to compute interval value so whole execution takes less time than (repeats * interval)
# Remove an arbitrary second for pre execution script work
# Use an arbitrary factor of 0.7 to cope with shell execution time

if [ "$interval" = "auto" ]; then
	interval=`awk 'BEGIN { print ('$timeToWait'*.7)}'`
fi

pingCount=0
pingFailCount=0

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`
		if [ $result -eq 20 ] || [ $result -eq 21 ] || [ $result -eq 22 ]; then
			exit $result
		fi
	elif [ $pingCount -lt $x ] || [ $x -eq 0 ]; then
		sleep ${interval}
	fi
done
if [ "$summary" = "yes" ]; then
	_quit
fi
exit