Skip to content

Instantly share code, notes, and snippets.

@gzcf
Created September 26, 2021 09:36
Show Gist options
  • Save gzcf/2693c62e95860d6d035d28040bf26398 to your computer and use it in GitHub Desktop.
Save gzcf/2693c62e95860d6d035d28040bf26398 to your computer and use it in GitHub Desktop.
Find interface in all namespaces by mac address
#!/bin/bash
# usage: ./find_if_by_mac.sh d4:3a:65:08:13:7e
function get_if_name() {
name=`echo "$1" | awk '{print $2}' | sed 's/:$//'`
echo $name
}
mac=$1
match=`ip --oneline -f link a | grep $mac`
if [[ $? = "0" ]]; then
echo "Found $mac in default namespace"
ip link show `get_if_name "$match"`
exit 0
fi
ns_arr=`ip netns | awk '{print $1}'`
for ns in ${ns_arr[@]}; do
match=`ip netns exec $ns ip --oneline -f link a | grep $mac`
if [[ $? = "0" ]]; then
echo "Found $mac in namespace $ns"
ip netns exec $ns ip link show `get_if_name "$match"`
exit 0
fi
done
echo "Not found"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment