The idea is 10/16 convert hex and string handling
Start-create the wheel
1, determines whether the address mac
Regular matches eligibility
1 import re 2 3 def isMac(string): 4 preg = re.compile('^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$') 5 ret = preg.match(string) 6 if ret is None: 7 return False 8 else: 9 return True
2, mac turn int
Replace the colon turn hex
1 def macToInt(mac): 2 mac = mac.replace(":", "") 3 return int(mac, 16)
3, int turn mac
After hexadecimal be back to fill the seats and fill colon
. 1 DEF intToMac (intMac): 2 # MAC address of each segment are less than two digits 0s example: F: 23 is: 54 is => 0F: 23 is: 54 is . 3 IF len (hex (intMac))% 2 =! 0 : . 4 hexStr = ' 0 {0: X-} ' .format (intMac) . 5 the else : . 6 hexStr = ' {0: X-} ' .format (intMac) . 7 . 8 I = 0 . 9 RET = "" 10 . 11 the while I < len = (hexStr) - 2 : 12 is IF RET == "" : 13 is ret = hexStr[i:(i + 2)] 14 else: 15 ret = "".join([ret, ":", hexStr[i:(i + 2)]]) 16 i = i + 2 17 return ret
4, Get next n mac
After transfection into numerical calculation int
1 Def getNextMac (macAddr, n): 2 return intToMac (macToInt (macAddr) + n)
5, obtaining the n mac
After transfection into numerical calculation int
1 Def backOldMac (macAddr, n): 2 return intToMac (macToInt (macAddr) - n)
6, mac address comparison
Comparing a value converted to an int
1 DEF compareMac (macA, out): 2 return macToInt (macA)> = macToInt (out)
7, mac address calculating the number of
After subtraction to int
1 def calcMacNum(mac_start, mac_end): 2 mac_start = mac_start.upper()
3 mac_end = mac_end.upper()
4 ret = macToInt(mac_end) - macToInt(mac_start) + 1
5 if ret < 0:
6 ret = 0
7 return ret
8, reverse mac address
Reverse order of the mac address, after removal of the colon and then added back to the inverting string colon
1 def macReverse(mac): 2 info = mac.split(":") 3 info.reverse() 4 return ":".join(info)