Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active January 18, 2017 14:31
Show Gist options
  • Save mjf/1477082 to your computer and use it in GitHub Desktop.
Save mjf/1477082 to your computer and use it in GitHub Desktop.
MAC - L2 address formatter
#! /bin/sh
# MAC - L2 address formatter
# Copyright (C) 2011, 2014 Matous J. Fialka, <http://mjf.cz/>
# Released under the terms of The MIT License
FORMAT=Collon
LETTER_CASE=Keep
while [ $# -gt 0 ]
do
case $1 in
-c) FORMAT=Collon ;;
-d) FORMAT=Dash ;;
-dd) FORMAT=Dash2 ;;
-o) FORMAT=OUI LETTER_CASE=Upper;;
-p) FORMAT=Period ;;
-x) FORMAT=Hexa ;;
-l) LETTER_CASE=Lower ;;
-u) LETTER_CASE=Upper ;;
-k) LETTER_CASE=Keep ;;
-h) cat <<- .
Usage: mac [-h] [-cdpx] [-uln] [address ...]
-h This help
Output format:
-c Colons (default)
-d Dashes
-dd Dashes (2-octet form)
-o IEEE oui.txt (-dd but first 3 octets only)
-p Periods
-x Heximal
Letter case:
-l Lower
-u Upper
-k Keep (default)
Notes:
Letter case is keeped intact by default.
Options -c, -d and -p are to be combined with options -u
and -l. The format set by the -l, -u or -k is global.
Example:
$ mac -l -d AABB.ccdd.EEFF -p AABB-CCDD-EEFF -c AABBCCDDEEFF
aabb-ccdd-eeff
aabb.ccdd.eeff
aa:bb:cc:dd:ee:ff
.
exit 0
;;
*)
echo $1 |
sed 's/^0[xX]//; s/[:.-]//g' |
if ! grep '^[0-9a-fA-F]\{12\}$'
then
echo Warning: invalid MAC address \`$1\'. 1>&2
fi |
case $FORMAT in
Collon) sed 's/\([0-9a-fA-F]\{2\}\)/\1:/g; s/:$//' ;;
Dash) sed 's/\([0-9a-fA-F]\{4\}\)/\1-/g; s/-$//' ;;
Dash2) sed 's/\([0-9a-fA-F]\{2\}\)/\1-/g; s/-$//' ;;
Period) sed 's/\([0-9a-fA-F]\{4\}\)/\1./g; s/\.$//' ;;
Hexa) sed 's/\([0-9a-fA-F]\{12\}\)/0x\1/g' ;;
OUI) sed 's/[0-9a-fA-F]\{6\}$//; s/\([0-9a-fA-F]\{2\}\)/\1-/g; s/-$//' ;;
esac |
case $LETTER_CASE in
Upper) tr a-f A-F ;;
Lower) tr A-F a-f ;;
Keep) cat ;;
esac
;;
esac
shift
done
unset FORMAT
unset LETTER_CASE
exit 0
@mjf
Copy link
Author

mjf commented Jan 18, 2017

TODO

  • refactor source code
  • add xsel(1x) support for X server selections

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