27
2024
11
17:24:45

centos tcping tcpping 两个命令区别

tcping 命令,centos本身就支持,可以通过yum install 安装,但是只能测试通跟不通,并没有显示延时,看到有脚本可以结合 tcptraceroute 实现延时

https://cloud.tencent.com/developer/article/2101992?from=15425  但是脚本 报错,估计是脚本里面的文字信息给错版了。


以下是修正后的脚本



#!/bin/sh


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=""

target=""


usage () {

    name=$(basename "$0")

    echo "tcpping $ver Richard van den Berg"

    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}" | grep -Eiq "(bad destination|got roo)"; then

        echo "${ttr}"

        exit

    fi

}


_testsite() {

    myseq="${1}"

    shift

    nows=""

    nowd=""

    [ "${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 [ -n "${rtt}" ] && [ -z "${not}" ]; 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 [ -n "${rtt}" ] && [ -z "${not}" ]; then

            echo -n " $rtt"

        else

            echo -n " -"

        fi

        [ "$x" = "1" ] && echo

    else

        echo "${ttr}" | sed -e "s/^.*.$/seq $myseq: no response (time out)/" -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) topts="$topts -$opt" ;;

        i|l|p|s) topts="$topts -$opt $OPTARG" ;;

        f|m) ttl="$OPTARG" ;;

        ?) usage; exit ;;

    esac

done

shift $((OPTIND - 1))


# Check if IP address is provided

if [ -z "$1" ]; then

    echo "Error: IP address is required."

    usage

    exit 1

fi


# Store the target IP address and port

target="$1"

port="${2:-80}"  # Default port is 80 if not provided


# max 为默认超时时间的毫秒数

max=$(echo "${w} * 1000" | bc)


# Output some debug info

echo "Target: $target"

echo "Port: $port"

echo "Max wait time: ${max}ms"


# Call the _testsite function with the target and port

_testsite "$seq" "$target" "$port"









tcpping命令,需要单独下载,http://www.vdberg.org/~richard/tcpping   并且需要搭载 yum install  tcptraceroute,否则显示就是空白



1、将rpm文件上传到tcping文件夹中

2、cd tcping目录,然后执行rpm -Uvh --force --nodeps *.rpm,部署依赖

4、给tcping文件授权  chmod +x tcping

5、执行命令后台运行tcping:./tcpping -d -r 3 ip port> test.txt &






备注:rpm包下载不了的,可以自己来这个网站下载:


https://centos.pkgs.org/7/centos-x86_64/traceroute-2.0.22-2.el7.x86_64.rpm.html




tcping文件下载地址:


https://soft.mengclaw.com/Bash/TCP-PING 


下载后,直接将TCP-PING重命名为tcping,然后授权可用






推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://hqyman.cn/post/8618.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: