From d910d6b39f2b4776c2edd8576b7d3b2bee8f912e Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Mon, 13 Aug 2018 11:04:10 +0200
Subject: [PATCH] [Need test] Handle multiple IPs in dhcp lease

Also small refactor to avoid constants in code
---
 wol/scripts/sendmac.sh          |  5 ++++-
 wol/scripts/wakeonlanservice.sh | 20 ++++++++++++++------
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/wol/scripts/sendmac.sh b/wol/scripts/sendmac.sh
index 1f7ac2a..80ed7e9 100755
--- a/wol/scripts/sendmac.sh
+++ b/wol/scripts/sendmac.sh
@@ -22,5 +22,8 @@ KPID=`ps aux | grep \'$netcat\' | awk '{print $2}'`
 [ ! -z "$KPID" ] && kill $KPID
 
 subnet=$(ip a | awk '/inet.*containers/ {print $2}' | cut -d / -f 1 | cut -d . -f 1-3)
-echo "$1" | netcat $subnet.52 9999
+host=$subnet.52
+port=9999
+
+echo "$1" | netcat $host $port
 $netcat
diff --git a/wol/scripts/wakeonlanservice.sh b/wol/scripts/wakeonlanservice.sh
index 6fd4565..d742a97 100755
--- a/wol/scripts/wakeonlanservice.sh
+++ b/wol/scripts/wakeonlanservice.sh
@@ -24,18 +24,26 @@ getip(){
 IP=$(ip a | awk '/inet.*containers/ {print $2}' | cut -d / -f 1)
 broadcast=$(ip a | awk '/inet.*eth1/ {print $4}' | cut -d / -f 1)
 subnet=$(ip a | awk '/inet.*containers/ {print $2}' | cut -d / -f 1 | cut -d . -f 1-3)
+host=$subnet.51
+port=9999
+
 while true
 do
-    MAC=`netcat -l $IP 9999`
+    MAC=`netcat -l $IP $port`
     #echo "$MAC"
     if [ $(echo "$MAC" | egrep -c "^([0-9a-f]{2}:?){6}$") -eq 1 ]
     then
         wakeonlan -i $broadcast "$MAC"
         sleep 3
-        bootedip=`getip $MAC`
-        ping -c 3 $bootedip || bootedip=""
-        #echo "Sending ip"
-        echo "$bootedip" | netcat $subnet.51 9999
-        #echo "Done"
+        IPS=`getip $MAC`
+        for ip in $IPS
+        do
+            ping -c 3 $ip
+            if [ $? -eq 0 ]
+            then
+                echo "$ip" | netcat $host $port
+                break
+            fi
+        done
     fi
 done
-- 
GitLab