import os import subprocess import sys import argparse def main(arguments): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('-s', '--serialNumber', help='Serial Number', type=int, required=True) args = parser.parse_args(arguments) sn = args.serialNumber IPCalculate(sn) MACCalculate(sn) def MACCalculate(serialNum): base = "08-00-30-F3-0" # Construct 3 unique nibles. Highest bit (11) is always 0 x1 = "0" # Next bit (10) is the 8 bit from the SN x1 = x1 + str(serialNum >> 8 & 1) # Bit 9 is always 0 x1 = x1 + str(0) # Bits 8 and 7 are bits 7 and 6 from the the SN, respectively x1 = x1 + str(serialNum >> 7 & 1) x1 = x1 + str(serialNum >> 6 & 1) # Bits 5-0 are the opposite of the corresponding bit in the serial number. This part is stupider than it needs to be because the complement operator is confusing x2 = "" for i in range(6): bit = 5 - i check = (serialNum >> bit & 1) if check == 1: x2 = x2 + "0" else: x2 = x2 + "1" # Bit 6 is board dependant; 0 for T2, 1 for T1 bitT2 = x1 + "0" + x2 bitT1 = x1 + "1" + x2 hexT2 = str(hex(int(bitT2,2)))[-3:].replace("x", "0").upper() # the .replace at the end corrects for hexT1 = str(hex(int(bitT1,2)))[-3:].replace("x", "0").upper() # the case where trailing zeros are truncated MACT2 = base + hexT2[:1] + "-" + hexT2[1:] MACT1 = base + hexT1[:1] + "-" + hexT1[1:] print("T2 MAC Address: " + MACT2) print("T1 MAC Address: " + MACT1) def IPCalculate(serialNum): base = "192.168." if 0 <= serialNum < 128: z = "1." elif 128 <= serialNum < 256: z = "2." elif 256 <= serialNum < 384: z = "3." elif 384 <= serialNum < 512: z = "4." else: print("Unexpected SN, cannot calculate IP address") return # Get the 7 lowest bits of 2*SN extractor = int("1111111",2) xBits = (serialNum<<1)&extractor xT2 = str(254 - xBits) xT1 = str(255 - xBits) print ("T2 IP Address: " + base + z + xT2) print ("T1 IP Address: " + base + z + xT1) if __name__ == '__main__': sys.exit(main(sys.argv[1:]))