From f0cb3b4bc95d355ca9547a5baeb3da65b97160d6 Mon Sep 17 00:00:00 2001
From: Elian Loraux <elian.loraux@tetras-libre.fr>
Date: Wed, 24 Jul 2024 16:50:07 +0200
Subject: [PATCH] =?UTF-8?q?Script=20de=20bascule=20r=C3=A9seau=20automatiq?=
 =?UTF-8?q?ue?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 net-switch.sh      | 54 ++++++++++++++++++++++++++++++++++++++++++++++
 reverseSSHproxy.sh | 10 +++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 net-switch.sh
 create mode 100644 reverseSSHproxy.sh

diff --git a/net-switch.sh b/net-switch.sh
new file mode 100644
index 0000000..2fad441
--- /dev/null
+++ b/net-switch.sh
@@ -0,0 +1,54 @@
+#!/usr/bin/env bash
+
+IPPubliqueTetrixChezOVH="109.190.180.230"
+IPPubliqueAquilaChezOVH="5.135.140.89"
+
+APIKEY="PUT YOUT PERSONAL TOKEN" # Jeton sur passbotl
+
+stateFile="/opt/net-state"
+
+currentIP=`curl api.ipify.org`
+
+reverseSSHProxyPath="/opt/reverseSSHproxy.sh"
+
+# check if file exist
+if [[ -f "$stateFile" ]]; then
+
+    $state=`cat $stateFile`
+
+    # Check if current ip is different of normal ip
+    if [[ $IPPubliqueTetrixChezOVH == $currentIP ]]; then
+        if [[ $state != "normal" ]]; then
+            # Switch on normal mode
+            # ssh dockerweb@aquila.tetras-libre.fr 'cd reverse-ssh-proxy && docker-compose down'
+            pid=`ps aux | grep test.sh | grep -v grep | awk '{print $2}'`
+            kill $pid
+            change_dns $APIKEY $IPPubliqueTetrixChezOVH
+            echo "normal" > "$stateFile"
+        fi
+    else
+        if [[ $state = "normal" ]]; then
+            # Switch on back up mode
+            # ssh dockerweb@aquila.tetras-libre.fr 'cd reverse-ssh-proxy && docker-compose up'
+            bash $reverseSSHProxyPath &
+            change_dns $APIKEY $IPPubliqueAquilaChezOVH
+            echo "backup" > "$stateFile"
+        fi
+    fi
+else
+    echo "normal" > "$stateFile"
+fi
+
+
+
+change_dns() {
+  APIKEY=$1
+  CURRENTIP=$2
+
+
+  curl -s -X PUT --header "Authorization: Bearer ${APIKEY}" \
+                --header "Content-Type: application/json" \
+                -d "{\"rrset_values\": [\"${CURRENTIP}\"], \"rrset_ttl\": "300"}" \
+                https://api.gandi.net/v5/livedns/domains/tetras-libre.fr/records/tetrixx/A
+
+}
diff --git a/reverseSSHproxy.sh b/reverseSSHproxy.sh
new file mode 100644
index 0000000..64d977d
--- /dev/null
+++ b/reverseSSHproxy.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+HTTPS_PORT=8080
+SSHPORT_INT=2223
+SSHPORT=2222
+PROXY_HOST=aquila.tetras-libre.fr
+while true; do
+    /usr/bin/ssh -i /root/.ssh/id_rsa -N -R ${HTTPS_PORT}:localhost:443 -R ${SSHPORT_INT}:localhost:22 -o "ExitOnForwardFailure yes" -o "StrictHostKeyChecking no" -o "ServerAliveInterval 60" sshuser@${PROXY_HOST} -p ${SSHPORT}
+    echo "Ssh failed $?"
+    sleep 5
+done
-- 
GitLab