Ping error return always equals 0

梦想与她 提交于 2020-01-04 05:18:05

问题


Having trouble getting this code to work. I can run the script (with changes) in vbscript and it runs fine. But I cant get this version to return a ping error return value other than 0. Any help is appreciated.

This is scanning a list of remote machines with checkboxes and returning the checked values. I want to run the ping to verify the remote machine are there before continuing. But with a error return of 0 for all queries its useless.

function statuschk3(){
var checkedValue = null; 
var inputElements = document.getElementsByName("comp");
for(var i=0; inputElements[i]; ++i){
  if(inputElements[i].checked)
  {checkedValue = inputElements[i].value;
var WshShell = new ActiveXObject("WScript.Shell");
var status = WshShell.run ("ping -n 1 -a"  + checkedValue + ",0 ,true");
    if(status == 0)
    {   var fso = new ActiveXObject("Scripting.FileSystemObject");
        var s = fso.OpenTextFile("C:/script/testfile1.txt", 8, true);
        s.WriteLine(checkedValue + " is turned off or off the domain");
        s.Close();
    } 
    else` 

回答1:


Here's a function I use to test connectivity. I use a vbscript version of this, but I rewrote it in javascript.

function Reachable(strComputer) 
{
    var wmiQuery = "Select * From Win32_PingStatus Where Address = '" + strComputer + "'";
    var objWMIService = GetObject("winmgmts://./root/cimv2");
    var colItems = objWMIService.ExecQuery(wmiQuery);
    var enumItems = new Enumerator(colItems)
    for (; !enumItems.atEnd(); enumItems.moveNext())
    {
        var objStatus = enumItems.item();
        if ((objStatus.StatusCode == null) || (objStatus.Statuscode != 0))
        {
            return false //if computer is unreachable, return false
        }
        else
        {
            return true //'if computer is reachable, return true
        }
    }
}

Usage:

vbscript:
If Reachable(strComputer) Then MsgBox "Online" Else MsgBox "Offline"

javascript:
if (Reachable(strComputer)) { alert("Online") } else { alert("Offline") }

edit:

If you'd like to adjust the timeout of this, you can add the following to this line:

var wmiQuery = "Select * From Win32_PingStatus Where Address = '" + strComputer + "' and Timeout=500";

Where 500 is 500 milliseconds.

Here's more on the Win32_PingStatus class, where it says the default Timeout is 1000 milliseconds.

another edit to address your original question:

It looks like you have some syntax issues with your original code:

var status = WshShell.run ("ping -n 1 -a"  + checkedValue + ",0 ,true");

needs to be

var status = WshShell.run ("ping -n 1 -a "  + checkedValue, 0,true);

Notice the location of the space after a and the quotation marks after checkedValue

Also, the logic is backwards. if(status==0) then the device is ONLINE.




回答2:


For what it's worth, if langstrom's WMI solution works, I think I prefer it over my own. My purpose in answering is to help you figure out what went wrong with your script.

Firstly, ping.exe can receive error responses from the gateway saying "no route to host" or similar. But having received responses (erroneous or not), the exit status is 0. It's better to check the output of the command for valid responses to determine success / fail.

But the more immediate problem is that shellObj.run() is forking the ping process asynchronously. It's non-blocking.

Using shellObj.exec() solves both problems. Here's an example using console JScript:

// returns 0 for success, non-zero for fail
function ping(host) {

    var osh = WSH.CreateObject('Wscript.Shell'),
        proc = osh.exec('ping -n 1 ' + host);

    while (!proc.Status) WSH.Sleep(25);
    return !/\btime\b/i.test(proc.StdOut.ReadAll());
}

Unfortunately, since browser / HTA JScript doesn't understand WSH or Sleep, you have to get creative with setInterval to block until the ping executable is finished. (This is why langstrom's WMI solution would be preferable to spawning an executable. Trying to hack a non-blocking command into something that blocks is cumbersome and awkward with HTA JScript.)

// alerts true on success, false on fail
function ping(host) {

    var osh = new ActiveXObject('Wscript.Shell'),
        proc = osh.exec('ping -n 1 ' + host),
        waitForExit = setInterval(function() {
            if (proc.Status) {
                clearInterval(waitForExit);
                alert(/\btime\b/i.test(proc.StdOut.ReadAll()));
            }
        }, 25);
}

ping('localhost');


来源:https://stackoverflow.com/questions/37578181/ping-error-return-always-equals-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!