You'll need some bash
magic and some FIFO to achieve that, but it's totally doable:
#!/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
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
You can test it by saving it to something like server.sh
and running in one session:
$ bash -x ./server.sh
handling Hello
And in other session:
$ telnet localhost 9999
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello
Hola!
^]
telnet> Connection closed.
To stop it just ^C
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…