By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

tcp traceroute

P: n/a
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()
Nov 28 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.