Search code examples
bashexit-codenetcat

How to use the exit code of netcat command in a if condition?


I have this function in bash that uses netcat to check that a port is open on an address. As an example, I am testing with localhost 3000.

ERROR

I am having trouble getting the error code from running nc. I keep seeing the messge:

nc_output: integer expression expected

from the if-condition. (See CODE below)

CODE

#!/bin/bash
set -o nounset

scan() {
  local host=$1
  local port=$2
  local nc_output

  nc_output=$(nc ${host} ${port} -w 5)
  eval ${nc_output}
  if [ nc_output -ne 0 ]; then
     "PORT ${port} CLOSED on ${host}"
  else
    echo "PORT ${port} OPEN on ${host}"
  fi
}

scan 127.0.0.1 3000

Solution

  • I think the problem comes down to usage of how you are storing the nc command in a variable to eval it later. When you did

    nc_output=$(nc ${host} ${port} -w 5)
    #          ^^                      ^
    

    The marked syntax $(..) is for command substitution in bash, which runs the command inside and stores the output of the command in the variable used. So the above line runs the nc command and stores the output returned in the variable nc_output. Remember this only stores the standard output of the command and not the return code of the nc command which should been fetched from the variable $?

    I'm guessing your intention was to store the command in a variable and then eval it once and get the exit code, but you are doing it wrong by eval-ing the output returned from the nc command which is roundabout and unnecessary.

    The ideal way should have been just

    nc "${host}" "${port}" -w 5
    nc_exit_code=$?
    

    and use this variable in the condition

    if [ "$nc_exit_code" -ne 0 ]; then
    

    Note that the $ prefix before the variable name. You need to use the $var syntax to access the value of the identifier var in shell syntax. You weren't using the symbol in your original post. Not doing so will compare a literal string nc_output with 0 which does not make sense as the native types of two are different from one another.

    Also storing this exit code explicitly in a variable is redundant, the if conditionals work directly on the exit code of the command returned, so just

    if ! nc "${host}" "${port}" -w 5; then
        echo "PORT ${port} CLOSED on ${host}"
    else
        echo "PORT ${port} OPEN on ${host}"
    fi
    

    should be sufficient for your logic.