SSH Tunnel Wrapper
tunnel.sh
#!/bin/bash
REMOTE_USER="root"
REMOTE_HOST="srv"
# Check if the correct number of arguments are passed
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: $0 <local_port> [remote_port]"
exit 1
fi
# Assign the first argument to the local port
LOCAL_PORT=$1
# If a remote port is provided, use it; otherwise, generate a random port in the range 10000-16000
if [ -n "$2" ]; then
REMOTE_PORT=$2
else
REMOTE_PORT=$(shuf -i 10000-16000 -n 1)
fi
# Output the chosen remote port for reference
echo "SSH tunnel established at http://${REMOTE_HOST}:${REMOTE_PORT}"
# Create the SSH tunnel
ssh ${REMOTE_USER}@${REMOTE_HOST} -N -R ${REMOTE_PORT}:127.0.0.1:${LOCAL_PORT}