如何使用本机 python 执行 ping 或 Traceroute?

发布于 2024-07-29 07:40:54 字数 75 浏览 5 评论 0原文

我希望能够从 Python 中执行 ping 和跟踪路由,而不必执行相应的 shell 命令,所以我更喜欢本地 python 解决方案。

I would like to be able to perform a ping and traceroute from within Python without having to execute the corresponding shell commands so I'd prefer a native python solution.

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

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

发布评论

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

评论(7

栖竹 2024-08-05 07:40:54

如果您不介意使用外部模块而不使用 UDP 或 TCP,scapy 是一个简单的解决方案:

from scapy.all import *
target = ["192.168.1.254"]
result, unans = traceroute(target,l4=UDP(sport=RandShort())/DNS(qd=DNSQR(qname="www.google.com")))

或者您可以使用 tcp 版本

from scapy.all import *
target = ["192.168.1.254"]
result, unans = traceroute(target,maxttl=32)

请注意,您必须以 root 身份运行 scapy 才能执行这些任务,否则您将得到:

socket.error: [Errno 1] Operation not permitted

If you don't mind using an external module and not using UDP or TCP, scapy is an easy solution:

from scapy.all import *
target = ["192.168.1.254"]
result, unans = traceroute(target,l4=UDP(sport=RandShort())/DNS(qd=DNSQR(qname="www.google.com")))

Or you can use the tcp version

from scapy.all import *
target = ["192.168.1.254"]
result, unans = traceroute(target,maxttl=32)

Please note you will have to run scapy as root in order to be able to perform these tasks or you will get:

socket.error: [Errno 1] Operation not permitted
段念尘 2024-08-05 07:40:54

出于安全原因,以 root 身份运行解释器通常会受到反对(当然,您确实需要具有 root 权限才能访问 ping 和 Traceroute 的 ICMP 规范所需的“原始”套接字!),但如果您对此没有任何问题这并不难——例如,这篇文章(死了?) 或者这篇文章给出了一个可行的 ping ,而 Jeremy Hylton 的旧页面仍然可以使用 ICMP 底层代码(都 ping和traceroute)虽然它是为非常旧的Python版本编写的,并且需要稍加修改才能与现代版本相匹配——但是,概念都在那里,在我给你的两个URL中!

Running interpreters as root is often frowned upon on security grounds (and of course you DO need to have root permission to access the "raw" socked as needed by the ICMP specs of ping and traceroute!), but if you have no problems with that it's not hard -- e.g., this post(dead?) or this post give a workable ping, and Jeremy Hylton's old page has still-usable underlying code for ICMP (both ping and traceroute) though it's written for very old Python versions and needs a litte facelift to shine with modern ones -- but, the concepts ARE all there, in both the URLs I gave you!

漫雪独思 2024-08-05 07:40:54

Webb 库 在执行各种与 Web 相关的提取方面非常方便......并且 ping 和 Traceroute 可以通过它轻松完成。 只需包含要跟踪路由的 URL:

import webb
webb.traceroute("your-web-page-url")

如果您希望自动将跟踪路由日志存储到文本文件,请使用以下命令:

webb.traceroute("your-web-page-url",'file-name.txt')

同样,可以使用以下代码行获取 URl(服务器)的 IP 地址

print(webb.get_ip("your-web-page-url"))

:它有助于!

The Webb Library is very handy in performing all kinds of web related extracts...and ping and traceroute can be done easily through it. Just include the URL you want to traceroute to:

import webb
webb.traceroute("your-web-page-url")

If you wish to store the traceroute log to a text file automatically, use the following command:

webb.traceroute("your-web-page-url",'file-name.txt')

Similarly a IP address of a URl (server) can be obtained with the following lines of code:

print(webb.get_ip("your-web-page-url"))

Hope it helps!

污味仙女 2024-08-05 07:40:54

mtrpacket 包可用于发送网络探测,它可以执行 ping 或跟踪路由。 由于它使用 mtr 命令行工具的后端,因此不需要您的脚本以 root 身份运行。

它还使用 asyncio 的事件循环,因此您可以同时进行多个正在进行的跟踪路由或 ping,并在完成时处理它们的结果。

下面是一个跟踪路由到“example.com”的 Python 脚本:

import asyncio
import mtrpacket

async def trace():
    async with mtrpacket.MtrPacket() as mtr:
        for ttl in range(1, 256):
            result = await mtr.probe('example.com', ttl=ttl)
            print(result)

            if result.success:
                break

asyncio.get_event_loop().run_until_complete(trace())

使用带有“ttl”的循环是因为传出数据包的“生存时间”决定了数据包在过期和发送之前将经过的网络跃点数。错误回到原始来源。

The mtrpacket package can be used to send network probes, which can perform either a ping or a traceroute. Since it uses the back-end to the mtr commandline tool, it doesn't require that your script run as root.

It also uses asyncio's event loop, so you can have multiple ongoing traceroutes or pings simultaneously, and deal with their results as they complete.

Here is a Python script to traceroute to 'example.com':

import asyncio
import mtrpacket

async def trace():
    async with mtrpacket.MtrPacket() as mtr:
        for ttl in range(1, 256):
            result = await mtr.probe('example.com', ttl=ttl)
            print(result)

            if result.success:
                break

asyncio.get_event_loop().run_until_complete(trace())

The loop with 'ttl' is used because the 'time-to-live' of an outgoing packet determines the number of network hops the packet will travel before expiring and sending an error back to the original source.

拿命拼未来 2024-08-05 07:40:54

您可能想查看 scapy 包。 它是Python网络工具中的瑞士军刀。

you might want to check out the scapy package. it's the swiss army knife of network tools for python.

莫言歌 2024-08-05 07:40:54

ICMP Ping 是作为 ICMP 协议一部分的标准。

Traceroute 使用 ICMP 和 IP 的功能通过生存时间值来确定路径。 使用 TTL 值,只要 IP/ICMP 工作,您就可以在各种协议中执行跟踪路由,因为 ICMP TTL EXceeded 消息告诉您路径中的跃点。

如果您尝试访问没有可用侦听器的端口,根据 ICMP 协议规则,主机应该发送 ICMP 端口不可达消息。

ICMP Ping is standard as part of the ICMP protocol.

Traceroute uses features of ICMP and IP to determine a path via Time To Live values. Using TTL values, you can do traceroutes in a variety of protocols as long as IP/ICMP work because it is the ICMP TTL EXceeded messages that tell you about the hop in the path.

If you attempt to access a port where no listener is available, by ICMP protocol rules, the host is supposed to send an ICMP Port Unreachable message.

酷到爆炸 2024-08-05 07:40:54

我用python编写了一个简单的tcptraceroute,它不需要root权限 http://www. thomas-guettler.de/scripts/tcptraceroute.py.txt

但它无法显示中间跳的IP地址。 但有时它很有用,因为您可以猜测阻止防火墙的位置:在路由的开头或结尾。

I wrote a simple tcptraceroute in python which does not need root privileges http://www.thomas-guettler.de/scripts/tcptraceroute.py.txt

But it can't display the IP addresses of the intermediate hops. But sometimes it is useful, since you can guess where the blocking firewall is: Either at the beginning or at the end of the route.

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