{"id":146,"date":"2018-03-12T11:18:51","date_gmt":"2018-03-12T03:18:51","guid":{"rendered":"http:\/\/www.max-shu.com\/blog\/?p=146"},"modified":"2018-03-12T11:18:51","modified_gmt":"2018-03-12T03:18:51","slug":"python%e8%af%bb%e5%8f%96pxe%e5%90%af%e5%8a%a8%e6%97%b6%e7%9a%84%e6%9c%ba%e5%99%a8mac%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"http:\/\/www.max-shu.com\/blog\/?p=146","title":{"rendered":"Python\u8bfb\u53d6PXE\u542f\u52a8\u65f6\u7684\u673a\u5668MAC\u5730\u5740"},"content":{"rendered":"<p>\u670d\u52a1\u5668\u4e0a\u7535\uff0c\u901a\u8fc7PXE\u53e3\u542f\u52a8\uff0c\u6279\u91cf\u5b89\u88c5\u65f6\u9700\u8981GUID\u6216\u8005MAC\u5730\u5740\u6765\u533a\u5206\u4e0d\u540c\u673a\u5668\uff0c\u6709\u4e9b\u670d\u52a1\u5668\u5382\u5bb6\u51fa\u5382\u65f6\u6ca1\u6709\u628amac\u5730\u5740\u8d34\u51fa\u6765\uff0c\u6bd4\u5982HP\uff0c\u6709\u4e9b\u5382\u5bb6\u8d34\u5728\u5305\u88c5\u7bb1\u548c\u673a\u5668\u540e\u9762\u677f\uff0c\u6bd4\u5982DELL\uff0c\u6ca1\u6709\u7684\u8bdd\uff0c\u53ef\u4ee5\u7ed9\u670d\u52a1\u5668\u52a0\u7535\uff0c\u8ba9\u5176\u7528PXE\u542f\u52a8\uff0c\u5411\u5916\u53d1\u9001DHCP DISCOVER\u8bf7\u6c42\uff0c\u901a\u8fc7\u4ee5\u4e0b\u811a\u672c\u5c31\u53ef\u4ee5\u6536\u5230\u8be5\u673a\u5668\u7684mac\u5730\u5740\uff0c\u9700\u8981\u81ea\u5df1\u7684\u7b14\u8bb0\u672c\u8bbe\u7f6e\u4e3a\u9759\u6001ip\u683c\u5f0f\uff0c\u5177\u4f53ip\u5730\u5740\u968f\u4fbf\u3002<br \/>\n\u5f53\u7136\u5982\u679c\u4ea4\u6362\u673a\u53ef\u767b\u5165\u7684\u8bdd\uff0c\u53ef\u4ee5\u7528show mac address-table\u770b\u5230\u6240\u6709\u8fde\u63a5\u5230\u8be5\u4ea4\u6362\u673a\u7684\u673a\u5668\u7684mac\u5730\u5740\u548c\u5bf9\u5e94\u7aef\u53e3\u53f7\u3002<\/p>\n<p>#!\/usr\/bin\/env python<br \/>\n# -*- coding: gbk -*-<\/p>\n<p>import os, sys, string<br \/>\nimport time<br \/>\nimport socket<br \/>\nimport traceback<br \/>\nimport msvcrt<br \/>\nimport array<br \/>\nimport struct<\/p>\n<p>programName=&#8221;<\/p>\n<p>class GetMacAddr_Class(object):<br \/>\ndef __init__(self):<br \/>\nself.mac_list = []<\/p>\n<p>#print get_ip_address(&#8216;lo&#8217;)<br \/>\n#print get_ip_address(&#8216;eth0&#8217;)<br \/>\n#print get_ip_address(&#8216;eth1&#8217;)<br \/>\n#print get_ip_address(&#8216;eth2&#8217;)<br \/>\n#print get_ip_address(&#8216;bond0&#8217;)<\/p>\n<p>def __del__(self):<br \/>\npass<\/p>\n<p>def Run(self):<br \/>\ns=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)<br \/>\n# Allow multiple sockets to use the same PORT number<br \/>\ns.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)<\/p>\n<p>self_ip=&#8221;\u00a0 # Symbolic name meaning all available interfaces<br \/>\nrecv_port = 67<\/p>\n<p>f = file(&#8216;mac_addr_list.csv&#8217;,&#8217;at+&#8217;)<br \/>\nf.seek(0, os.SEEK_END)<br \/>\nline_index = 0<\/p>\n<p>s.bind((self_ip, recv_port))<br \/>\ns.setblocking(False)<br \/>\nmsg_num = 0<br \/>\nprint &#8220;\u7b49\u5f85\u6570\u636e\u4e2d(\u6309&#8217;z&#8217;\u952e\u6b63\u5e38\u9000\u51fa)\u3002\u3002\u3002\u3002&#8221;<br \/>\nwhile 1:<br \/>\nif msvcrt.kbhit():<br \/>\nch = msvcrt.getch()<br \/>\n#print ch<br \/>\nif ch == &#8216;\\x5A&#8217; or ch == &#8216;\\x7A&#8217;: # &#8216;Z&#8217; or &#8216;z&#8217;<br \/>\nbreak;<br \/>\ntime.sleep(0.001)<br \/>\ntry:<br \/>\ndata,src_sock_addr = s.recvfrom(8192)<br \/>\ndhcp_pkg = array.array(&#8216;B&#8217;,data)<br \/>\nprint &#8220;%s \u5728 %s\u53d1\u6765 %u\u5b57\u8282\u6570\u636e \uff1a%s &#8220;%(src_sock_addr,time.ctime(),len(dhcp_pkg),dhcp_pkg)<br \/>\nmsg_num += 1<br \/>\nprint &#8216;########## No. %4u Start ##################################################&#8217; % (msg_num)<br \/>\nprint &#8216;DHCP Message: &#8216;<br \/>\nindex = 0<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 op[1 byte]: 0x%02x (1:request, 2:response)&#8217; % (dhcp_pkg[index])<br \/>\nindex += 1<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 htype[1 byte]: 0x%02x (1:ethernet, &#8230;)&#8217; % (dhcp_pkg[index])<br \/>\nindex += 1<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 hlen[1 byte]: 0x%02x &#8216; % (dhcp_pkg[index])<br \/>\nindex += 1<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 hops[1 byte]: 0x%02x &#8216; % (dhcp_pkg[index])<br \/>\nindex += 1<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 xid[4 byte]: 0x%08x &#8216; % (toUint32(dhcp_pkg[index:]))<br \/>\nindex += 4<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 secs[2 byte]: 0x%04x &#8216; % (toUint16(dhcp_pkg[index:]))<br \/>\nindex += 2<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 flags[2 byte]: 0x%04x (0: unicast, non-0: broadcast)&#8217; % (toUint16(dhcp_pkg[index:]))<br \/>\nindex += 2<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 ciaddr[4 byte]: 0x%08x &#8216; % (toUint32(dhcp_pkg[index:]))<br \/>\nindex += 4<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 yiaddr[4 byte]: 0x%08x &#8216; % (toUint32(dhcp_pkg[index:]))<br \/>\nindex += 4<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 siaddr[4 byte]: 0x%08x &#8216; % (toUint32(dhcp_pkg[index:]))<br \/>\nindex += 4<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 giaddr[4 byte]: 0x%08x &#8216; % (toUint32(dhcp_pkg[index:]))<br \/>\nindex += 4<\/p>\n<p>print &#8216;\u00a0\u00a0\u00a0 chaddr[16 byte] HEX (MAC address): %02x&#8217; % (dhcp_pkg[index]),<br \/>\nfor i in range(1, 16):<br \/>\nprint &#8216;%02x&#8217; % (dhcp_pkg[index+i]),<br \/>\nprint &#8221;<br \/>\nmac_addr = array.array(&#8216;B&#8217;)<br \/>\nfor i in range(0, 16):<br \/>\nmac_addr.append(dhcp_pkg[index+i])<br \/>\nindex = 0<br \/>\nfor mac in self.mac_list:<br \/>\nif mac == mac_addr:<br \/>\nbreak<br \/>\nindex += 1<br \/>\nif index == len(self.mac_list):<br \/>\nself.mac_list += [ mac_addr]<br \/>\nline = &#8216;%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x&#8217; % \\<br \/>\n(mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5],mac_addr[6],mac_addr[7],<br \/>\nmac_addr[8],mac_addr[9],mac_addr[10],mac_addr[11],mac_addr[12],mac_addr[13],mac_addr[14],mac_addr[15])<br \/>\nline = &#8216;%u,%s,&#8217; % (line_index+1, line)<br \/>\nline_index += 1<br \/>\nprint &#8216;WRITE to file: &#8216; + line<br \/>\nf.write(line+&#8217;\\n&#8217;)<br \/>\nf.flush()<br \/>\nindex += 16<\/p>\n<p>print &#8216;\u00a0\u00a0\u00a0 sname[64 byte] HEX: %02x&#8217; % (dhcp_pkg[index]),<br \/>\nfor i in range(1, 64):<br \/>\nprint &#8216;%02x&#8217; % (dhcp_pkg[index+i]),<br \/>\nprint &#8221;<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 { sname[64 byte] STR: %s }&#8217; % (struct.unpack(&#8220;64s&#8221;,dhcp_pkg[index:index+64]))<br \/>\nindex += 64<\/p>\n<p>print &#8216;\u00a0\u00a0\u00a0 file[128 byte] HEX: %02x &#8216; % (dhcp_pkg[index]),<br \/>\nfor i in range(1, 128):<br \/>\nprint &#8216;%02x&#8217; % (dhcp_pkg[index+i]),<br \/>\nprint &#8221;<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 { file[128 byte] STR: %s }&#8217; % (struct.unpack(&#8220;128s&#8221;,dhcp_pkg[index:index+128]))<br \/>\nindex += 128<\/p>\n<p>print &#8216;\u00a0\u00a0\u00a0 options[64 byte] HEX: %02x &#8216; % (dhcp_pkg[index]),<br \/>\nfor i in range(1, 64):<br \/>\nprint &#8216;%02x&#8217; % (dhcp_pkg[index+i]),<br \/>\nprint &#8221;<br \/>\nprint &#8216;\u00a0\u00a0\u00a0 { options[64 byte] STR: %s }&#8217; % (struct.unpack(&#8220;64s&#8221;,dhcp_pkg[index:index+64]))<br \/>\nindex += 64<\/p>\n<p>print &#8216;########## No. %4u End ####################################################&#8217; % (msg_num)<br \/>\nexcept Exception,data:<br \/>\n#print traceback.print_exc()<br \/>\ncontinue<br \/>\ns.close()<br \/>\nf.close()<\/p>\n<p>for index, mac in enumerate(self.mac_list):<br \/>\nprint &#8216;No.%4u MAC : &#8216; % (index),<br \/>\nfor i in range(0,16):<br \/>\nprint &#8216;%02x&#8217; % (mac[i]),<br \/>\nline = &#8216;%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x&#8217; % \\<br \/>\n(mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],mac[6],mac[7],mac[8],mac[9],mac[10],mac[11],mac[12],mac[13],mac[14],mac[15])<br \/>\nprint &#8216; ( &#8216; + line + &#8216; )&#8217;<\/p>\n<p>def toUint32(bytes):<br \/>\nuint32 = bytes[0]&lt;&lt;24<br \/>\nuint32 += bytes[1]&lt;&lt;16<br \/>\nuint32 += bytes[2]&lt;&lt;8<br \/>\nuint32 += bytes[3]<br \/>\nreturn uint32<\/p>\n<p>def toUint16(bytes):<br \/>\nuint16 = bytes[0]&lt;&lt;8<br \/>\nuint16 += bytes[1]<br \/>\nreturn uint16<\/p>\n<p># Unix:<br \/>\n&#8221;&#8217;<br \/>\ndef get_ip_address(ifname=&#8221;eth0&#8243;):<br \/>\ns = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<br \/>\nip = &#8221;<br \/>\ntry:<br \/>\nip = socket.inet_ntoa(fcntl.ioctl(<br \/>\ns.fileno(),<br \/>\n0x8915, # SIOCGIFADDR<br \/>\nstruct.pack(&#8216;256s&#8217;, ifname[:15])<br \/>\n)[20:24])<br \/>\nexcept Exception,data:<br \/>\nprint traceback.print_exc()<br \/>\nreturn ip<br \/>\n&#8221;&#8217;<\/p>\n<p># Windows:<br \/>\ndef get_ip_address():<br \/>\nip = socket.gethostbyname(socket.gethostname())<br \/>\nreturn ip<\/p>\n<p>def OutputHelp():<br \/>\nprint &#8216;Get MAC address by PXE DHCP broadcast packet: &#8216;<br \/>\nprint &#8216;Usage: &#8216; + programName + &#8221;<br \/>\nprint &#8216;Example: &#8216; + programName + &#8221;<br \/>\nprint &#8216; &#8216;<\/p>\n<p>if __name__ == &#8216;__main__&#8217; :<br \/>\nprogramName = sys.argv[0]<br \/>\nif len(sys.argv) &gt; 1:<br \/>\nOutputHelp()<br \/>\nsys.exit(0)<\/p>\n<p>GetMacAddr = GetMacAddr_Class()<br \/>\nGetMacAddr.Run()<\/p>\n<p>sys.exit()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u670d\u52a1\u5668\u4e0a\u7535\uff0c\u901a\u8fc7PXE\u53e3\u542f\u52a8\uff0c\u6279\u91cf\u5b89\u88c5\u65f6\u9700\u8981GUID\u6216\u8005MAC\u5730\u5740\u6765\u533a\u5206\u4e0d\u540c\u673a\u5668\uff0c\u6709\u4e9b\u670d\u52a1\u5668\u5382\u5bb6\u51fa\u5382\u65f6\u6ca1\u6709\u628am &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[109,98,108],"class_list":["post-146","post","type-post","status-publish","format-standard","hentry","category-linuxandroid","tag-mac","tag-pxe","tag-python"],"views":1895,"_links":{"self":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=146"}],"version-history":[{"count":1,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":147,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/146\/revisions\/147"}],"wp:attachment":[{"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-shu.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}