Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > tcp traceroute

Reply
Thread Tools

tcp traceroute

 
 
Thomas Guettler
Guest
Posts: n/a
 
      11-28-2007
Hi,

I want to write a small tcp traceroute script. I works, but how
can I get the IP of the hop that send 'no route to host'?

Result:
python tmp/tcptraceroute.py a.b.c.d 80
ttl=01: (113, 'No route to host')
ttl=02: (113, 'No route to host')
ttl=03: (113, 'No route to host')
ttl=04: timed out
ttl=05: timed out
ttl=06: timed out
ttl=07: timed out
ttl=08: timed out
ttl=09: OK


#!/usr/bin/env python
# tcptraceroute.py
# This script is in the public domain

import os
import sys
import struct
import socket

def usage():
print '''Usage: %s host port
Tries to connect to host at TCP port with increasing TTL (Time to live).
''' % os.path.basename(sys.argv[0])

def main():
if not len(sys.argv)==3:
usage()
sys.exit(1)
ttl=1
host, port = sys.argv[1:]
port=int(port)
for ttl in range(1, 30):
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_IP, socket.IP_TTL, struct.pack('I', ttl))
s.settimeout(2)
try:
s.connect((host, port))
except (socket.error, socket.timeout), err:
print 'ttl=%02d: %s' % (ttl, err)
s.close()
continue
except KeyboardInterrupt:
print 'ttl=%02d (KeyboardInterrupt)' % ttl
break
print 'ttl=%02d: OK' % (ttl)
break

if __name__=='__main__':
main()
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
send tcp raw socket (bogus tcp header length) Tiger C Programming 5 05-01-2006 05:53 AM
tcp/ip vs microsoft tcp/ip ver 6 john Computer Support 1 08-05-2005 02:45 PM
NAT two outside TCP ports to one inside TCP port Kevin Cisco 1 11-10-2004 08:15 AM
Secure and compressed TCP/IP connection (for any TCP/IP services and applications) Pavel Aronovich Computer Security 0 02-22-2004 08:35 AM
LPR/Standard, TCP/IP, HP TCP/IP Ports DJ Chiro MCSE 1 11-07-2003 08:06 PM



Advertisments