diff options
| author | hc <hc@email.ch> | 2024-12-16 14:37:48 +0800 |
|---|---|---|
| committer | hc <hc@email.ch> | 2024-12-16 14:37:48 +0800 |
| commit | 47303626554d42356dfd1b61ad3f9de97929cc87 (patch) | |
| tree | eeb959ddac157e96096c54fb46af136055c1e198 | |
| parent | 71c7d66a5fcc1dc44665741dd264ba2c6a5524c5 (diff) | |
multiportforward
| -rw-r--r-- | port-forward/ssh-multiport-forward.sh | 44 |
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 |
