Answer the question
In order to leave comments, you need to log in
Bash check for an open port. How to check the output of a command?
There is a task using a bash script to check if the port is open and, depending on the result, perform one or another action.
I can not figure out how to check the result of the NC command in the if condition, something like this
#!/bin/bash
RESULT=$(nc -z <host> <port>)
if [$RESULT]; then
echo 'Открыт'
else
echo 'Закрыт'
fi
Answer the question
In order to leave comments, you need to log in
Variable $? contains the result of the last executed command (0=success)
#!/bin/bash
nc -z <host> <port>
if [ $? -ne 0 ]; then
echo 'Открыт'
else
echo 'Закрыт'
fi
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question