在 Python 中 Ping 服务器
在Python中,有没有办法通过ICMP ping服务器并在服务器响应时返回TRUE,如果没有响应则返回FALSE?
In Python, is there a way to ping a server through ICMP and return TRUE if the server responds, or FALSE if there is no response?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
如果您不需要支持 Windows,这里有一个非常简洁的方法:
此方法有效,因为如果连接失败,ping 将返回非零值。 (返回值实际上根据网络错误而有所不同。)您还可以使用“-t”选项更改 ping 超时(以秒为单位)。请注意,这会将文本输出到控制台。
If you don't need to support Windows, here's a really concise way to do it:
This works because ping returns a non-zero value if the connection fails. (The return value actually differs depending on the network error.) You could also change the ping timeout (in seconds) using the '-t' option. Note, this will output text to the console.
此功能适用于任何操作系统(Unix、Linux、macOS 和 Windows)
Python 2 和 Python 3
编辑:
由 @radato
os.system
替换为subprocess.call
。这可以避免在主机名字符串可能无法验证的情况下出现 shell 注入漏洞。请注意,根据 Windows 上的 @ikrase,此函数仍将返回
True
如果您收到目标主机无法访问
错误。说明
该命令在 Windows 和类 Unix 系统中都是
ping
。选项
-n
(Windows) 或-c
(Unix) 控制数据包数量,在本例中设置为 1。platform.system()
返回平台名称。前任。 macOS 上的“达尔文”
。subprocess.call()
执行系统调用。前任。subprocess.call(['ls','-l'])
。This function works in any OS (Unix, Linux, macOS, and Windows)
Python 2 and Python 3
EDITS:
By @radato
os.system
was replaced bysubprocess.call
. This avoids shell injection vulnerability in cases where your hostname string might not be validated.Note that, according to @ikrase on Windows this function will still return
True
if you get aDestination Host Unreachable
error.Explanation
The command is
ping
in both Windows and Unix-like systems.The option
-n
(Windows) or-c
(Unix) controls the number of packets which in this example was set to 1.platform.system()
returns the platform name. Ex.'Darwin'
on macOS.subprocess.call()
performs a system call. Ex.subprocess.call(['ls','-l'])
.有一个名为 pyping 的模块可以做到这一点。它可以使用 pip 安装,
使用起来非常简单,但是,在使用此模块时,您需要 root 访问权限,因为它是在幕后制作原始数据包。
There is a module called pyping that can do this. It can be installed with pip
It is pretty simple to use, however, when using this module, you need root access due to the fact that it is crafting raw packets under the hood.
对于python3,有一个非常简单方便的python模块ping3:(
pip install ping3
,需要root权限)。该模块还允许自定义一些参数。
For python3 there's a very simple and convenient python module ping3: (
pip install ping3
, needs root privileges).This module allows for the customization of some parameters as well.
在python3中使用socket包:
using socket package in python3:
由于发送原始 ICMP 数据包需要提升权限,因此编程 ICMP ping 很复杂,而且调用
ping
二进制文件也很难看。对于服务器监控,您可以使用称为 TCP ping 的技术来实现相同的结果:在内部,这只是建立到目标服务器的 TCP 连接并立即断开连接,测量所用的时间。这个特定的实现有一点限制,因为它不处理关闭的端口,但对于您自己的服务器来说,它工作得很好。
Programmatic ICMP ping is complicated due to the elevated privileges required to send raw ICMP packets, and calling
ping
binary is ugly. For server monitoring, you can achieve the same result using a technique called TCP ping:Internally, this simply establishes a TCP connection to the target server and drops it immediately, measuring time elapsed. This particular implementation is a bit limited in that it doesn't handle closed ports but for your own servers it works pretty well.
因为我想让我的 Python 程序在 2.7 和 3.x 版本以及 Linux、Mac OS 和 Windows 平台上通用,所以我必须修改现有的示例。
Because I like to have my Python program universal on version 2.7 and 3.x and on platform Linux, Mac OS and Windows, I had to modify the existing examples.
我的 ping 函数版本:
请随意使用它。
My version of a ping function:
Feel free to use it as you will.
环顾四周后,我最终编写了自己的 ping 模块,该模块旨在监视大量地址,是异步的,并且不使用大量系统资源。您可以在这里找到它:https://github.com/romana/multi-ping/它已获得 Apache 许可,因此您可以按照您认为合适的任何方式在项目中使用它。
实现我自己的方法的主要原因是其他方法的限制:
After looking around, I ended up writing my own ping module, which is designed to monitor large numbers of addresses, is asynchronous and doesn't use a lot of system resources. You can find it here: https://github.com/romana/multi-ping/ It's Apache licensed, so you can use it in your project in any way you see fit.
The main reasons for implementing my own are the restrictions of the other approaches:
如果您的服务器不支持 ICMP(防火墙可能会阻止它),它很可能仍然在 TCP 端口上提供服务。在这种情况下,您可以执行TCP ping1(独立于平台,无需安装额外的Python模块),如下所示:
代码取自此处。
1 TCP ping 并不真正存在,因为 ping 是在 ISO/OSI 第 3 层上使用 ICMP 执行的。TCP ping 是在 ISO/OSI 第 4 层上执行的。它只是尝试以最基本的方式连接到 TCP 端口,即不传输任何数据,而是在连接后立即关闭连接。
If your server does not support ICMP (firewall might block it), it most probably still offers a service on a TCP port. In this case, you can perform a TCP ping1 (platform independently and without installing additional python modules) like this:
The code is taken and only slightly modified from here.
1 A TCP ping does not really exist as a ping is performed with ICMP on ISO/OSI layer 3. A TCP ping is performed on ISO/OSI layer 4. It just tries to connect to a TCP port in the most basic way, that it is not transmitting any data, but closing the connection immediately after connecting.
我用以下方法解决了这个问题:
“TTL” 是了解 ping 是否正确的方法。
萨卢多斯
I resolve this with:
"TTL" is the way to know if the ping is correctly.
Saludos
确保 pyping 已安装或安装它 pip install pyping
Make Sure pyping is installed or install it pip install pyping
在 Linux 上,无需 root(或 setuid 或
CAP_NET_RAW
)即可创建 ICMP 数据报(非原始)套接字:https://unix.stackexchange.com/a/592914。我最终得到了虽然其他答案在这里建议的许多软件包也可以工作
on linux, it's possible to create ICMP datagram (not raw) sockets without being root (or setuid or
CAP_NET_RAW
): https://unix.stackexchange.com/a/592914. I ended up withthough many of the packages other answers have suggested here would work too
我使用这篇文章中答案的想法进行减少,但仅使用较新推荐的子进程模块和 python3:
My reduction using ideas from answers in this post but only using the newer recommended subprocess module and python3:
该脚本适用于 Windows,并且应该适用于其他操作系统:
它可以在 Windows、Debian 和 Macosx 上运行,需要在Solaris 上进行测试。
This script works on Windows, and should work on other OSes :
It works on Windows, Debian, and macosx, need a test on solaris.
使用Multi-ping(
pip install multiPing
)我编写了这个简单的代码(如果您愿意,只需复制并粘贴即可!):用法:
如果你想要单个样本,第二个参数“
10
”可以忽略!希望有帮助!
Using Multi-ping (
pip install multiPing
) I made this simple code (simply copy and paste if you will!):Usage:
If you want a single sample, the second parameter "
10
" can be ignored!Hope it helps!
看起来很简单,但让我很不舒服。我不断收到“不允许 icmp 打开套接字操作”的消息,否则如果服务器离线,解决方案将挂起。但是,如果您想知道服务器是否处于活动状态并且您正在该服务器上运行 Web 服务器,那么curl 将完成这项工作。如果您有 ssh 和证书,那么 ssh 和一个简单的命令就足够了。这是代码:
Seems simple enough, but gave me fits. I kept getting "icmp open socket operation not permitted" or else the solutions would hang up if the server was off line. If, however, what you want to know is that the server is alive and you are running a web server on that server, then curl will do the job. If you have ssh and certificates, then ssh and a simple command will suffice. Here is the code:
使用它,它在 python 2.7 上进行了测试,工作正常,如果成功则返回 ping 时间(以毫秒为单位),如果失败则返回 False。
Use this it's tested on python 2.7 and works fine it returns ping time in milliseconds if success and return False on fail.
我有类似的要求,所以我实现了它,如下所示。它在 Windows 64 位和 Linux 上进行了测试。
当 IP 不可访问时, subprocess.check_output() 会引发异常。可以通过从输出行“数据包:已发送 = 2,已接收 = 2,丢失 = 0(0% 丢失)”中提取信息来完成额外验证。
I had similar requirement so i implemented it as shown below. It is tested on Windows 64 bit and Linux.
When IP is not reachable subprocess.check_output() raises an exception. Extra verification can be done by extracting information from output line 'Packets: Sent = 2, Received = 2, Lost = 0 (0% loss)'.
我最终发现了关于类似场景的这个问题。我尝试了 pyping,但 Naveen 给出的示例在 Python 2.7 下的 Windows 中对我不起作用。
对我有用的一个例子是:
I ended up finding this question regarding a similar scenario. I tried out pyping but the example given by Naveen didn't work for me in Windows under Python 2.7.
An example that worked for me is:
仅限 WINDOWS - 不敢相信没有人破解 Win32_PingStatus
使用简单的 WMI 查询,我们免费返回一个包含真正详细信息的对象
示例输出
WINDOWS ONLY - Can't believe no-ones cracked open Win32_PingStatus
Using a simple WMI query we return an object full of really detailed info for free
sample output
以下是使用 Python 的
subprocess
模块和底层操作系统提供的ping
CLI 工具的解决方案。在 Windows 和 Linux 上进行了测试。支持设置网络超时。不需要 root 权限(至少在 Windows 和 Linux 上)。Here's a solution using Python's
subprocess
module and theping
CLI tool provided by the underlying OS. Tested on Windows and Linux. Support setting a network timeout. Doesn't need root privileges (at least on Windows and Linux).我需要更快的 ping 扫描,并且不想使用任何外部库,因此我决定使用内置
asyncio
来使用并发。此代码需要 python 3.7+,并且仅在 Linux 上制作和测试。它无法在 Windows 上运行,但我相信您可以轻松地将其更改为在 Windows 上运行。
我不是
asyncio
方面的专家,但我使用了这篇很棒的文章加速你的Python并发编程,我想出了这些代码行。我试图使其尽可能简单,因此您很可能需要向其中添加更多代码以满足您的需求。它不返回 true 或 false,我认为让它打印响应 ping 请求的 IP 会更方便。我认为它相当快,在近 10 秒内 ping 255 ips。
示例输出:
请注意,IP 不按顺序排列,因为 IP 在回复后立即打印,因此首先响应的 IP 将首先打印。
I needed a faster ping sweep and I didn't want to use any external libraries, so I resolved to using concurrency using built-in
asyncio
.This code requires python 3.7+ and is made and tested on Linux only. It won't work on Windows but I am sure you can easily change it to work on Windows.
I ain't an expert with
asyncio
but I used this great article Speed Up Your Python Program With Concurrency and I came up with these lines of codes. I tried to make it as simple as possible, so most likely you will need to add more code to it to suit your needs.It doesn't return true or false, I thought it would be more convenient just to make it print the IP that responds to a ping request. I think it is pretty fast, pinging 255 ips in nearly 10 seconds.
Sample output:
Note that the IPs are not in order, as the IP is printed as soon it replies, so the one that responds first gets printed first.
我借用了其他答案。尝试简化和最小化查询。
编辑:根据 Olivier B. 的评论,忽略大小写。
My take borrowing from other answers. Attempt to simplify and minimize queries.
EDIT: ignoring case in return as per comment by Olivier B.
我想出的唯一一个半途而废的解决方案是:
通过简单的套接字使用非特权帐户在 Windows 和 Linux 下工作。
显然还支持异步/多“ping”。
https://pypi.org/project/tcppinglib/
The only half-way satisfying solution I came up with is this:
Works under Windows and Linux using unprivileged accounts via simple sockets.
Apparently also supports async/multi "ping".
https://pypi.org/project/tcppinglib/
在 Windows 或 Linux 中对它们进行 ping 操作,返回排序后的列表。这是@Ahmed Essam 和@Arno 回复的混合/修复。
Ping them all in windows or linux, return a sorted list. This is a hybrid/fix from the responses @Ahmed Essam and @Arno.