2

After years of 'just watching' i finally need your help! I cant find a solution for this puzzle.

I am trying to make a script that checks if a server is running on my ubuntu platform, if there is echo "smthing" else echo "other_thing".

I am blocked on the start of it because i need to check my localhost:port for a connection.

I am using "nc -zv localhost 80" for testing purposes.

my idea: If port 80 is on echo "yes" if dont echo "No."

I always get errors or miss results because the code seems to be not good.

#! /bin/sh

cmd=`nc -zv localhost 80`
answer="Connection to localhost 80 port [tcp/http] succeeded!"


if [ "$cmd" = "$answer" ]; then
    echo "Yes"
else
    echo "No"
fi

exit 0

Thank you!!

4
  • 2
    The script seems to be fine (pasting it in shellcheck.net just reveals the suggestion to use $() instead of backticks). I wonder what can be wrong. What is the output of nc -zv localhost 80? What do you get if you say echo "$cmd"? Commented Nov 25, 2015 at 16:30
  • I'd actually printf '%q\n' "$cmd", rather than echo "$cmd", to ensure that any hidden characters get displayed. Commented Nov 25, 2015 at 16:46
  • ...though looking at context, it's clear that kojiro and that other guy have the right of it. Commented Nov 25, 2015 at 16:48
  • The output of nc ends with a carriage-return/newline pair; the command substitution strips the newline but leaves the carriage return. Commented Nov 25, 2015 at 17:29

2 Answers 2

3

If you run your code you see the message printed to screen, while you would have expected it to be captured. This is because it's being written to stderr.

You can capture such output with cmd=`nc -zv localhost 80 2>&1` , but the better and more robust way is to use the fact that all Unix tools have an exit code that tells you whether the command succeeded or failed, and can therefore be used directly in if statements:

if nc -z localhost 80
then
  echo "Port open"
else
  echo "Port closed"
fi
Sign up to request clarification or add additional context in comments.

4 Comments

For the user's case, though, the text to be compared to $answer is written to standard output. Also, it's possible (though unlikely in this context) that nc could succeed but not output the expected text.
That worked like a charm. Thanks. However when i assign the command to cmd -> cmd=nc -zv localhost 80 <2&1 it shouts: ./test.sh: 5: ./test.sh: : Permission denied and always gets the negative answer.
@chepner Are you sure? My netcat.traditional writes these messages to stderr. @DiogoAlpha 2>&1 and <2&1 are very different things
It may vary by implementation, but the version I am using prints the success message to standard output and error messages to standard error.
0

Remove the trailing carriage return from the output of nc:

cmd=$(nc -zv localhost 80)
cmd=${cmd%$'\r'}

or include the carriage return in the expected output:

answer="Connection to localhost 80 port [tcp/http] succeeded!"$'\r'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.