From 162e5ef9eb859a8709399fcc9be868d8b7b07ca3 Mon Sep 17 00:00:00 2001 From: Matthias Schoepfer Date: Tue, 6 Jul 2021 16:04:46 +0200 Subject: [PATCH] ethercatctl: allow to use the network device name instead of mac address In the /etc/ethercat.conf file, the MASTERX_DEVICE string is given as a mac address or a wildcard. When we want to deploy the (same) software (image) to a bunch of different hardware, and these computers do have more than one interface that matches the driver, we need a way to define the device by network interface name. This allows to also define the network device by name (e.g. eth0, eno1, ...) Signed-off-by: Matthias Schoepfer --- script/ethercatctl.in | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/script/ethercatctl.in b/script/ethercatctl.in index 62dae0bd..1664a31f 100755 --- a/script/ethercatctl.in +++ b/script/ethercatctl.in @@ -37,6 +37,7 @@ LSMOD=/sbin/lsmod MODPROBE=/sbin/modprobe RMMOD=/sbin/rmmod MODINFO=/sbin/modinfo +IP=/bin/ip ETHERCAT=@bindir@/ethercat @@ -55,13 +56,19 @@ fi #------------------------------------------------------------------------------ parse_mac_address() { + local DEVICENAMETOMAC if [ -z "${1}" ]; then MAC="" - elif echo "${1}" | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then + elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then MAC=${1} else - echo Invalid MAC address \""${1}"\" in ${ETHERCAT_CONFIG} - exit 1 + DEVICENAMETOMAC=$("${IP}" address show dev "${1}" | awk '/link\/ether/ { print $2; }') + if echo "${DEVICENAMETOMAC}" | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then + MAC=${DEVICENAMETOMAC} + else + echo Invalid MAC address \""${1}"\" in ${ETHERCAT_CONFIG} + exit 1 + fi fi }