Skip to content

Instantly share code, notes, and snippets.

@agail
Created April 28, 2020 12:34
Show Gist options
  • Save agail/c1de8a580794845840d3e71ebe8cccf0 to your computer and use it in GitHub Desktop.
Save agail/c1de8a580794845840d3e71ebe8cccf0 to your computer and use it in GitHub Desktop.
simple mac address lookup
#!/bin/bash
#
# description: simple mac address lookup
# version: 0.3
#
# mac address src: https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf
#
_uri='https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf'
_macfile=/tmp/mac.txt
input=$1
t48h=172800
f_download () {
if [ ! -s "${_macfile}" ]; then
echo "Downloading mac address listing .."
curl -Lko "${_macfile}" "${_uri}"
elif [ $(date +%s) -ge $(( $(stat ${_macfile} -c %Z) + ${t48h} )) ]; then
echo "Updating mac address listing .."
curl -sLko "${_macfile}" "${_uri}"
fi
}
f_find_mac () {
if [ -r $1 ]; then
grep -i $(echo $1 | awk -F "[.:-]" 'BEGIN{OFS=""} $1=$1' | sed 's/../&:/g;s/:$//g' | cut -b -8) "${_macfile}"
else
grep -i $(echo $1 | sed -r 's/[:.-]//g;s/../&:/g' | cut -b -8) "${_macfile}"
fi
}
f_parse_file () {
echo -e "\nVLAN MAC ADDRESS TYPE PORT VENDOR"
grep DYNAMIC $1 | sort -k 4,4 -V | while read -r line; do
echo "$line $(f_find_mac $(echo $line | cut -d ' ' -f2) | awk '$1=""; $0')"
done
echo ""
}
f_usage () {
cat <<-EOF
$(basename $0) takes one argument, single mac address or input file*.
mac address: - can be the following formats: 000000000000, 00:00:00:00:00:00, 00-00-00-00-00-00, 0000.0000.0000
input file*: - format of cisco show mac address-table (shown below):
* if input doesn't exist, we assume it's a mac address
Example file: (simply copy/paste into text file, the script looks for DYNAMIC type only)
Mac Address Table
-------------------------------------------
Vlan Mac Address Type Ports
---- ----------- -------- -----
All 0100.0ccc.cccc STATIC CPU
All 0100.0ccc.cccd STATIC CPU
[snip]
All 0180.c200.0010 STATIC CPU
All ffff.ffff.ffff STATIC CPU
11 0008.2f86.0919 DYNAMIC Gi0/1
11 0012.7c03.77bf DYNAMIC Fa0/3
11 0021.7b20.1c79 DYNAMIC Gi0/1
11 0021.7b20.2337 DYNAMIC Fa0/1
11 00a0.0308.5ed4 DYNAMIC Gi0/1
11 4c02.890c.4597 DYNAMIC Gi0/1
11 5852.8a8c.2bdb DYNAMIC Fa0/4
Total Mac Addresses for this criterion: 27
EOF
}
#f_find_mac $1
#exit 0
if [ $# -lt 1 ]; then
f_usage
exit
elif [ ! -s $input -a ! -r $input ]; then
f_download
f_find_mac $1
exit
fi
f_download
f_parse_file $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment