MAC Address Formatting Conversions

AllLinux

My work is now using non-Cisco devices as their gateway, which means I've been spending a decent amount of time converting Standard MAC Addresses to Cisco whenever I need to rundown an issue based on its IP address. 
Because of this I've create the two functions to convert the MACs back and forth for these formats. Below is the script I use and the process for adding it to your .bashrc file.

How it works when you're done
tc = "to Cisco" from Standard
fc = "from Cisco" to Standard


Script
# ------------------------------------------------------------------------------
_tocisco()
# ------------------------------------------------------------------------------
# Changes standard format MACs to Cisco's amazing '.' format
{
# Use a standard format MAC like 'F4:15:63:C9:30:CD' 
# and this will convert to 'f415.63c9.30cd'
      if [ "$1" != "" ]
      then
       _arg=`echo $1`
        echo $_arg | sed -e 's/\(..\):\(..\)/\1\2/g' | tr ':[:upper:]' '.[:lower:]'
      fi
}
# ------------------------------------------------------------------------------
_fromcisco()
# ------------------------------------------------------------------------------
# Changes Cisco format MACs to standard format
{
# Use a cisco format MAC like 'f415.63c9.30cd'
# this will convert to 'F4:15:63:C9:30:CD'
      if [ "$1" != "" ]
      then
       _arg=`echo $1`
        echo $_arg | tr -d .  | sed 's/.\{2\}/&:/g' | sed 's/.$//'  | tr '[:lower:]' '[:upper:]'
      fi
}

alias fc="_fromcisco"
alias tc="_tocisco"    

This will work on pretty much any linux system.

Use a text editor (vi, or nano/pico) to open your .bashrc file.
If you don't have one you may need to create it.


  • Here I use nano >> nano .bashrc



  • Then I scroll to the bottom and paste the script from above in at the bottom. 
  • Save and exit nano.




  • Exit out of your session, and re-enter



  • Now you can translate to back and forth for these formats quickly.


Categories: All, Linux