多主机连接测试

多主机连接测试

我有一个简单的脚本,可以显示服务器之间的连接是否正常工作,问题是现在我有多个要检查的服务器,但我不想一一检查,最好将服务器列表添加到文件和脚本将检查文件中的每一行。任何人都可以帮助用循环扩展脚本

    SERVER=$1
PORT=$2
nc -z -v -G5 $SERVER $PORT &> /dev/null
result1=$?


if [  "$result1" != 0 ]; then
  echo  port $PORT is closed on $SERVER
else
  echo port $PORT is open on $SERVER
fi

答案1

你是这个意思吗?

服务器.txt

[hostname or IP 1] [port 1]
[hostname or IP 2] [port 2]
[hostname or IP 3] [port 3]
[hostname or IP 4] [port 4]
[hostname or IP 5] [port 5]
#!/usr/bin/bash
while IFS=" " read -r SERVER PORT; do
  nc -z -v -G5 $SERVER $PORT &> /dev/null
  result1=$?

  if [  "$result1" != 0 ]; then
    echo "port $PORT is closed on $SERVER"
  else
    echo "port $PORT is open on $SERVER"
  fi
done < server.txt

相关内容