import threadingimport timefrom netaddr import IPNetwork,IPAddress--snip--#监听的主机host = "192.168.0.187"#扫描的目标子网subnet = "192.168.0.0/24"#自定义的字符串,我们将在ICMP相应中进行核对。magic_message = "PYTHONRULE!"#批量发送UDP数据包def udp_sender(subnet,magic_message): time.sleep(5) sender = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) for ip in IPNetwork(subnet): try: sender.sendto(magic_message,("%s") % ip,65212) except: passt = threading.Thread(target=udp_sender,args=(subnet,magic_message))t.start()--snip--try: while True: --snip-- #print "ICMP --> TYPE: %d Code: %d (icmp_header.type,icmp_header.code) #检查类型和代码值是否为3 if icmp_header.code == 3 and icmp_header.type==3: #确认相应的主机在我们目标子网之内 if IPAddress(ip_header.src_address) in IPNetwork(subnet): if raw_buffer[len(raw_buffer)-len(magic_message):]==magic_message print "Host up:%s" % ip_header.src_address.