From 8ade577c9e221de0ee6e2d13f972f9b53591255d Mon Sep 17 00:00:00 2001
From: ERIK ALEXANDRE PUCCI <eap08@c3sl.ufpr.br>
Date: Tue, 23 Nov 2010 10:59:48 -0200
Subject: [PATCH] common.sh: Created getNetworkInterface() to obtain the
 default route NIC

The function checks all network interface controllers (NICs) set as the
default route, using ping(1) to test them with two root servers (in case one
is off). The first NIC connected to the Internet is chosen.

Signed-off-by: Erik Alexandre Pucci <eap08@c3sl.ufpr.br>
Signed-off-by: Danilo K. S. Yorinori <danilok@c3sl.ufpr.br>
---
 common.sh | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/common.sh b/common.sh
index 0081a49..cb030e0 100644
--- a/common.sh
+++ b/common.sh
@@ -70,3 +70,21 @@ function tryWget(){
                                      --proxy-password="${ppasswd}" \
                                      $* || return 1
 }
+
+#------------------------------------------------------------------------------
+# function: getNetworkInterface
+# Get the network interface which the client must bind to (in case there is
+# more than one default route) and the collect will use to obtain the MAC
+# address.
+function getNetworkInterface()
+{
+    interface=""
+
+    for interface in $(route | grep default | awk '{print $NF}'); do
+        # Send ICMP ECHO_REQUEST to root server I or J
+        if ping -c 1 -I ${interface} 192.36.148.17 > /dev/null 2>&1 ||
+           ping -c 1 -I ${interface} 192.58.128.30 > /dev/null 2>&1 ; then
+            break
+        fi
+    done
+}
-- 
GitLab