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
  • Only content written to stdout, not stderr, is captured by command substitutions. Commented Oct 22, 2020 at 16:38

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.