tags:

views:

27

answers:

1

Starting from Ameer Dawood ideea of ping from JS ( pinger ), I've got a problem when the remote IP is a http server, and request HTTP authentification, the browser pop's up the box for user/password. How can I avoid this, If the remote server request auth, we know that the remote server is responding and I need to call alive() function ( doesn't work with http://someBogusUser:someBogusPass@ + ip, beacause it request the password again, I do not want to have the corect password saved on a client side script :) )
The main part of the code is this:

img = new Image();
img.onload = function() { alive()();};
img.onerror = function() { unreachable()();};

img.src = "http://" + ip;
setTimeout(function() { unreachable();}, 1500);

Curently I'm trying to test if I can use a xmlhttprequest readyState and status to see if the remote IP is responding.
Anyone has an ideea on how to do this, or has knowledge of another method to test if a IP is alive from JS. Please don't give me server side solutions (I'm using them now + AJAX).

Thank you

EDIT: I think Web workers will stop any authentication popup from reaching the browser, but they can't handle DOM events (onload, onerror) so workers can't help ... :(

+1  A: 

I don't think you can avoid it.

This hack is not ‘ping’ in any normal sense of the word; it won't “check an IP is alive”. It's an HTTP request and subject to authentication, redirection and other facets of HTTP. (And of course it won't ‘ping’ a machine that's not running an HTTP server.) Any method of remote inclusion (img, script, style, iframe) risks popping up authentication.

With XMLHttpRequest you will have problems because of the Same Origin Policy. IE and Opera will immediately fail any attempt with a security error. Firefox and WebKit will try to make the connection though you won't get anything usable as a response so you'll have to continue to guess from the timeout length. Which itself seems pretty problematic to me. Certainly it's not out of bounds to expect the / page of any given website to take more than 1.5s to return a complete document.

In short I don't think ‘pinger’ is viable as anything other than a fun experiment. For any real work you will have to continue with the server-side ping.

bobince
When I try to load an image from a managed switch that has no HTTP, when the switch is pluged in the network it fires alive(), when I unplug the switch it fires unreachable(). I didn't belive it either, but after a few test it seems that it works, and now I'm trying to understand why it works and how can it be used ( as a fun experiment as you said ). You should give it a try. see the link in Question.
Radu
That will depend on how the device responds to connections on unconnected ports. If the port is ‘closed’ you will typically get a failure quite quickly. If there is no response at all (‘stealthed’ ports) the response will be slow. If you know exactly what the machines are you're testing, and how they will responds to probes on port 80, it might be useful for something. But not in the general case.
bobince
In a way Ameer Dawood ideea of a ping emulator is like Diego Perini DOMContentLoaded emulation for IE browsers http://javascript.nwbox.com/IEContentLoaded/ (and as far as i know jQuery still use his solution for IE). It can be a "ping" emulation that works in some cases, and for me, if I know exactly how the remote IP behaves, maybe I can use it, and I know exactly what IP's have HTTP, and for them I can fall back to an AJAX call, and use JS "ping" emulator for the others.
Radu