From 5d96098a82939995dd852380c759771c7c2dd928 Mon Sep 17 00:00:00 2001 From: Michael Boelen Date: Thu, 8 Jul 2021 14:54:49 +0200 Subject: [PATCH] Switched order for interface detection on Linux --- include/functions | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/include/functions b/include/functions index cdb0c786..f4fc6218 100644 --- a/include/functions +++ b/include/functions @@ -990,9 +990,23 @@ ;; "Linux") + # Try fetching information from /sys in case 'ip' is not available or does not give expected results + if IsEmpty "${FIND}" -a -d /sys/class/net ]; then + NET_INTERFACES=$(${FINDBINARY} /sys/class/net ! -type d -exec realpath {} \; 2> /dev/null | sort | awk -F'/' '!/virtual/ && /devices/ {for (x=1;x<=NF;x++) if ($x~"net") print $(x+1)}') + for INTERFACE in ${NET_INTERFACES}; do + if grep -q -s 'up' "/sys/class/net/${INTERFACE}/operstate"; then + LogText "Interface '${INTERFACE}' is up, fetching MAC address" + FIND=$(head -1 "/sys/class/net/${INTERFACE}/address" | tr '[:upper:]' '[:lower:]') + if HasData "${FIND}"; then + HOSTID_GEN="linux-sys-interface-up" + break + fi + fi + done + fi - # First try ip, as it is available to most modern Linux distributions - if [ -n "${IPBINARY}" ]; then + # Next is to try ip, as it is available to most modern Linux distributions + if IsEmpty "${FIND}" && [ -n "${IPBINARY}" ]; then LogText "Info: trying output from 'ip' to generate HostID" # Determine if we have the common available eth0 interface. If so, give that priority. # Note: apply sorting in case there would be multiple MAC addresses linked to increase predictable end result @@ -1022,21 +1036,7 @@ fi fi - # Try fetching information from /sys in case 'ip' is not available or does not give expected results - if IsEmpty "${FIND}" && [ ${PRIVILEGED} -eq 1 -a -d /sys/class/net ]; then - NET_INTERFACES=$(${FINDBINARY} /sys/class/net ! -type d -exec realpath {} \; 2> /dev/null | sort | awk -F'/' '!/virtual/ && /devices/ {for (x=1;x<=NF;x++) if ($x~"net") print $(x+1)}') - for INTERFACE in ${NET_INTERFACES}; do - if grep -s 'up' "/sys/class/net/${INTERFACE}/operstate"; then - LogText "Interface '${INTERFACE}' is up, fetching MAC address" - FIND=$(head -1 "/sys/class/net/${INTERFACE}/address" | tr '[:upper:]' '[:lower:]') - if HasData "${FIND}"; then - HOSTID_GEN="linux-sys-interface-up" - break - fi - fi - done - fi - + # Finally try ifconfig if IsEmpty "${FIND}" && [ -n "${IFCONFIGBINARY}" ]; then LogText "Info: no information found from 'ip' or in /sys, trying output from 'ifconfig'" # Determine if we have the eth0 interface (not all Linux distributions have this, e.g. Arch) @@ -1073,9 +1073,9 @@ fi fi - # Check if we found a HostID + # Check if we found a MAC address to generate the HostID if HasData "${FIND}"; then - LogText "Info: using hardware address ${FIND} to create HostID" + LogText "Info: using hardware address '${FIND}' to create HostID" HOSTID=$(echo ${FIND} | ${SHA1SUMBINARY} | awk '{ print $1 }') LogText "Result: Found HostID: ${HOSTID}" else