Created
December 13, 2013 10:04
-
-
Save dantesun/7942250 to your computer and use it in GitHub Desktop.
A bash script displaying only physical interfaces and mac address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Collecting all physical interfaces's name and mac addresse | |
declare -A NAME_TO_MAC | |
set -e | |
for f in /sys/class/net/*; do | |
if [ -L $f ]; then | |
name=`readlink $f` | |
if echo $name | grep -v 'devices/virtual' > /dev/null; then | |
eval $(ifconfig `basename $f` | head -n 1 | awk '{print "NAME_TO_MAC[\"",$1,"\"]=",$5}' | tr -d ' ') | |
fi | |
fi | |
done | |
function getRealMac() | |
{ | |
local ifname=$1 | |
local bond=$2 | |
local pattern="Slave Interface: $ifname" | |
awk -v pattern="$pattern" '$0 ~ pattern, $0 ~ /^$/' $bond | awk '/Permanent HW addr/{print $4}' | tr -d ' ' | |
} | |
#Trying to get the real mac when there's a bonding interface | |
for name in "${!NAME_TO_MAC[@]}"; do | |
for bond in /proc/net/bonding/*; do | |
if grep $name /sys/devices/virtual/net/`basename $bond`/bonding/slaves > /dev/null; then | |
MAC=`getRealMac $name $bond` | |
if ! [ -z $MAC ]; then | |
NAME_TO_MAC["$name"]="$MAC" | |
fi | |
fi | |
done | |
done | |
set +e | |
echo "System Physical Interfaces" | |
echo "==========================" | |
for k in ${!NAME_TO_MAC[@]}; do | |
echo $k ${NAME_TO_MAC[$k]} | |
done | |
echo "==========================" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment