Exercise: MAC Addresses

In order to complete this exercise, you will need to download this script first, uncompress it and make the file executable.

You can run the script a couple of times to see what type of output it generates. You will see twenty MAC addresses, one per line. Some will be in a "correct" format (six pairs of hexadecimal separated by colons); other will have more or fewer pairs, or use a different type of separator, or use upper case characters. You goal is to fix all incorrect addresses and get a consistant set of values.

This exercise is for beginners, all commands are provided with additional explainations. You can basically copy/paste each command to move to the next step.

Exercises

Generate a new list of random MAC addresses

Run the script and redirect all the results to a file.


./generate-random-mac-addresses.sh > mac-addresses.txt

Delete all lines that have too many or not enough characters

A MAC address should be 17 characters longs: six pairs (6 x 2 = 12) separated by five delimiters (12 + 5 = 17). So any entry that is not exactly 17 characters longs should be deleted.

When not using extended regular expressions (using the "-E" option) the curly braces will need to be escaped using a backslash character.


sed -i '/^.\{17\}$/!d' mac-addresses.txt

Match only valid MAC addresses

You can use a regular expression that will check for six pairs of hexadecimal values, separated by either a dash or a colon.


grep -E "^[[:xdigit:]]{2}[:-][[:xdigit:]]{2}[:-][[:xdigit:]]{2}[:-][[:xdigit:]]{2}[:-][[:xdigit:]]{2}[:-][[:xdigit:]]{2}$" mac-addresses.txt > valid-mac-addresses.txt

Alternatively, you can use a shorter regular expression, that would be less strict (not checking for pairs, only for the valid characters):


grep -E "^[0-9A-Fa-f:-]{17}$" mac-addresses.txt > valid-mac-addresses.txt

Convert all dashes to colons

This is a classic use case of "sed": substituting all occurences of a specific character (a dash) with another one (a colon).


sed -i 's/-/:/g' valid-mac-addresses.txt

Another option would be to use the "tr" command (but this requires to redirect the results to a new file).


cat valid-mac-addresses.txt | tr '-' ':' > valid-mac-addresses-2.txt

Convert all uppercase characters to lowercase

Use "sed" to replace all letters to a lower case version (the "\L&" code).


sed -iE 's/.*/\L&/g' valid-mac-addresses.txt

Again, you can use the "tr" command instead:


cat valid-mac-addresses-2.txt | tr 'A-Z' 'a-z' > valid-mac-addresses-3.txt

Additional Ideas

Once you've completed the previous section, you can start at looking at some other commands to use against the resulting file, such as generating statistics by vendors using the "sort" and "uniq" commands. You will need to look at the script code to find the necessary information to identify each vendor.