summaryrefslogtreecommitdiff
path: root/port-forward/ssh-multiport-forward.sh
blob: 40d4ef6dde9d575db3ad11244d4e1515252aa19b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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