summaryrefslogtreecommitdiff
path: root/setup/port-forward
diff options
context:
space:
mode:
authorhc <hc@email.ch>2025-06-25 19:40:43 +0800
committerhc <hc@email.ch>2025-06-25 19:40:43 +0800
commitccdde5f4424836fc8e9cc98c204510fed9612e70 (patch)
treedf1500f00b2f0b32b8729732454585c318b51110 /setup/port-forward
parentd6eb567da3e6d2e64ebf22adf1fc6d21c47090f8 (diff)
merged setup and contaienrs
Diffstat (limited to 'setup/port-forward')
-rwxr-xr-xsetup/port-forward/persistent-ssh.sh35
-rwxr-xr-xsetup/port-forward/rm_all_ssh_connections.sh4
-rwxr-xr-xsetup/port-forward/ssh-multiport-forward.sh44
-rwxr-xr-xsetup/port-forward/ssh-port-forward.sh19
4 files changed, 102 insertions, 0 deletions
diff --git a/setup/port-forward/persistent-ssh.sh b/setup/port-forward/persistent-ssh.sh
new file mode 100755
index 0000000..36d14dd
--- /dev/null
+++ b/setup/port-forward/persistent-ssh.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+echo "selinux command is untested. copy paste systemd file if you want"
+echo "reboot afterwards(selinux unset, need reboot to take effect, or just do sudo setenforce 0 for temp unset)"
+echo "make sure /root/m exists"
+echo "remember to change the remote port or host if needed"
+
+# disable selinux
+sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
+
+sudo cat << 'EOF' > /etc/systemd/system/ssh-tunnel.service
+[Unit]
+Description=Persistent SSH Tunnel
+After=network.target
+StartLimitIntervalSec=0
+
+[Service]
+Type=simple
+ExecStart=/usr/bin/ssh \
+ -i /root/m \
+ -o "ExitOnForwardFailure=yes" \
+ -o "StrictHostKeyChecking=no" \
+ -N -R 24:localhost:22 root@p.0nom.ch
+Restart=always
+RestartSec=10
+RemainAfterExit=no
+KillMode=process
+
+[Install]
+WantedBy=multi-user.target
+EOF
+
+sudo chmod 600 /root/m
+sudo systemctl daemon-reload
+sudo systemctl enable --now ssh-tunnel
diff --git a/setup/port-forward/rm_all_ssh_connections.sh b/setup/port-forward/rm_all_ssh_connections.sh
new file mode 100755
index 0000000..f19300d
--- /dev/null
+++ b/setup/port-forward/rm_all_ssh_connections.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+ss -tnp | grep ':22'
+ps -ef | grep sshd | grep -v grep | awk '{if($3!=1) print $2}' | xargs kill
+#sudo systemctl restart sshd
diff --git a/setup/port-forward/ssh-multiport-forward.sh b/setup/port-forward/ssh-multiport-forward.sh
new file mode 100755
index 0000000..40d4ef6
--- /dev/null
+++ b/setup/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
diff --git a/setup/port-forward/ssh-port-forward.sh b/setup/port-forward/ssh-port-forward.sh
new file mode 100755
index 0000000..4483ef4
--- /dev/null
+++ b/setup/port-forward/ssh-port-forward.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+
+# opens 5 ports
+
+#file will be run from where the user executes the script
+j=$1
+HOST="root@p.0nom.ch"
+
+# get the process holding the specified port and kill it
+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."
+
+ssh $HOST $(for i in $(seq ${j}000 ${j}005); do echo "-R $i:localhost:$i"; done; echo "-R ${j}022:localhost:22")
+
+echo "port forward is available on port {j}00x"
+
+#ideally port forward from 2 onwards, there seems to be a problem for 1000(or just change all to j001)
+#jupyter lab --port=2001 --ip=0.0.0.0