92 lines
2.1 KiB
Bash
92 lines
2.1 KiB
Bash
#!/usr/bin/env bash
|
|
# Copyright (c) 2025. LUXIM d.o.o., Slovenia - Matjaž Mozetič.
|
|
|
|
# backtunnel-access: Mount a folder shared over reverse SSH
|
|
# Usage: backtunnel-access /path/to/folder from remoteuser:remotehost [-p PORT] [-m MOUNTPOINT]
|
|
|
|
set -euo pipefail
|
|
|
|
PORT=2222
|
|
MOUNTPOINT="/mnt/remote-rssh"
|
|
|
|
usage() {
|
|
echo "Usage: $0 /path/to/folder from remoteuser:remotehost [-p PORT] [-m MOUNTPOINT]" >&2
|
|
exit 1
|
|
}
|
|
|
|
# --- parse positional args ---
|
|
[[ $# -lt 3 ]] && usage
|
|
|
|
FOLDER=$1
|
|
KEYWORD=$2
|
|
REMOTE=$3
|
|
shift 3 || true
|
|
|
|
[[ "$KEYWORD" != "from" ]] && usage
|
|
|
|
# Optional flags
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-p|--port)
|
|
[[ $# -lt 2 ]] && usage
|
|
PORT=$2
|
|
shift 2
|
|
;;
|
|
-m|--mount-point)
|
|
[[ $# -lt 2 ]] && usage
|
|
MOUNTPOINT=$2
|
|
shift 2
|
|
;;
|
|
-h|--help)
|
|
usage
|
|
;;
|
|
*)
|
|
echo "Unknown option: $1" >&2
|
|
usage
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# --- split remote user/host (supports user:host or user@host) ---
|
|
REMOTE_USER=""
|
|
REMOTE_HOST=""
|
|
|
|
if [[ "$REMOTE" == *:* ]]; then
|
|
REMOTE_USER=${REMOTE%%:*}
|
|
REMOTE_HOST=${REMOTE#*:}
|
|
elif [[ "$REMOTE" == *"@"* ]]; then
|
|
REMOTE_USER=${REMOTE%%@*}
|
|
REMOTE_HOST=${REMOTE#*@}
|
|
else
|
|
echo "Invalid remote format. Use remoteuser:remotehost or remoteuser@remotehost" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# --- deps check ---
|
|
command -v sshfs >/dev/null 2>&1 || { echo "sshfs not found. Install sshfs first."; exit 1; }
|
|
command -v mountpoint >/dev/null 2>&1 || { echo "mountpoint utility not found."; exit 1; }
|
|
|
|
# --- prepare mountpoint ---
|
|
if [[ ! -d "$MOUNTPOINT" ]]; then
|
|
mkdir -p -- "$MOUNTPOINT"
|
|
fi
|
|
|
|
# Avoid double-mount
|
|
if mountpoint -q -- "$MOUNTPOINT"; then
|
|
echo "Mount point '$MOUNTPOINT' is already mounted. Unmount it first (e.g., 'fusermount -u \"$MOUNTPOINT\"')." >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "🔗 Mounting '$FOLDER' from '$REMOTE_USER@$REMOTE_HOST' via reverse-tunnel localhost:$PORT → '$MOUNTPOINT' ..."
|
|
|
|
sshfs \
|
|
-p "$PORT" \
|
|
-o reconnect \
|
|
-o ServerAliveInterval=15 \
|
|
-o ServerAliveCountMax=3 \
|
|
-o ssh_command="ssh -o ConnectTimeout=10" \
|
|
-- "$REMOTE_USER@localhost:$FOLDER" "$MOUNTPOINT"
|
|
|
|
echo "✅ Mounted at: $MOUNTPOINT"
|
|
echo "To unmount: fusermount -u \"$MOUNTPOINT\""
|