【问题标题】:Need help for troubleshooting traceroute in Unix需要帮助解决 Unix 中的 traceroute
【发布时间】:2011-02-08 04:48:04
【问题描述】:

我有一个用于 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')**

【问题讨论】:

  • 你应该仔细阅读this
  • 这对我有用。你以root身份运行它吗?使用 Linux 发行版?
  • “程序与 windows cmd 配合得很好”是什么意思?同一个 Python 程序? Windows tracert 命令?如果 Python 程序可以在 Windows XP 而不是 Linux 上运行,那可能是因为您需要 root 才能在 Linux 上打开原始套接字。
  • 您好,该程序仅在与 win cmd tracert 结合使用时才能在 windows 系统上以 python IDLE 运行,否则不会。我什至尝试以root身份运行它但失败了?显示错误:socket.gaierror: [Errno -5] No address associated with hostname
  • 按预期工作 - 如果对发送的 icmp-ping 消息没有应答,您会在 tracerout 的输出中看到星号。如果在 unix 下但不是从 windows 机器,防火墙是否有可能阻止您的 icmp-ping 包?

标签: python unix traceroute


【解决方案1】:

traceroute/tracert 在 Linux 和 Windows 上的行为不同。

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

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

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

http://en.wikipedia.org/wiki/Traceroute

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-24
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多