2

I use the following code to generate ICMP requests, get the results back and also get the round trip time for each request.

It works fine, except when I add icmp_type to the packet header. As long as I pass icmp_type, the code stops working.

I appreciate any help on this.

def icmp_ping(host, icmp_code, icmp_type=None, count=2):

    packet = Ether() / IP(dst=host, proto=1) / ICMP(code=icmp_code)

    t = 0.0
    for x in range(count):
        ans, unans = srp(packet, iface="h1-eth0", verbose=0)
        rx = ans[0][1]
        tx = ans[0][0]
        delta = rx.time - tx.sent_time
        print "Ping:", delta * 1000
        print packet.summary()
        t += (delta * 1000)
    return (t / count)

if __name__ == "__main__":
    for i in range(6):
        total = icmp_ping('10.0.0.3', i)
    print "TOTAL", total
aaron
  • 39,695
  • 6
  • 46
  • 102

1 Answers1

0

For ICMP request the code must be 0 and the type must be 8. In program you are assigning wrong value to code. The below code is enough to craft ICMP request as scapy's default ICMP packet is an icmp request.

packet = Ether() / IP(dst=host, proto=1) / ICMP()

rabhis
  • 440
  • 2
  • 5