Skip to content

Instantly share code, notes, and snippets.

@stopsopa
Last active November 13, 2022 00:23
Show Gist options
  • Select an option

  • Save stopsopa/7724daa4537ae7774af59a7c48d0f3a3 to your computer and use it in GitHub Desktop.

Select an option

Save stopsopa/7724daa4537ae7774af59a7c48d0f3a3 to your computer and use it in GitHub Desktop.
Data forwarding through port and processing in the middle
# so now we gonna forward data to different port but also process it in between
# WARNING: if you working on mac replace default netcat with one from homebrew
brew install netcat
# for purpose of this tutorial lets assume that our machine ip address is: 192.168.180.74
# first we need script to work as a middleware
# pipe.sh (remember about newline char \n instead of \r on \r\n - this is bash)
BUFFER=""
LASTNL="false"
while read line; do
if [ "$line" == "" ]; then
# echo 'nl'
if [ "$LASTNL" == "true" ]; then
# echo 'last true'
LASTNL="false"
echo -e $BUFFER | /bin/bash run.sh
BUFFER="";
else
# echo 'last false'
LASTNL="true"
BUFFER="$BUFFER\n"
fi
else
# echo 'just data'
LASTNL="false"
BUFFER="$BUFFER\n$line"
fi
echo -e $line;
done < "${1:-/dev/stdin}"
# then we need script to process one chunk of data
# run.sh (remember about newline char \n instead of \r on \r\n - this is bash)
DATA=`cat`
echo -e "\n::>${DATA}<::\n";
# then setup pipe.sh script like
nc -lp 45678 | /bin/bash pipe.sh | nc -lp 45679
# in different console on the same machine run
nc 192.168.180.74 45679
# to listen on port 45678
# and on another (third) console run:
nc 192.168.180.74 45678
# and here start writing something on the keyboard
# from time to time press enter from one time to three time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment