Question
ICMP Traceroute Python. Below is the code I have from socket import * import os import sys import struct import time import select import binascii
ICMP Traceroute Python. Below is the code I have
from socket import * import os import sys import struct import time import select import binascii
ICMP_ECHO_REQUEST = 8 MAX_HOPS = 30 TIMEOUT = 2.0 TRIES = 2
# The packet that we shall send to each router along the path is the ICMP echo # request packet, which is exactly what we had used in the ICMP ping exercise. # We shall use the same packet that we built in the Ping exercise def checksum(str): # In this function we make the checksum of our packet # hint: see icmpPing lab def build_packet(): # In the sendOnePing() method of the ICMP Ping exercise ,firstly the header of our # packet to be sent was made, secondly the checksum was appended to the header and # then finally the complete packet was sent to the destination.
# Make the header in a similar way to the ping exercise. # Append checksum to the header.
# Dont send the packet yet , just return the final packet in this function.
# So the function ending should look like this packet = header + data return packet def get_route(hostname): timeLeft = TIMEOUT for ttl in xrange(1,MAX_HOPS): for tries in xrange(TRIES): destAddr = gethostbyname(hostname) #Fill in start # Make a raw socket named mySocket #Fill in end mySocket.setsockopt(IPPROTO_IP, IP_TTL, struct.pack('I', ttl)) mySocket.settimeout(TIMEOUT) try: d = build_packet() mySocket.sendto(d, (hostname, 0)) t= time.time() startedSelect = time.time() whatReady = select.select([mySocket], [], [], timeLeft) howLongInSelect = (time.time() - startedSelect) if whatReady[0] == []: # Timeout print " * * * Request timed out." recvPacket, addr = mySocket.recvfrom(1024) timeReceived = time.time() timeLeft = timeLeft - howLongInSelect if timeLeft <= 0: print " * * * Request timed out." except timeout: continue else: #Fill in start # Fetch the icmp type from the IP packet #Fill in end if type == 11: bytes = struct.calcsize("d") timeSent = struct.unpack("d", recvPacket[28:28 + bytes])[0] print " %d rtt=%.0f ms %s" %(ttl, (timeReceived -t)*1000, addr[0]) elif type == 3: bytes = struct.calcsize("d") timeSent = struct.unpack("d", recvPacket[28:28 + bytes])[0] print " %d rtt=%.0f ms %s" %(ttl, (timeReceived-t)*1000, addr[0]) elif type == 0: bytes = struct.calcsize("d") timeSent = struct.unpack("d", recvPacket[28:28 + bytes])[0] print " %d rtt=%.0f ms %s" %(ttl, (timeReceived - timeSent)*1000, addr[0]) return else: print "error" break finally: mySocket.close()
get_route("google.com")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started