Friday, January 23, 2009

Script to convert MAC Address to format used by PXELinux

This script takes CCCC.CCCC.CCCC or CC:CC:CC:CC or cc:cc:cc:cc and spits out

01-cc-cc-cc-cc for a PXELinux MAC address file on the "first network segment."

#!/bin/sh
# Name: pxemac
# Usage: pxemac ${MAC Address}

OS=`uname -s`

type () {

  TYPE=`echo $1 | sed s/[A-Za-z0-9]*//g`
  if [ "$TYPE" = ".." ]
  then
    return 0
  else
    return 1
  fi

}

linux () {

  if type $1
  then
    echo $1 | sed -e "s/\./-/g" -e "s/^/01-/" -e "s/-[A-Za-z0-9]\{2\}/&-/g" | tr A-Z a-z
  else
    echo $1 | sed -e "s/\:/-/g" -e "s/^/01-/" | tr A-Z a-z
  fi

}

nonlinux () {

  if type $1
  then
    echo $1 | sed -e "s/\./-/g" -e "s/^/01-/" -e "s/-[A-Za-z0-9]\{2\}/&-/g" | tr [A-Z] [a-z]
  else
    echo $1 | sed -e "s/\:/-/g" -e "s/^/01-/" | tr '[A-Z]' '[a-z]'
  fi

}

if [ "$OS" = "Linux" ]
then
  linux $1
else
  nonlinux $1
fi