ping & tracert over TCP

时间:2023-03-09 16:04:07
ping & tracert over TCP

偶然发现还有这样的工具: 通过TCP协议实现ping和tracert. 之前一直苦恼无法通过ping的方式测试被q网站, 现在有了这两个工具后就方便了.

[Windows]

tcping: http://www.elifulkerson.com/projects/tcping.php

tracetcp: https://github.com/SimulatedSimian/tracetcp

[Linux]

tcpping: http://xmodulo.com/how-to-install-tcpping-on-linux.html

1. apt-get install tcptraceroute

2. vi tcpping.sh

#!/bin/sh
#
# tcpping: test response times using TCP SYN packets
# URL: http://www.vdberg.org/~richard/tcpping.html
#
# uses tcptraceroute from http://michael.toren.net/code/tcptraceroute/
#
# (c) - Richard van den Berg <richard@vdberg.org> under the GPL
# http://www.gnu.org/copyleft/gpl.html
#
# // v1. initial version
# // v1. added -c and -r options
# now accepting all other tcptraceroute options
# // v1. removed double quotes around backquotes
# // v1. added -x option, courtesy of Alvin Austin <alvin@crlogic.com>
# // v1. added -C option, courtesy of Norman Rasmussen <norman@rasmussen.org>
# // v1. catch bad destination addresses
# // v1. catch non-root tcptraceroute
# // v1. make -C work when reverse lookup fails, courtesy of Fabrice Le Dorze <Fabrice.LeDorze@apx.fr> ver="v1.7"
format="%Y%m%d%H%M%S"
d="no"
c="no"
C="no"
ttl=
seq=
q=
r=
w=
topts="" usage () {
name=`basename $`
echo "tcpping $ver Richard van den Berg <richard@vdberg.org>"
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)"
echo " -r repeat every n seconds (defaults to 1)"
echo " -x repeat n times (defaults to unlimited)"
echo
echo "See also: man tcptraceroute"
echo
} _checksite() {
ttr=`tcptraceroute -f ${ttl} -m ${ttl} -q ${q} -w ${w} $* >&`
if echo "${ttr}" | egrep -i "(bad destination|got roo)" >/dev/null >&; then
echo "${ttr}"
exit
fi
} _testsite() {
myseq="${1}"
shift
[ "${c}" = "yes" ] && nows=`date +${format}`
[ "${d}" = "yes" ] && nowd=`date`
ttr=`tcptraceroute -f ${ttl} -m ${ttl} -q ${q} -w ${w} $* >/dev/null`
host=`echo "${ttr}" | awk '{print $2 " " $3}'`
rtt=`echo "${ttr}" | sed 's/.*] //' | awk '{print $1}'`
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 $max $host"
fi
elif [ "${C}" = "yes" ]; then
if [ "$myseq" = "" ]; then
echo -n "$1 :"
fi
if [ "x${rtt}" != "x" -a "x${not}" = "x" ]; then
echo -n " $rtt"
else
echo -n " -"
fi
if [ "$x" = "" ]; then
echo
fi
else
echo "${ttr}" | sed -e "s/^.*\*.*$/seq $myseq: no response (timeout)/" -e "s/^$ttl /seq $myseq: tcp response from/"
fi
# echo "${ttr}"
} while getopts dhq:w:cr:nNFSAEi:f:l:m:p:s:x:C opt ; do
case "$opt" in
d|c|C) eval $opt="yes" ;;
q|w|r|x) eval $opt="$OPTARG" ;;
n|N|F|S|A|E) topt="$topt -$opt" ;;
i|l|p|s) topt="$topt -$opt $OPTARG" ;;
f|m) ttl="$OPTARG" ;;
?) usage; exit ;;
esac
done shift `expr $OPTIND - ` if [ "x$1" = "x" ]; then
usage
exit
fi max=`echo "${w} * 1000" | bc` if [ `date +%s` != "%s" ]; then
format="%s"
fi _checksite ${topt} $* if [ "$x" = "" ]; then
while [ ] ; do
_testsite ${seq} ${topt} $* &
pid=$!
if [ "${C}" = "yes" ]; then
wait $pid
fi
seq=`expr $seq + `
sleep ${r}
done
else
while [ "$x" -gt ] ; do
_testsite ${seq} ${topt} $* &
pid=$!
if [ "${C}" = "yes" ]; then
wait $pid
fi
seq=`expr $seq + `
x=`expr $x - `
if [ "$x" -gt ]; then
sleep ${r}
fi
done
fi exit

3. chmod 755 tcpping