1. 首页
  2. 系统运维
  3. Linux

TCP端口检测、网络连接时延测试工具 tcping

在主流的linux系统中,通过yum或者apt也可安装tcping,不过通过源安装的tcping只能显示单次检测的结果,也没有具体的连接时延。现参考windows版tcping,用python写了一个linux环境下的类似工具。

代码

建立socket连接,测试端口连通性和网络连接时延。

def tcp(ip, port, timeout=2):
    sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sk.settimeout(timeout)
    try:
        t1 = time.time()
        sk.connect((ip, port))
        t2 = time.time()
        sk.close()
        return True, int(round((t2-t1)*1000))
    except Exception:
        sk.close()
        return False, timeout*1000

完整代码:https://github.com/zhangnq/scripts/blob/master/python/tcping.py

使用方法

下载

输入如下命令下载安装。

wget http://download.chekiang.info/tool/tcping -O /usr/bin/tcping
chmod +x /usr/bin/tcping

参数介绍

必选参数:
destination a DNS name, an IP address
可选参数:
-h, --help show this help message and exit
-p PORT a numeric TCP port, 1-65535. If not specified, defaults to 80.
-t ping continuously until stopped via control-c.
-n NUMBER send count pings and then stop, default 4.
-i INTERVAL wait seconds between pings, default 1.
-w WAIT wait seconds for a response, default 2.
-v, --version print version and exit.

使用举例

tcping blog.nbqykj.cn
tcping 114.114.114.114 -t -p 53
tcping blog.nbqykj.cn -n 10 -p 443 -i 5 -w 1

结果

默认和ping一样只发送四个检测包,-t参数表示持续检测,ctrl+c退出。

tcping for linux result

附:Windows tcping下载

网址:https://elifulkerson.com/projects/tcping.php

2 评论
内联反馈
查看所有评论
在线教育培训系统

不错 很赞

夏日博客
4 年 前

赞赞赞

联系我们

0574-55011290

QQ:248687950

邮件:admin@nbhao.org

工作时间:周一至周五,9:00-18:00,节假日休息

QR code