Skip to content

Instantly share code, notes, and snippets.

@holly
Created October 17, 2015 02:32
Show Gist options
  • Save holly/6d52dd9addd3e58b2fd5 to your computer and use it in GitHub Desktop.
Save holly/6d52dd9addd3e58b2fd5 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