Apr 19, 2015 09:18 AM
(This post was last modified: Apr 19, 2015 09:29 AM by Dr_Ciphers.)
I have the following python code which implements a traceroute program. The python that i'm using is the 2.7.8
Although when executed i get the following error.
Traceback (most recent call last):
File "./traceroute.py", line 56, in <module>
main()
File "./traceroute.py", line 39, in main
a= send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument
Any ideas would be appreciated!
Thank you.
Code
#!/usr/bin/python
import sys
import socket
def main():
if len(sys.argv) > 1:
dest_name = sys.argv[1]
else:
print("traceroute.py hostname")
sys.exit(0)
#define destination address from hostname
dest_addr = socket.gethostbyname(dest_name)
#Return the protocol number for the icmp protocol.
icmp = socket.getprotobyname('icmp')
#Return the protocol number for the udp protocol.
udp = socket.getprotobyname('udp')
#define ttl as 1 since we want to increment it gradually
ttl = 1
port = 33434
while True:
#we need two sockets for our connection. One for sending packets and one for receiving
#we are going to receive the routers and servers responses in icmp.
#we are going to send packets by using the udp
#create socket for the icmp packets
recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
#create a socket for the udp packets
send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
#create a send socket object for sending udp packets
a= send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
recv_socket.bind(("", port))
send_socket.sendto("", (dest_name, port))
ttl +=1
# ... open connections ...
# ... print data ...
# ... break if useful ...
pass
if __name__ == '__main__':
main()Although when executed i get the following error.
Traceback (most recent call last):
File "./traceroute.py", line 56, in <module>
main()
File "./traceroute.py", line 39, in main
a= send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument
Any ideas would be appreciated!
Thank you.

