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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | # import module import socket import os import random import time import select from ICMP import ICMPPacket, ext_icmp_header
def catch_ping_reply(s, ID, time_sent, timeout=1):
# create while loop while True: starting_time = time.time() # Record Starting Time
# to handle timeout function of socket process = select.select([s], [], [], timeout) # check if timeout if process[0] == []: return
# receive packet rec_packet, addr = s.recvfrom(1024)
# extract icmp packet from received packet icmp = rec_packet[20:28]
# extract information from icmp packet _id = ext_icmp_header(icmp)['id']
# check identification if _id == ID: return ext_icmp_header(icmp) return
# def single_ping_request(s, addr=None):
# Random Packet Id pkt_id = random.randrange(10000,65000) # Create ICMP Packet packet = ICMPPacket(icmp_id=pkt_id).raw
# Send ICMP Packet while packet: sent = s.sendto(packet, (addr, 1)) packet = packet[sent:]
return pkt_id
def main(): # create socket s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) # take Input addr = raw_input("[+] Enter Domain Name : ") or "www.google.com" # Request sent ID = single_ping_request(s, addr)
# Catch Reply reply = catch_ping_reply(s, ID, time.time())
if reply: print reply
# close socket s.close() return
if __name__=='__main__': main()
|