From e68f669860821c532b7b8336734c45e961dd1b69 Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Thu, 2 Aug 2018 09:53:01 +0200
Subject: [PATCH] Async boot with output on the flow

---
 wol/scripts/sendmac.sh          |  5 ++++-
 wol/scripts/wakeonlanservice.sh |  7 ++++++-
 wol/www/index.php               | 18 +++++++++++++++++-
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/wol/scripts/sendmac.sh b/wol/scripts/sendmac.sh
index 9a8d206..168daed 100755
--- a/wol/scripts/sendmac.sh
+++ b/wol/scripts/sendmac.sh
@@ -18,5 +18,8 @@
 
 IP=$(ip a | awk '/inet.*containers/ {print $2}' | cut -d / -f 1)
 subnet=$(ip a | awk '/inet.*containers/ {print $2}' | cut -d / -f 1 | cut -d . -f 1-3)
+echo "Demande de démarrage pour l'adresse mac : $1"
 echo "$1" | netcat $subnet.52 9999
-netcat -l $IP 9999
+echo "Démarrage en cours, attente de l'IP"
+retip=`netcat -l $IP 9999`
+echo "Vous pouvez désormais lancer XRDP et vous connecter à l'adresse : $retip"
diff --git a/wol/scripts/wakeonlanservice.sh b/wol/scripts/wakeonlanservice.sh
index 4a05590..f08690b 100755
--- a/wol/scripts/wakeonlanservice.sh
+++ b/wol/scripts/wakeonlanservice.sh
@@ -30,8 +30,13 @@ netcat -k -l $IP 9999 | \
         if [ $(echo "$MAC" | egrep -c "^([0-9a-f]{2}:?){6}$") -eq 1 ]
         then
             wakeonlan -i $broadcast "$MAC"
+            $bootedip=""
             sleep 30
-            bootedip=`getip $MAC`
+            while [ -z "$bootedip" ]
+            do
+                bootedip=`getip $MAC`
+                sleep 5
+            done
             echo "$bootedip" | netcat $subnet.51 9999
         fi
     done
diff --git a/wol/www/index.php b/wol/www/index.php
index b4e2830..e14c111 100644
--- a/wol/www/index.php
+++ b/wol/www/index.php
@@ -32,7 +32,23 @@
     if (strcmp($cmd, "")){
         //echo "<p>execution de ' $cmd $args '</p>";
         //echo "<p>".shell_exec("$cmd $args")."</p>";
-        echo "<p> Vous pouvez ouvrir XRDP et vous connecter à l'IP : <code>".shell_exec("$cmd $args")."</code></p>";
+        echo "<p> Demarrage de la machine ".$args."</p>";
+        echo "<pre><code>";
+        $descriptorspec = array(
+           0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
+           1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
+           2 => array("pipe", "w")    // stderr is a pipe that the child will write to
+        );
+        flush();
+        $process = proc_open("$cmd $args", $descriptorspec, $pipes, realpath('./'), array());
+        echo "<pre>";
+        if (is_resource($process)) {
+            while ($s = fgets($pipes[1])) {
+                print $s;
+                flush();
+            }
+        }
+        echo "</code></pre>";
     }
 ?>
 <div id="box">
-- 
GitLab