测试双向网络连接的简便方法

Easyway to test two way network connectivity

我有一种情况需要检查从客户端 A 到 40 台主机的网络连接,反之亦然。因为登录到每个客户端都需要时间。我想知道是否有一种简单的方法可以实现这一点,请展示一些光。

例如:

目的地 1:

来源到目的地:OK

来源目的地:KO

来源:

10.1.2.3

目的地:

10.2.2.2
10.3.3.3
10.4.4.4
10.5.5.5

端口:8080

任何人都可以帮助我应该使用哪个模块吗?请。

这应该可以,但我没有测试过。如果对你有帮助,请告诉我。

编辑:我正在编辑,因为我意识到您也需要 "viceversa" 方式。所以在这里,假设您在主服务器和您要测试的 40 台主机之间有 ssh 密钥。正如评论中所述,这将在 Bash.

中起作用
 #!/bin/bash

port=8080
viceversaIp="10.1.2.3"
while read line
do

    result=$( echo > /dev/tcp/$line/$port )
    if [ -z "$result" ]
    then
        echo "Server : $line ; Port : $port ;  The port is closed!!"
    else
        echo "Server : $line ; Port : $port ;  The port is open!!"
    fi
    result=""

    viceversa_result=$( ssh -n $line " echo > /dev/tcp/$viceversaIp/$port " | tail -1 )

    if [ -z "$viceversa_result" ]
    then
        echo "Server $line can reach $viceversaIp at port $port "
    else
        echo "Server $line can NOT reach $viceversaIp at port $port "
    fi
    viceversa_result=""

done <( cat ips.txt )

此致!

这是使用 netcat nc:

的单线
cat hosts.txt| xargs -n 1 sh -c 'nc -G1 -w1 -z  8080' argv0

其中文件 hosts.txt 每行包含一个 IP/domain。

如果您有每个 IP 的自定义端口,您可以遵循以下格式:

# IP PORT
10.10.0.1 8080
10.10.0.2 80

然后做:

cat hosts.txt| xargs -n 2 sh -c 'nc -G1 -w1 -z  ' argv0

netcat 正在通过 TPC 进行连接,该连接是双向的(三次握手),因此也许可以满足您在 "two way"

中进行测试的要求

选项 -G-w 用于超时:

-G conntimeout TCP connection timeout in seconds
-w If a connection and stdin are idle for more than timeout sec-
   onds, then the connection is silently closed.