Skip to content

Instantly share code, notes, and snippets.

@ivan-loh
Last active September 30, 2022 19:13
Show Gist options
  • Save ivan-loh/3ce7bb1d464495a04d8b to your computer and use it in GitHub Desktop.
Save ivan-loh/3ce7bb1d464495a04d8b 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"
Copy link

ghost commented Sep 30, 2022

of note: this script needs netcat-openbsd instead of netcat-traditional on debian-based systems

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