需要帮助解决 Unix 中的路由跟踪问题

发布于 2024-10-15 21:21:24 字数 1704 浏览 7 评论 0原文

我有一个用于 Unix 系统的跟踪路由 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')**

I have a traceroute Python program for a Unix system that prints out the path the packets take to get from the local machine to a destination — that is, the sequence of routers that the packets go through. The problem is I get an output which displays:

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  * * *

I have a DSL connection. The program works great with the Windows command-line (cmd.exe). What is the exact reason for the above output?

The code looks like this:

#!/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')**

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-10-22 21:21:24

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

traceroute/tracert act differently on Linux and Windows.

Linux will send a UDP packet with a decreasing TTL and listen for ICMP responses. Windows sends ICMP echo requests and listens for ICMP responses.

The Python version is failing because the UDP packets are being blocked.

On Unix-like operating systems, the traceroute utility by default uses User Datagram Protocol (UDP) datagrams with destination port numbers from 33434 to 33534. The traceroute utility usually has an option to specify use of ICMP echo request (type 8) instead, as used by the Windows tracert utility.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文