在 lua 中 ping 一个服务器

发布于 2024-12-26 23:32:37 字数 111 浏览 3 评论 0原文

只是好奇是否有一种 lua 方法可以在不使用 os.execute 的情况下 ping 服务器。目的是查看服务器是否启动。

我检查了 lua 套接字库,但我认为不支持 ICMP?有什么想法吗?

Just curious to see if there is a lua way to ping a server without using os.execute. The purpose being to see if a server is up.

I checked the lua sockets library but I don't think ICMP is supported? Any idea?

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

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

发布评论

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

评论(3

国际总奸 2025-01-02 23:32:37

您可以使用 io.popen() 执行 ping 命令。
例如,

local handler = io.popen("ping -c 3 -i 0.5 10.10.10.10")
local response = handler:read("*a")
print(response)

You can use io.popen() to execute ping commands.
e.g.,

local handler = io.popen("ping -c 3 -i 0.5 10.10.10.10")
local response = handler:read("*a")
print(response)
夜访吸血鬼 2025-01-02 23:32:37

据我所知,不,如果没有 root 访问权限,您无法发送 ICMP 原始数据包。这不是 Lua 限制,而是操作系统限制。

要获得 root 访问权限,最好的方法是拥有一个经过良好测试的小型程序,该程序是 SUID root,而不是使用 Lua 将整个应用程序更改为 SUID root。这意味着您最终将使用 os.execute()。操作系统提供的 ping 似乎是解决您问题的一个很好的命令,而不是编写您自己的程序。

我同意这并不理想(特别是因为这会创建操作系统特定的代码来处理各种 ping 命令)。但如果没有SUID函数调用,我认为没有更好的方法。

To the best of my knowledge, no, you can't send ICMP raw packets without root access. That's not a Lua limitation, it's an OS restriction.

To get root access, the best way is to have a small well tested program that's SUID root rather than changing your entire application with Lua to be SUID root. This means you'll end up using os.execute(). And rather than writing your own program, the OS provided ping seems to be a nice command for solving your issue.

I agree it's not ideal (especially since this creates OS specific code to handle the various ping commands). But without a SUID function call, I don't think there's any better way.

小糖芽 2025-01-02 23:32:37

如果您在 MacOS 上的 Hammerspoon 中使用 Lua,那么它内置了对在其 hs.network.ping 库 例如:来自 Hammerspoon控制台:

hs.network.ping.ping('1.1.1.1')
hs.network.ping: 1.1.1.1 (0x600001d29000)
2023-04-03 12:39:42: PING: 1.1.1.1 (1.1.1.1):
2023-04-03 12:39:43: 64 bytes from 1.1.1.1: icmp_seq=0 time=23.463 ms
2023-04-03 12:39:44: 64 bytes from 1.1.1.1: icmp_seq=1 time=16.267 ms
2023-04-03 12:39:45: 64 bytes from 1.1.1.1: icmp_seq=2 time=16.820 ms
2023-04-03 12:39:46: 64 bytes from 1.1.1.1: icmp_seq=3 time=12.700 ms
2023-04-03 12:39:47: 64 bytes from 1.1.1.1: icmp_seq=4 time=13.559 ms
2023-04-03 12:39:47: --- 1.1.1.1 ping statistics ---
5 packets transmitted, 5 packets received, 0.0 packet loss
round-trip min/avg/max = 12.700/16.562/23.463 ms

还有一个旧的 hping 到 lua 的端口,它允许 hping 使用 ICMP、TCP、UDP 或 RAW 的各种“ping”选项...

If you're using Lua in Hammerspoon on MacOS then it has built in support for doing ping in its hs.network.ping library e.g: from the Hammerspoon console:

hs.network.ping.ping('1.1.1.1')
hs.network.ping: 1.1.1.1 (0x600001d29000)
2023-04-03 12:39:42: PING: 1.1.1.1 (1.1.1.1):
2023-04-03 12:39:43: 64 bytes from 1.1.1.1: icmp_seq=0 time=23.463 ms
2023-04-03 12:39:44: 64 bytes from 1.1.1.1: icmp_seq=1 time=16.267 ms
2023-04-03 12:39:45: 64 bytes from 1.1.1.1: icmp_seq=2 time=16.820 ms
2023-04-03 12:39:46: 64 bytes from 1.1.1.1: icmp_seq=3 time=12.700 ms
2023-04-03 12:39:47: 64 bytes from 1.1.1.1: icmp_seq=4 time=13.559 ms
2023-04-03 12:39:47: --- 1.1.1.1 ping statistics ---
5 packets transmitted, 5 packets received, 0.0 packet loss
round-trip min/avg/max = 12.700/16.562/23.463 ms

There's also an old port of hping to lua which would allow for hping's wide array of 'ping' options using ICMP, TCP, UDP, or RAW ....

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