0
if [[ $(timeout 3 nc -zv 172.31.3.2 88) = *open* ]];then
        echo "Port Open"
else
        echo "Port Closed"
fi

I am trying to check if a port is open then it printing out port open if it is open.

I am using netcat to do that.

It should return something like: [172.31.3.2] 88 (kerberos) open.

I am trying to check for that open string in the output, then if it says that it will return port open

For some reason it just keeps returning this:

[172.31.3.2] 88 (kerberos) open
Port Closed

1 Answers1

1

If you're seeing the output in the terminal, it's being written to stderr, since stdout is captured by the command substitution. So you need to redirect stderr to stdout so you can capture it as well.

if [[ $(timeout 3 nc -zv 172.31.3.2 88 2>&1) = *open* ]];then
        echo "Port Open"
else
        echo "Port Closed"
fi
Barmar
  • 741,623
  • 53
  • 500
  • 612