Tuesday, November 27, 2012

Increment mac address by count using tcl script


Below copied the proc used to increment physical address (mac) by given count.

proc incrmentMac { mac byCount} {
   while { $byCount } {
      set hexValues [ split $mac ":" ]
      set position 5
      set carryFwd 1
      while { $carryFwd && [expr $position + 1]} {
         set carryFwd 0
         set hexVal [lindex $hexValues $position]
         scan $hexVal "%x" decimal_value
         incr decimal_value
         set hexVal [format "%x" $decimal_value]
         if { $hexVal == 100 } {
            set hexValues [lreplace $hexValues $position $position "00"]
            set carryFwd 1
            incr position -1
         } elseif { [string length $hexVal] == 1 } {
            set hexValues [lreplace $hexValues $position $position "0$hexVal"]
            set carryFwd 0
         } else {
            set hexValues [lreplace $hexValues $position $position "$hexVal"]
            set carryFwd 0
         }
      }
      set mac ""
      for {set position 0 } {$position < 6} {incr position} {
         if { $position == 0 } {
            set mac "$mac[lindex $hexValues $position]"
         } else {
            set mac "$mac:[lindex $hexValues $position]"
         }
      }
      incr byCount -1
   }
   return "$mac"
}