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 @@
1#!/bin/bash
2# Check if at least one argument is provided
3if [ $# -lt 1 ]; then
4 echo "Usage: $0 <base_number> [additional_ports...]"
5 echo "Example: $0 5 80 443"
6 exit 1
7fi
8
9# First argument is the base number for port range
10j=$1
11shift # Remove first argument from the list, leaving only additional ports
12
13HOST="root@p.0nom.ch"
14
15# Clean management port
16echo "cleaning management port..."
17ssh $HOST "ss -tunlp | grep :${j}022 | awk '{print $NF}' | sed 's/.*pid=\([^,]*\).*/\1/' | head -n1 | xargs kill -9"
18echo "cleaning attempted."
19
20# Build the SSH command with all port forwards
21SSH_CMD="ssh $HOST"
22
23# Add range-based port forwards (j000-j005)
24for i in $(seq ${j}000 ${j}005); do
25 SSH_CMD+=" -R $i:localhost:$i"
26done
27
28# Add management port forward
29SSH_CMD+=" -R ${j}022:localhost:22"
30
31# Add additional individual port forwards from remaining arguments
32for port in "$@"; do
33 SSH_CMD+=" -R $port:localhost:$port"
34done
35
36# Execute the SSH command
37eval $SSH_CMD
38
39echo "Port forwards are available on:"
40echo "- Ports ${j}000-${j}005"
41echo "- Management port ${j}022"
42if [ $# -gt 0 ]; then
43 echo "- Additional ports: $@"
44fi