Identify Ethernet interfaces in check_ethernet.hook.

BUG=chromium-os:35478
TEST=Tested check_ethernet.hook on machines with the following settings:
1. with an Ethernet interface
2. with two Ethernet interfaces
3. with an Ethernet interface and an E362 modem.

Change-Id: I57de4e50e9ceb73141e240bfc2219432b9d8d11a
Reviewed-on: https://gerrit.chromium.org/gerrit/36439
Reviewed-by: Chris Sosa <sosa@chromium.org>
Commit-Ready: Ben Chan <benchan@chromium.org>
Tested-by: Ben Chan <benchan@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/37485
Tested-by: Chris Sosa <sosa@chromium.org>
diff --git a/recover_duts/hooks/check_ethernet.hook b/recover_duts/hooks/check_ethernet.hook
index 19fc00a..7e700f6 100755
--- a/recover_duts/hooks/check_ethernet.hook
+++ b/recover_duts/hooks/check_ethernet.hook
@@ -12,32 +12,66 @@
   exit 0
 fi
 
-# Ping itself doesn't work on test images in a VM.
-PING="curl --interface eth0 -o /dev/null www.google.com"
+NON_ETHERNET_DRIVERS="cdc_ether"
 
-if ${PING};  then
-  exit 0
-fi
+# Returns 0 if $1 is a non-Ethernet driver, or 1 otherwise.
+is_non_ethernet_driver() {
+  local driver="$1"
+  local non_ethernet_driver
 
-ifconfig eth0 down
-ifconfig eth0 up
-sleep 5
+  for non_ethernet_driver in ${NON_ETHERNET_DRIVERS}; do
+    if [ "${driver}" = "${non_ethernet_driver}" ]; then
+      return 0
+    fi
+  done
+  return 1
+}
 
-if ${PING}; then
-  echo "Reconfigured using ifconfig down/up."
-  exit 1
-fi
+# Shows the list of Ethernet interfaces found on the system.
+find_ethernet_interfaces() {
+  local device_path
+  local driver_path
+  local driver
 
-initctl stop flimflam || echo "Flimflam was not running."
-initctl start flimflam
+  for device_path in /sys/class/net/eth*; do
+    driver_path="${device_path}/device/driver"
+    if [ -e "${driver_path}" ]; then
+      driver=$(basename $(readlink -f "${driver_path}"))
+      if ! is_non_ethernet_driver "${driver}"; then
+        basename "${device_path}"
+      fi
+    fi
+  done
+}
 
-sleep 5
+for eth in $(find_ethernet_interfaces); do
+  # Ping itself doesn't work on test images in a VM.
+  PING="curl --interface ${eth} -o /dev/null www.google.com"
 
-if ${PING}; then
-  exit 1
-fi
+  if ${PING}; then
+    exit 0
+  fi
+
+  ifconfig ${eth} down
+  ifconfig ${eth} up
+  sleep 5
+
+  if ${PING}; then
+    echo "Reconfigured using ifconfig down/up."
+    exit 1
+  fi
+
+  initctl stop flimflam || echo "Flimflam was not running."
+  initctl start flimflam
+
+  sleep 5
+
+  if ${PING}; then
+    exit 1
+  fi
+done
 
 # Last chance - reboot if we can't get any connectivity.
 echo "All efforts to recover ethernet have been exhausted. Rebooting."
 (sleep 5 && reboot) &
-exit 1
\ No newline at end of file
+exit 1