Skip to content

Instantly share code, notes, and snippets.

@kenoir
Last active October 7, 2023 07:56
Proxy local port to remote host via SSH
#!/bin/bash
#Usage: ./stun.sh ubuntu@hostname.com [Destination port] [Local port (optional)]
DESTINATION=$1
DEST_PORT=$2
PROXY_PORT=$((RANDOM%10000+10000))
#Set local port to same as remote if unset
if [ -z $3 ]; then
LOCAL_PORT=$2
else
LOCAL_PORT=$3
fi
echo "Establishing SSH tunnel ..."
ssh -fN -D localhost:$PROXY_PORT $DESTINATION
if [[ $? -ne 0 ]]; then
echo "Failed!"
exit 1
else
echo "SSH Tunnel Established."
fi
echo "Proxying local port $LOCAL_PORT to $DEST_PORT at $DESTINATION (through local $PROXY_PORT)."
socat TCP-LISTEN:$LOCAL_PORT,fork SOCKS4:127.0.0.1:localhost:$DEST_PORT,socksport=$PROXY_PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment