Skip to content

Instantly share code, notes, and snippets.

@tomkul
Forked from ivan-loh/README.md
Created August 6, 2019 20:23
Show Gist options
  • Save tomkul/f164be6cfbf7c0c9cf921f8d3e2bf8e7 to your computer and use it in GitHub Desktop.
Save tomkul/f164be6cfbf7c0c9cf921f8d3e2bf8e7 to your computer and use it in GitHub Desktop.
netcat tcp proxy, for mapping ports on a remote machine to a local machine

Helper script to map ports to other servers.

Use Case

had a database that was only accesible in the VPC on aws, so i created an dev intance and did a ssh tunnel to that dev instance with netcat mapping the port to the database

Sample Usage

Forward all request from local 5432 to remote host google.com port 80

./nc-proxy.sh 5432 google.com 80

Reference:

#!/bin/sh -e
if [ $# != 3 ]
then
echo "usage: $0 <src-port> <dst-host> <dst-port>"
exit 0
fi
TMP=`mktemp -d`
PIPE=$TMP/pipe
trap 'rm -rf "$TMP"' EXIT
mkfifo -m 0600 "$PIPE"
nc -k -l -p "$1" <"$PIPE" | nc "$2" "$3" > "$PIPE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment