Skip to content

Instantly share code, notes, and snippets.

@m1tk4
Forked from ivan-loh/README.md
Last active August 9, 2024 13:17
Show Gist options
  • Save m1tk4/bb626dcf2bc1bfade170a38b3085cdbd to your computer and use it in GitHub Desktop.
Save m1tk4/bb626dcf2bc1bfade170a38b3085cdbd 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"
@elvisciotti
Copy link

that worked when launched on a k8s pod, and then when I forward the port (kubectl port-forward) I'm then able to connect to a elasticache instance accessible only from the k8s cluster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment