tcp traceroute

Discussion in 'Python' started by Thomas Guettler, Nov 28, 2007.

  1. 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()
     
    Thomas Guettler, Nov 28, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Heiko Besemann
    Replies:
    1
    Views:
    2,881
    Harsh Thakur
    Feb 19, 2004
  2. Tiger
    Replies:
    5
    Views:
    973
    Dave Thompson
    May 1, 2006
  3. clint
    Replies:
    2
    Views:
    72
    A. Sinan Unur
    Jun 18, 2005
  4. Replies:
    0
    Views:
    242
  5. Roy Smith
    Replies:
    4
    Views:
    301
    Chris Angelico
    Nov 18, 2012
Loading...

Share This Page