Skip to content

Instantly share code, notes, and snippets.

@redperadot
Last active May 16, 2023 21:39
Show Gist options
  • Save redperadot/8828742 to your computer and use it in GitHub Desktop.
Save redperadot/8828742 to your computer and use it in GitHub Desktop.
Get the vendor for a mac address from the terminal.
#!/usr/bin/env bash
#
# mac_vendor.sh - Get the vendor for a mac address from the terminal.
# Made with ❤︎ by [email protected]
# Set Functions
usage()
{
echo "MAC Vendor Help"
echo "mac_vendor -a 'MAC Address' | Get the vendor of the specified address."
echo "mac_vendor -s 'MAC Address' | Silent, only output result."
exit 0
}
get_database()
{
# Test Connection
ping -c 1 standards.ieee.org &> /dev/null
[[ $? > 0 ]] && echo "[Error 68:$(($LINENO - 1))] Was unable to reach the database." && exit 68
# Download Database
database="$(curl -sf http://standards.ieee.org/develop/regauth/oui/oui.txt)"
[[ -z $database ]] && echo "[Error 72:$(($LINENO - 1))] Could not read the database." && exit 72
}
format_vendor_address()
{
[[ -n $1 ]] && vendor_address="$1"
vendor_address="$(echo "$vendor_address" | tr -d ':' | tr '[:lower:]' '[:upper:]')"
if [[ ${#vendor_address} == "6" ]]; then
return
else
vendor_address="$(echo "$vendor_address" | cut -c1-6)"
fi
}
# Set Arguments
while getopts ":s:a:h" opt; do
case $opt in
s ) silent=true; vendor_address="$OPTARG" ;;
a ) vendor_address="$OPTARG" ;;
h ) usage ;;
\?) echo "[Error] Invalid option: -$OPTARG" ; exit 1 ;;
esac
done
# Get Vendor Address
[[ -z $vendor_address && -n $1 ]] && vendor_address="$1"
[[ -z $vendor_address ]] && read -p "Vender address to test for [##:##:##:##:##:##]: " vendor_address
format_vendor_address
[[ ${#vendor_address} < 6 || ! $vendor_address =~ ^[0-9A-F]{6}$ ]] \
&& echo "[Error 65:$LINENO] The address '$vendor_address' was invalid." && exit 65
# Search Database
[[ $silent != true ]] && echo "Searching Database..."
get_database
vendor="$(echo "$database" | grep -E "$vendor_address.*(base 16)" | awk '{for(i=4;i<NF;i++) printf"%s",$i OFS;if(NF)printf"%s",$NF;printf ORS}')"
[[ -z $vendor ]] && vendor="Unknown"
# Print Result
[[ $silent != true ]] && echo -n "The MAC address prefix "
echo -n "$vendor_address "
[[ $silent != true ]] && echo -n "belongs to "
echo "$vendor"
exit 0
@redperadot
Copy link
Author

To quickly run, use:

bash <(curl -fs https://gist.github.com/redperadot/8828742/raw/mac_vender.sh)

@martin-simko
Copy link

Hi there,
Ilike your work.
The code has change in line 18. standards-oui.ieee.org
And in line 22. http://standards-oui.ieee.org/oui/oui.txt

Thank you for your code. Great job man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment