如何在网页中动态显示 ping 输出?

发布于 2024-09-08 22:53:12 字数 585 浏览 0 评论 0原文

作为诊断页面的一部分,我希望用户能够运行“ping”,即一个普通的 shell 命令,将 ICMP_ECHO_REQUST 发送到某个 IP,并在浏览器的 div 中动态显示结果。

后端是 Ruby/Rails。

我已经在服务器端运行该命令并读取 ping 命令的输出。

我一直在构建定期回调服务器以动态更新页面某些部分的网页。

但在这种情况下,存在三个挑战:

  • 能够让 ajax 回调到 uri/url 以找到运行 ping 命令的进程
  • 理想情况下 能够在 ping 命令返回新的数据行时更新页面
  • 可选地能够“打破”ping。通过 ping,我当然可以将选项设置为仅发送 x ping,然后退出,从而无需停止该进程。但我还有另一个工具,即日志查看器,并且该工具不会在一定行数后自行停止,而是在不中断的情况下永远继续,即使用 Control-C。

我是否设置一个内存缓存来与运行 ping 的进程会合,或者是否有更简单的方法?

我搜索了很多,认为这应该是一个足够常见的问题,有一个 Rails 插件可以神奇地实现所需的功能,但我根本没有找到太多。

有什么建议或指示吗?

As part of a diagnostics page I would like to have the user be able to run a "ping", ie an ordinary shell command to send ICMP_ECHO_REQUSTs to a certain IP and display the resuls dynamically in a div in the browser.

The backend is Ruby/Rails.

I'm past running the command on the server side and reading the output from the ping command.

And I've been building web pages that periodically calls back to the server to update som parts of the page dynamically.

But in this case there are three challenges:

  • be able to have the ajax call back to an uri/url to find the process that runs the ping command
  • ideally be able to update the page when the ping command returns a new line of data
  • Optionally be able to "break" the ping. With ping I can of course just set the option to only send x pings and then exit and thus eliminating the need to stop the process. But I also have another tool that would be next, a log viewer, and that tool does not stop by itself after a certain number of lines but continue forever if not interrupted, ie with Control-C.

Do I set up a memcache to rendez-vous with the process running ping or is there a simpler way?

I searched a lot thinking this should be a problem common enough to have a rails plugin just magically implementing whats needed but I didn't find much at all.

Any suggestions or pointers?

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

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

发布评论

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

