Netcat双向通信

时间:2017-07-04 08:31:18

标签: ubuntu netcat

我试图实现的目标是让客户端将以下命令发送到服务器:

echo "Hello" | nc IP Port

收到“Hello”关键字后,正在侦听的服务器使用:

nc -nlp port
然后

应该将响应“Holla”发回客户端。我尝试过以下方法,但在我的实例中都没有:

所以我目前面临的问题是,在从客户端收到“Hello”后,服务器端无法向客户端发送“Holla”响应。有没有办法用netcat做到这一点?如果是这样,我错过了什么?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您需要一些bash魔术和一些FIFO才能实现,但这是完全可行的:

#!/bin/bash

# Communication diagram:
# 
# +----------+          +------------+
# |          |  stdout  |            |   
# |    nc    | =======> | _feed_fifo |
# |          |          |            |
# +---stdin--+          +------------+
#      /\                ||
#      ||                || $fifo_ref 
#      ||                \/
#  +--stdout----------stdin-+
#  |                        |
#  |   __handle_fifo_data   |
#  |                        |
#  +------------------------+

fifo_ref="$RANDOM.fifo"
running_lock="$RANDOM.run"

# Strip leading and trailing white space (new line inclusive).
trim() {
    [[ "$1" =~ [^[:space:]](.*[^[:space:]])? ]]
    printf "%s" "$BASH_REMATCH"
}

# Handles data coming from the nc, with it's stdout connected
# directly to the client.
__handle_fifo_data() {
    # Keep trying to read the fifo while it exists
    while test -e "$1"
    do
        while read -r line
        do
            # This is where the magic happens. Anything sent to stdout will
            # be transmitted to the client connected to the nc
            echo "handling $line" >&2 # this is how we debug what's coming

            # Lines will include a \r in the end if coming from telnet.
            # Stripping the line from unwanted characters in the beginning/end
            # (line breaks, spaces, etc).
            message="$(trim "$line")"
            
            # Responding only if the message is "Hello"
            [ "$message" == "Hello" ] && {
                echo "Hola!"
            }
        done < $1 # feeding the loop with our fifo
    done
}

# Will read input and feed the fifo so we can handle data
__feed_fifo() {
    # This is important, otherwise we'll trash the terminal with lots of 
    # bash pids trying to read a dead stdin
    while test -e "$1"
    do
        # Testing again in case of concurrency
        test -e "$1" && cat - > "$1"
    done
}

# Cleaning up if something goes south or the program simply ends
trap "rm -rf ${fifo_ref} ${running_lock}" EXIT INT TERM HUP

# Let's get this party started
touch "$running_lock"

# Won't stop until you say so
while test -e "$running_lock"
do
    # Let's clear out the fifo so we don't get dirt from previous connections
    test -e "${fifo_ref}" && rm -rf "${fifo_ref}"
    mkfifo "${fifo_ref}"

    # Netcat port 9999
    # We'll feed the nc using our __handle_fifo_data and read from it to a
    # __feed_fifo which will try to keep stdin open all the time, feeding
    # __handle_fifo_data
    nc -l 9999 \
        0< <(__handle_fifo_data "${fifo_ref}") \
        1> >(__feed_fifo "${fifo_ref}")
done

您可以通过将其保存到server.sh之类并在一个会话中运行来对其进行测试:

$ bash -x ./server.sh
handling Hello

在其他会话中:

$ telnet localhost 9999
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello
Hola!
^]
telnet> Connection closed.

要停止它,只需^C

相关问题