From 4ef0e2d92acf7d5ba0b2bbebf210cfaea8b23a39 Mon Sep 17 00:00:00 2001 From: Will Woods Date: Thu, 13 Sep 2012 10:52:33 -0400 Subject: [PATCH] net-lib: add configured_ifaces() configured_ifaces is a function that returns the names of each interface that the user wanted configured. Currently, this is accomplished by reading the list from /tmp/net.ifaces. But if we want to allow the user to specify an interface by its MAC address or IP or something, we need a function that will read the cache and convert the MACs etc. to names. (Obviously this conversion only works once udev starts, so it will warn you if you try it too early.) --- modules.d/40network/net-lib.sh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/modules.d/40network/net-lib.sh b/modules.d/40network/net-lib.sh index 7b8d1d7..7fff83a 100644 --- a/modules.d/40network/net-lib.sh +++ b/modules.d/40network/net-lib.sh @@ -14,6 +14,11 @@ iface_for_remote_addr() { echo $5 } +iface_for_ip() { + set -- $(ip -o addr show to $1) + echo $2 +} + iface_for_mac() { local interface="" mac="$(echo $1 | sed 'y/ABCDEF/abcdef/')" for interface in /sys/class/net/*; do @@ -47,6 +52,32 @@ find_iface_with_link() { return 1 } +# get the iface name for the given identifier - either a MAC, IP, or iface name +iface_name() { + case $1 in + ??:??:??:??:??:??|??-??-??-??-??-??) iface_for_mac $1 ;; + *:*:*|*.*.*.*) iface_for_ip $1 ;; + *) echo $1 ;; + esac +} + +# list the configured interfaces +configured_ifaces() { + local IFACES="" iface_id="" rv=1 + [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces + if { pidof udevd || pidof systemd-udevd; } > /dev/null; then + for iface_id in $IFACES; do + echo $(iface_name $iface_id) + rv=0 + done + else + warn "configured_ifaces called before udev is running" + echo $IFACES + [ -n "$IFACES" ] && rv=0 + fi + return $rv +} + all_ifaces_up() { local iface="" IFACES="" [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces -- 2.7.4