评论(2

萌面超妹 2024-09-15 22:53:12

如果我理解正确的话,您想要的通常是在客户端中从 Web 服务器接收一些信息,而客户端不知道该信息何时传入,即将信息从 Web 服务器推送到客户端。有几种方法可以做到这一点,但它们都有一些缺点:

  • HTTP 推送 - 需要保持活动连接,并且服务器以分块方式持续发送信息,每次都宣布下一个块传入,直到准备好才发送。通常,这个“查出的流”要么在 XMLHttpRequest 对象中接收,要么在隐藏的 iframe 中接收,尽管如果需要的话可以将其按原样显示给用户(如您的情况)。
  • 轮询 - 客户端只是询问服务器是否有定期传入的内容。令人望而生畏,具有巨大的消息传递延迟,而且是一个流量大户,但几乎总是有效。
  • 长轮询 - 轮询和 HTTP 推送的组合 - 即轮询后的第一个答案被延迟,直到有东西要回答为止。
  • 服务器发送事件 (SSE) - 一个几乎被接受的标准,在 Opera 中实施了很长时间,现在许多浏览器都支持它,并且它的目标是成为 W3C 标准。
  • WebSockets 也是 Google 新提出的标准,允许通过 Javascript 进行更复杂的 TCP 连接和发送/接收功能。它还可以用于 HTTP 推送。
  • 使用非 HTML 方法(即 Flash、Java、Silverlight)获取传入内容。有一些库/现成的模块化 SWF/小程序(例如,BlazeDS )可用于此目的,例如,它可以从给定连接传输信息以在目标页面上以 JSON 形式执行。

所有这些方法确实都包含在总括术语“HTTP 推送”和“comet”中。有大量文档、教程和现有解决方案。例如,对于 RoR,您可以尝试 Juggernautshooting_star,或者直接选择 简约解决方案

最后,我想推荐 Gregor Roth 撰写的一篇关于 SSE(第 1 部分)WebSockets(第 2 部分) 提供了详细的解释、示例和使用前景。

If I understand you correctly, what you want is generally receiving some information from web server in client while client doesn't know when exactly that information is incoming, i.e. pushing information from web server to client. There are a few ways to do that, all of them have some downsides:

  • HTTP push - requires a keep-alive connection and server keeping sending information in chunked manner, announcing next chunk incoming every time and not sending it until ready. Usually this "chucked stream" is either received in XMLHttpRequest object or a hidden iframe, although it's possible to just display it to user as is if it's desirable (as in your case).
  • Polling - client just asks server if it's something incoming regularly. Daunting, has huge messaging latencies and it's a traffic hog, but works almost always.
  • Long polling - a combination of polling and HTTP push - i.e. first answer after poll gets delayed till it will be something to answer.
  • Server-sent events (SSE) - a nearly-accepted standard, implemented in Opera for ages, now many browsers support it and it's aiming to become a W3C standard.
  • WebSockets is also a newly proposed standard by Google, allowing more complex TCP connections with send/receive functionality from Javascript. It also can be used for HTTP push.
  • Using non-HTML methods (i.e. Flash, Java, Silverlight) to get incoming content. There are a few libraries / readymade modular SWFs/applets (for example, BlazeDS) available for this purpose, which can tunnel information from a given connection to execute as JSON on target page, for example.

All these methods are indeed covered by umbrella terms "HTTP push" and "comet". There's plenty of documentation, tutorials and existing solutions flying around. For example, for RoR, you can try Juggernaut or shooting_star, or just opt for minimalistic solutions.

Finally, I'd like to recommend an excellent article by Gregor Roth on SSE (part 1) and WebSockets (part 2) that gives detailed explanations, examples and prospects for usage.

忘东忘西忘不掉你 2024-09-15 22:53:12

听起来,您只需要在页面加载并且人们正在观看时执行 ping 操作。如果是这样的话,我认为你可以避免后端进程。

我认为对控制器操作的 ajax 调用会执行 ping 操作,然后输出响应。您可以通过页面上的 javascript 控制频率、启动、停止,并使用响应更新特定的 div 或其他页面对象。

这个例子使用 ruby​​ ping 库,它只返回 true。如果您需要更多功能,可以使用其他库(例如 net-ping )。

在你的控制器中

require 'ping'

def ping
  if Ping.ping_echo(params[:hostname], params[:timeout])
    render :text => "Oh goodie, it pinged successfully"
  else
    render :text => "No go on the pingage"
  end
end

然后在你的javascript中(我使用的是jQuery,但你可以使用prototype/scriptaculous或你最喜欢的JS风格):

function ping_host {
  $.get("/controller/ping", function(data){
    $("#some_div_id").append(data);
  });
}

从那里你可以使用setTimeout命令每5秒运行一次,或者你想要生成的频率平。

如果您需要始终进行 ping,您可能需要查看一些后端作业处理器,例如 resqueue 这将使用 ping 结果更新数据库表,或者使用与上面页面类似的方法轮询 memcached 存储。

From what it sounds, you only need the ping when the page is loaded and people are watching it. If that is the case, I think you can avoid a backend process.

I would think that an ajax call to a controller action that pings and then outputs the response. You could control the frequency, start, stop through javascript on the page and update a specific div or other page object with the response.

this example uses ruby ping library which only returns true. If you need more functionality there are other libraries available (e.g. net-ping).

In your controller

require 'ping'

def ping
  if Ping.ping_echo(params[:hostname], params[:timeout])
    render :text => "Oh goodie, it pinged successfully"
  else
    render :text => "No go on the pingage"
  end
end

And then in your javascript (I am using jQuery, but you could use prototype/scriptaculous or you favorite JS flavor):

function ping_host {
  $.get("/controller/ping", function(data){
    $("#some_div_id").append(data);
  });
}

From there you can use a setTimeout command to run it every 5 seconds or however often you would like to generate the ping.

If you need the ping going on all the time, you might want to look at some backend job processors like resqueue that would update a database table with the ping results, or a memcached store that you then poll using a similar method as above from the page.

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