Skip to content

Instantly share code, notes, and snippets.

@Wilsonilo
Created April 24, 2021 01:20
Show Gist options
  • Save Wilsonilo/f0f66707b719074d38cdd8a3cd027679 to your computer and use it in GitHub Desktop.
Save Wilsonilo/f0f66707b719074d38cdd8a3cd027679 to your computer and use it in GitHub Desktop.
Small bash script to get IP and MAC Address on MAC.
#!/usr/bin/env bash
#Original source: https://apple.stackexchange.com/questions/20547/how-do-i-find-my-ip-address-from-the-command-line
#I just added the MAC address.
dumpIpForInterface()
{
IT=$(ifconfig "$1")
if [[ "$IT" != *"status: active"* ]]; then
return
fi
if [[ "$IT" != *" broadcast "* ]]; then
return
fi
echo "IP: "; echo "$IT" | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
# echo -e "\n"
echo "MAC: "; echo "$IT" | grep "ether " | awk '{print $2}'
}
main()
{
# snagged from here: https://superuser.com/a/627581/38941
DEFAULT_ROUTE=$(route -n get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}')
if [ -n "$DEFAULT_ROUTE" ]; then
dumpIpForInterface "$DEFAULT_ROUTE"
else
for i in $(ifconfig -s | awk '{print $1}' | awk '{if(NR>1)print}')
do
if [[ $i != *"vboxnet"* ]]; then
dumpIpForInterface "$i"
fi
done
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment