Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show ping output dynamically in a web page?

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?

like image 440
Thomas Holmström Avatar asked Jul 09 '10 08:07

Thomas Holmström


People also ask

Can you ping from a browser?

You cannot ping from a browser. What do you actually want to achieve? Are you looking to calculate the round-trip time of a connection from the client to the server? The popular "ping" refers to basic ICMP comunication, you are not capable of doing that with pure HTML/JS.

How do I ping a website using Javascript?

Output. Open the web page in your web browser. Press 'Ctrl+Shift+I' to navigate to Browser Developer Tools. Enter the URL you wish to ping in the form input and click the 'Submit' button.


2 Answers

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.

like image 108
GreyCat Avatar answered Oct 13 '22 12:10

GreyCat


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.

like image 31
Geoff Lanotte Avatar answered Oct 13 '22 12:10

Geoff Lanotte