Tcping脚本使用说明

羌俊恩 社区创始人
运维领域优质创作者
博客专家认证
2023-10-21 20:41:02
#!/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) 2002-2005 Richard van den Berg <richard@vdberg.org> under the GPL
#               http://www.gnu.org/copyleft/gpl.html
#
# 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>


ver="v1.7"
format="%Y%m%d%H%M%S"
d="no"
c="no"
C="no"
ttl=255
seq=0
q=1
r=1
w=3
topts=""

usage () {
	name=`basename $0`
	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} $* 2>&1`
	if echo "${ttr}" | egrep -i "(bad destination|got roo)" >/dev/null 2>&1; 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} $* 2>/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" = "0" ]; then
			echo -n "$1 :"
		fi
		if [ "x${rtt}" != "x" -a "x${not}" = "x" ]; then
			echo -n " $rtt"
		else
			echo -n " -"
		fi
		if [ "$x" = "1" ]; 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 - 1`

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 [ 1 ] ; do
		_testsite ${seq} ${topt} $* &
		pid=$!
		if [ "${C}" = "yes" ]; then
			wait $pid
		fi
		seq=`expr $seq + 1`
		sleep ${r}
	done
else
	while [ "$x" -gt 0 ] ; do
		_testsite ${seq} ${topt} $* &
		pid=$!
		if [ "${C}" = "yes" ]; then
			wait $pid
		fi
		seq=`expr $seq + 1`
		x=`expr $x - 1`
		if [ "$x" -gt 0 ]; then
			sleep ${r}
		fi
	done
fi

exit

#用法

将上述文件保存为tcpping

cp tcpping /usr/bin  &&chmod 755 tcpping

 

#脚本用法:tcpping [-d] [-c] [-r sec] [-x count] ipaddress [端口]

  • -d: 在每个结果之前打印时间戳。

  • -c: 使用分列输出以便于解析。

  • -r:连续探测之间的间隔(以秒为单位)(默认为 1 秒)。

  • -x: 重复 n 次(默认无限制)。

  • [port]: 目标端口(默认为 80)。

注意:该脚本需要 root 权限才能运行,tcpping因为它需要调用特权tcptraceroute命令。对于任何开放 80 端口(默认使用)的目标 Web 服务器,都可以用它测RTT;

#示例:

1)nc -vn <ip-address> <port-number>   #检查远程 TCP 端口是否打开

2)tcping -t 8.8.8.8 443  #-t: 连续 TCPing ,直到使用 Ctrl+C 键停止

3)tcping -n 5 8.8.8.8 443  #-n 5: TCPing 5次后停止

4)tcping -w 5 8.8.8.8 443   #-w 0.5: 设置超时时间为 0.5秒(1秒=1000毫秒),单位 秒

5)tcping -d 8.8.8.8 443   #-d:在每行返回信息中加入时间信息

6)tcping -s 8.8.8.8 443  #-s: 当 TCPing 测试成功后(在超时时间以内返回 TCPing 延迟数据)自动停止 TCPing

7)tcping -4 8.8.8.8 443  #-4: 优先 IPv4(如果一个域名有 IPv4 和 IPv6 解析,那么走 IPv4)

8)tcping -6 8.8.8.8 443  #-6:优先 IPv6(如果一个域名有 IPv4 和 IPv6 解析,那么走 IPv6)

9)tcping --file iptest.txt   #--file: TCPing 将逐行循环遍历文件内的 服务器IP/域名 信息(一行一个,支持端口,例如:ip port)

 

...全文
178 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

99,469

社区成员

发帖
与我相关
我的任务
社区描述
欢迎各位小伙伴踊跃加入,将日常运维中遇到的问题贴出来,解决方案分享出来,或自己的博客关于运维的文章分享到这里,来一起营造一个运维的知识库,以提升运维工作更加高效快捷,减少重复造车轮的无限循环。
devops经验分享开源 企业社区 北京·西城区
社区管理员
  • 羌俊恩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

本社区:旨在推进运维云化知识库建设,为更多运维提供参考及经验交流;加入社区的同学,如果运维工作中遇到问题或最后得到解决,有劳凑空发帖,分享共建FAQ社区。

得到解决方案的同学,请积极为发帖的同学点赞、评论、能力范围内打赏激励。另外本社区采用游戏修仙角色作为鼓励,对不同贡献者授予对应“勋章”。

最后祝愿社区繁荣昌盛,此致敬礼!!!

试试用AI创作助手写篇文章吧