We’re always working with code we didn’t write. You’ll spend far more time looking at code you didn’t write (or don’t remember writing) than you will spend writing new code.
Today I looked at an example Perl script that used 45 lines of code to pull the company associated with an OUI (Organizationally Unique Identifier) from a text file, given a MAC address.
I thought I could do slightly better.
find_mac_co.sh:
#!/bin/sh
OUI=$(echo "$1" | sed 's/[^A-Fa-f0-9]//g' | cut -c1-6)
awk -F "\t" -v IGNORECASE=1 -v OUI="$OUI" '$0 ~ OUI { print $3 }' ouidb.tsv
exit 0
Example run:
$ sh find_mac_co.sh 7c:ab:60:ff:ff:ff
Apple, Inc.
There’s probably a way to make the Perl version shorter too. I’m more familiar with bash and shell commands.
The biggest problem with this script is that it relies on an up-to-date list of OUIs. An even better way is to query an API:
find_mac_co_api.sh
#!/bin/sh
MACADDRESS="$1"
curl "https://api.maclookup.app/v2/macs/$MACADDRESS/company/name"
exit 0
Example run:
$ sh find_mac_co_api.sh 7c:ab:60:ff:ff:ff
Apple, Inc.