Code to ping request using raw python socket

Namaste Friends,




Today, In this tutorial I am going to tell you about how you can create a simple ping script using python raw sockets.


Introduction


Ping is a simple concept to find and ensure that a host computer or device the user wants to connect is actually working and available on users network. In simple words, With the help of ping request, a user can find available and operate IP address on to its network. Ping works by Sending ICMP packet on any specific network interface and wait for its reply. ICMP stands for Internet control message protocol.

Click here to read more.



Requirement


  • Python 2.x Or 3.x
  • ICMP packet script (Click here)
  • Root Permission to run it



Simplest Function To Send Ping Request (Click here For ICMPPacket Function)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#  
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

Explanation



  • line 5, To choice Random Integer For Identification
  • line 7, To make a raw ICMP packet bytes
  • line 12, To send Packet




Simplest Function To Receive Ping Reply (Click here For ICMPPacket Function)


 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
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

Explanation



  • line 5, To Record Current Time
  • line 8, To Handle socket timeout function
  • line 11, To Check Function Timeout status
  • line 15, To capture packet
  • line 18, To Extract ICMP header From Complete Packet
  • line 21, To Decode ICMP header
  • line 24, Verify ICMP packet reply




Complete Codes (Click here For ICMPPacket Function)




 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()

Explanation



  • line 58, To make a raw socket



This Tutorial Ends Here,
I Hope You Enyed it

Share this

Related Posts

Previous
Next Post »