Last active
January 18, 2017 14:31
-
-
Save mjf/1477082 to your computer and use it in GitHub Desktop.
MAC - L2 address formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO
xsel(1x)
support for X server selections