Skip to content

Instantly share code, notes, and snippets.

@mskf3000
Forked from kenoir/stun.sh
Created October 7, 2023 07:56
Show Gist options
  • Save mskf3000/2e009845482043aea98e59133870d6d6 to your computer and use it in GitHub Desktop.
Save mskf3000/2e009845482043aea98e59133870d6d6 to your computer and use it in GitHub Desktop.
Proxy local port to remote host via SSH
#!/bin/bash
#Usage: ./stun.sh [email protected] [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