Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #84102 > unrolled thread

python traceroute

Started byChandrakant Tiwari <chandrakant.tiwari204@gmail.com>
First post2015-01-20 19:37 -0800
Last post2015-01-21 09:41 -0500
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  python traceroute Chandrakant Tiwari <chandrakant.tiwari204@gmail.com> - 2015-01-20 19:37 -0800
    Re: python traceroute Steven D'Aprano <steve@pearwood.info> - 2015-01-21 04:02 +0000
    Re: python traceroute Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-21 05:06 +0000
      Re: python traceroute William Ray Wing <wrw@mac.com> - 2015-01-21 09:41 -0500

#84102 — python traceroute

FromChandrakant Tiwari <chandrakant.tiwari204@gmail.com>
Date2015-01-20 19:37 -0800
Subjectpython traceroute
Message-ID<dd1c4ee0-a65d-4d0b-915e-b7c306126685@googlegroups.com>
in the program below i want it to make it work  the same way as TRACERT  command . but i am not able to make it  work the same way . for  which i need  your help thank  you 
 here is the program




#!/usr/bin/python
import socket
import struct
import sys

# We want unbuffered stdout so we can provide live feedback for
# each TTL. You could also use the "-u" flag to Python.
class flushfile(file):
    def __init__(self, f):
        self.f = f
    def write(self, x):
        self.f.write(x)
        self.f.flush()

sys.stdout = flushfile(sys.stdout)

def main(dest_name):
    dest_addr = socket.gethostbyname(dest_name)
    port = 33434
    max_hops = 10
    icmp = socket.getprotobyname('icmp')
    udp = socket.getprotobyname('udp')
    ttl = 1
    while True:
        recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
        send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
        send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
        
        # Build the GNU timeval struct (seconds, microseconds)
        timeout = struct.pack("ll", 5, 0)
        
        # Set the receive timeout so we behave more like regular traceroute
        recv_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, timeout)
        
        recv_socket.bind(("", port))
        sys.stdout.write(" %d  " % ttl)
        send_socket.sendto("", (dest_name, port))
        curr_addr = None
        curr_name = None
        finished = False
        tries = 3
        while not finished and tries > 0:
            try:
                _, curr_addr = recv_socket.recvfrom(512)
                finished = True
                curr_addr = curr_addr[0]
                try:
                    curr_name = socket.gethostbyaddr(curr_addr)[0]
                except socket.error:
                    curr_name = curr_addr
            except socket.error as (errno, errmsg):
                tries = tries - 1
                sys.stdout.write("* ")
        
        send_socket.close()
        recv_socket.close()
        
        if not finished:
            pass
        
        if curr_addr is not None:
            curr_host = "%s (%s)" % (curr_name, curr_addr)
        else:
            curr_host = ""
        sys.stdout.write("%s\n" % (curr_host))

        ttl += 1
        if curr_addr == dest_addr or ttl > max_hops:
            break

if __name__ == "__main__":
    main('google.com')

[toc] | [next] | [standalone]


#84103

FromSteven D'Aprano <steve@pearwood.info>
Date2015-01-21 04:02 +0000
Message-ID<54bf24d6$0$2738$c3e8da3$76491128@news.astraweb.com>
In reply to#84102
On Tue, 20 Jan 2015 19:37:26 -0800, Chandrakant Tiwari wrote:

> in the program below i want it to make it work  the same way as TRACERT 
> command . but i am not able to make it  work the same way . for  which i
> need  your help thank  you

What is the difference between TRACERT and your Python script?

What do you expect your script to do that it doesn't do, or what does it 
do that it shouldn't do? Please be explicit:


# Wrong, don't do this.
"Program should work just like TRACERT."

# A little better, but still wrong.
"Program should work just like TRACERT on operating system Foo version 7 
when running this command: tracert -options argument"

# Better.
"I tried to do <SOME TASK> where I expected to get <THESE RESULTS> which 
matches this TRACERT command <COMMAND> on <OPERATING SYSTEM>, but instead 
I got <THESE UNWANTED RESULTS>."


Otherwise, we have to:

- guess what results you want
- guess what results you got
- guess what causes the wrong results
- guess what changes you need to make




-- 
Steve

[toc] | [prev] | [next] | [standalone]


#84106

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-01-21 05:06 +0000
Message-ID<m9nc4c$i7h$2@dont-email.me>
In reply to#84102
On Tue, 20 Jan 2015 19:37:26 -0800, Chandrakant Tiwari wrote:

> in the program below i want it to make it work  the same way as TRACERT 
> command.

As an observation, you're re-inventing a wheel that already works 
perfectly well, in that any failings of tracert tend to be more down to 
the way routers are configured to handle icmp than the tracert 
application itself. Unless you're doing this purely as an exercise in 
socket programming with python, it might be better to find a new problem 
to solve.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#84128

FromWilliam Ray Wing <wrw@mac.com>
Date2015-01-21 09:41 -0500
Message-ID<mailman.17916.1421854902.18130.python-list@python.org>
In reply to#84106
> On Jan 21, 2015, at 12:06 AM, Denis McMahon <denismfmcmahon@gmail.com> wrote:
> 
> On Tue, 20 Jan 2015 19:37:26 -0800, Chandrakant Tiwari wrote:
> 
>> in the program below i want it to make it work  the same way as TRACERT 
>> command.
> 
> As an observation, you're re-inventing a wheel that already works 
> perfectly well, in that any failings of tracert tend to be more down to 
> the way routers are configured to handle icmp than the tracert 
> application itself. Unless you're doing this purely as an exercise in 
> socket programming with python, it might be better to find a new problem 
> to solve.
> 
> -- 
> Denis McMahon, denismfmcmahon@gmail.com
> -- 
> https://mail.python.org/mailman/listinfo/python-list

I’d further add that as a security measure these days, many (soon to be most) hosts are configured to throw icmp packets away without acknowledging them in any way.

Bill

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web