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=False) args = parser.parse_args(arguments) sn = args.serialNumber if (type(sn) is int): IPCalculate(sn) MACCalculate(sn) else: print("Run with argument \"-s X\", where X is the serial number of your AMC13") 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 bits in the serial number. x2 = format(~serialNum & 0b111111, '06b') # 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 xT2 = str(254 & ~(2*serialNum)) xT1 = str(255 & ~(2*serialNum)) print ("T2 IP Address: " + base + z + xT2) print ("T1 IP Address: " + base + z + xT1) if __name__ == '__main__': sys.exit(main(sys.argv[1:]))