Run a Command Conditionally with Netcat and Grep

run a command conditionally with netcat and grep

How about this?

    #!/bin/bash

netcat -lk -p 12345 | while read line
do
match=$(echo $line | grep -c 'Keep-Alive')
if [ $match -eq 1 ]; then
echo "Here run whatever you want..."
fi
done

Replace the "echo" command with the script you want to execute.

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

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.

it is possible to use an if without executing its condition

You cannot not-execute a command and then react on the return value of the executed command (because this is what you really want to do: check if you can run sftp successful, and if so do a "proper" run; but you'll never know whether it can run successfull without running it).

So the main question is, what it is what you actually want to test.

If you want to test whether you can do a full sftp connection (with all the handshaking and what not), you could try running sftp in batch-mode (which is handily non-interactive).

E.g. the following runs an sftp session, only to terminate it immediately with a bye command:

if echo bye | sftp -b - -oPort=23 user@server ; then 
echo "sftp succeeded"
fi

This will only succeed if the entire sftp session works (that is: you pass any key checks; you can authenticate, ...).
If the server asks you for a password, it will fail to authenticate (being non-interactive), and you won't enter the then body.

If you only want to check whether something is listening on port 23, you can use netcat for this:

if netcat -z server 23; then
echo "port:32 is open"
fi

This will succeed whenever it can successfully bind to port 23 on the server. It doesn't care whether there's an sftp daemon running, or (more likely) a telnet daemon.

You could also do some minimal test whether the remote server looks like an SSH/SFTP server: ssh servers usually greet you with a string indicating that they indeed speak ssh: something like "SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4".
With this information you can then run:

if echo QUIT | netcat server 23 | grep SSH; then
echo "found an ssh server"
fi

Redirect process stdin and stdout to netcat

I found that by using bash v. >= 4.0 I can use coproc:

#!/bin/bash

coproc myapp
nc -kl -p 4000 <&"${COPROC[0]}" >&"${COPROC[1]}"

EDIT

I eventually incorporated a telnet server in my cli library. You can find the result on GitHub: https://github.com/daniele77/cli

How to use Grep + cut + sort with Colour

Add option -c (interpret ANSI color and style sequences) to your watch command and --color=always to your GNU grep command.



Related Topics



Leave a reply



Submit