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

本文介绍了一款在Linux环境下,使用Python编写的类似Windows tcping的工具,用于测试端口连通性和网络连接时延。提供了完整的代码和使用方法,包括参数说明和使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:https://zhangnq.com/3158.html

在主流的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 zhangnq.com
tcping 114.114.114.114 -t -p 53
tcping zhangnq.com -n 10 -p 443 -i 5 -w 1

结果

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

tcping for linux result

附:Windows tcping下载

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值