summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--port-forward/ssh-multiport-forward.sh44
1 files changed, 44 insertions, 0 deletions
diff --git a/port-forward/ssh-multiport-forward.sh b/port-forward/ssh-multiport-forward.sh
new file mode 100644
index 0000000..40d4ef6
--- /dev/null
+++ b/port-forward/ssh-multiport-forward.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+# Check if at least one argument is provided
+if [ $# -lt 1 ]; then
+ echo "Usage: $0 <base_number> [additional_ports...]"
+ echo "Example: $0 5 80 443"
+ exit 1
+fi
+
+# First argument is the base number for port range
+j=$1
+shift # Remove first argument from the list, leaving only additional ports
+
+HOST="root@p.0nom.ch"
+
+# Clean management port
+echo "cleaning management port..."
+ssh $HOST "ss -tunlp | grep :${j}022 | awk '{print $NF}' | sed 's/.*pid=\([^,]*\).*/\1/' | head -n1 | xargs kill -9"
+echo "cleaning attempted."
+
+# Build the SSH command with all port forwards
+SSH_CMD="ssh $HOST"
+
+# Add range-based port forwards (j000-j005)
+for i in $(seq ${j}000 ${j}005); do
+ SSH_CMD+=" -R $i:localhost:$i"
+done
+
+# Add management port forward
+SSH_CMD+=" -R ${j}022:localhost:22"
+
+# Add additional individual port forwards from remaining arguments
+for port in "$@"; do
+ SSH_CMD+=" -R $port:localhost:$port"
+done
+
+# Execute the SSH command
+eval $SSH_CMD
+
+echo "Port forwards are available on:"
+echo "- Ports ${j}000-${j}005"
+echo "- Management port ${j}022"
+if [ $# -gt 0 ]; then
+ echo "- Additional ports: $@"
+fi