Skip to content

Instantly share code, notes, and snippets.

@liubin
Forked from holly/nc-tcp-forward.sh
Created February 9, 2023 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liubin/109535f7d215002f5395643e325855ab to your computer and use it in GitHub Desktop.
Save liubin/109535f7d215002f5395643e325855ab to your computer and use it in GitHub Desktop.
easy tcp port forwarding by netcat
#!/usr/bin/env bash
set -e
if [ $# != 3 ]; then
echo 'Usage: nc-tcp-forward.sh $FRONTPORT $BACKHOST $BACKPORT' >&2
exit 1
fi
FRONTPORT=$1
BACKHOST=$2
BACKPORT=$3
FIFO=/tmp/backpipe
trap 'echo "trapped."; pkill nc; rm -f $FIFO; exit 1' 1 2 3 15
mkfifo $FIFO
while true; do
nc -l $FRONTPORT <$FIFO | nc $BACKHOST $BACKPORT >$FIFO
done
rm -f $FIFO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment