原文地址:/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
完整代码:/zhangnq/scripts/blob/master/python/tcping.py
使用方法
下载
输入如下命令下载安装。
wget http://download.chekiang.info/tool/tcping -O /usr/bin/tcpingchmod +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 tcping 114.114.114.114 -t -p 53tcping -n 10 -p 443 -i 5 -w 1
结果
默认和ping一样只发送四个检测包,-t参数表示持续检测,ctrl+c退出。
附:Windows tcping下载
网址:/projects/tcping.php
如果觉得《TCP端口检测 网络连接时延测试工具 tcping》对你有帮助,请点赞、收藏,并留下你的观点哦!