Code WTF: Check Ethernet Address
So I guess I am daring, but not many software developers/programmers would publicly disparage their code like I am about to do. I was looking at some code I wrote about 3.5 years ago when I was first learning python during a summer job in the computer science department at my University and this is what I found.
Basically the intention of the code shutdown computers remotely(x number of hosts in a computer lab) in a wakeable state and had a function to do a wake on lan(for x number of wakeable hosts). This is used in a web based lab administration system and it works pretty well.
Here is the code in question(just the Wake on Lan part):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import socket import struct import string def Wol(eth_addr, ip): """wake on lan given an ethernet/mac address and IP""" byte_addr = [] # check that our eth address is the correct format addr = CheckEthFormat(eth_addr) if addr == False: print "Ethernet/MAC Address in wrong format" return False #take our given ethernet address and put it into 6 sub strings for x in range(len(addr)): if ((x*2) + 1) > len(addr): break byte_addr.append(addr[x*2] + addr[(x*2)+1]) #construct a string with a 6 byte hardware address eth_addrB = struct.pack('BBBBBB',int(byte_addr[0]), int(byte_addr[1]), int(byte_addr[2]), int(byte_addr[3]), int(byte_addr[4]), int(byte_addr[5])) #construct our message for the Wake on Lan packet # 12 leading F's + target machines mac address 16 times packet = "F" * 12 + eth_addrB * 16 #create a datagram socket to send our packet sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # send our message sock.sendto(packet, ip) #close the socket sock.close() |
So whats the wtf here?