Python 需要有关在Unix中排除跟踪路由故障的帮助吗

Python 需要有关在Unix中排除跟踪路由故障的帮助吗,python,unix,traceroute,Python,Unix,Traceroute,我有一个用于Unix系统的traceroute Python程序,它打印出数据包从本地计算机到目的地的路径,即数据包经过的路由器序列。问题是我得到的输出显示: traceroute to yahoo.co.in (68.180.206.184), 30 hops max, 60 byte packets 1 * * * 2 * * * 3 * * * 4 * * * 5 * * * 6 * * * 7 * * * 8 * * * 9 * * * . .

我有一个用于Unix系统的traceroute Python程序,它打印出数据包从本地计算机到目的地的路径,即数据包经过的路由器序列。问题是我得到的输出显示:

traceroute to yahoo.co.in (68.180.206.184), 30 hops max, 60 byte packets

 1  * * *
 2  * * *
 3  * * *
 4  * * *
 5  * * *
 6  * * *
 7  * * *
 8  * * *
 9  * * *
 .
 .
 .
 30  * * *
我有DSL连接。该程序与Windows命令行(cmd.exe)配合使用效果很好。上述输出的确切原因是什么

代码如下所示:

#!/usr/bin/python
import socket
def main(dest_name):
    dest_addr = socket.gethostbyname(dest_name)
    port = 33434
    max_hops = 30
    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)
        recv_socket.bind(("", port))
        send_socket.sendto("", (dest_name, port))
        curr_addr = None
        curr_name = None
        try:
            _, curr_addr = recv_socket.recvfrom(512)
            curr_addr = curr_addr[0]
            try:
                curr_name = socket.gethostbyaddr(curr_addr)[0]
            except socket.error:
                curr_name = curr_addr
        except socket.error:
            pass
        finally:
            send_socket.close()
            recv_socket.close()
        if curr_addr is not None:
            curr_host = "%s (%s)" % (curr_name, curr_addr)
        else:
            curr_host = "*"
        print "%d\t%s" % (ttl, curr_host)
        ttl += 1
        if curr_addr == dest_addr or ttl > max_hops:
            break
if __name__ == "__main__":
    main('yahoo.co.in')**

traceroute/tracert在Linux和Windows上的作用不同

Linux将发送一个TTL降低的UDP数据包,并侦听ICMP响应。Windows发送ICMP回显请求并侦听ICMP响应

Python版本失败,因为UDP数据包被阻止

在类Unix操作系统上,traceroute实用程序默认使用目标端口号为33434到33534的用户数据报协议(UDP)数据报。traceroute实用程序通常有一个选项来指定ICMP回显请求(类型8)的使用,就像Windows tracert实用程序所使用的那样


你应该仔细阅读。这对我很有用。你是以root身份运行的吗?使用Linux发行版?您所说的“程序与windows cmd配合使用非常好”是什么意思?同样的Python程序?Windows
tracert
命令?如果Python程序可以在Windows XP上运行,但不能在Linux上运行,那么可能是因为您需要是root用户才能在Linux上打开原始套接字。您好,该程序只有在与win cmd tracert结合使用时才能在Windows系统上以Python IDLE运行,否则不会运行。我甚至尝试以root用户身份运行它,但失败了?显示错误:socket.gaierror:[Errno-5]没有与hostnameWorks关联的地址,如预期的那样-如果发送的icmp ping消息没有应答,则tracerout的输出中会显示星号。如果在unix下而不是在windows计算机上,防火墙是否可能阻止您的icmp ping包?