Ping错误返回始终等于0 [英] Ping error return always equals 0

查看:242
本文介绍了Ping错误返回始终等于0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法使此代码正常工作.我可以在vbscript中运行脚本(有更改),并且运行良好.但是我无法获得此版本以返回除0以外的ping错误返回值.感谢您的帮助.

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.

这将扫描带有复选框的远程计算机列表,并返回检查的值.我要运行ping来验证远程计算机是否存在,然后再继续.但是对于所有查询,错误返回0都是没有用的.

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` 

推荐答案

这是我用来测试连通性的功能.我使用的是vbscript版本,但是用javascript重写了它.

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
        }
    }
}

用法:

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

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

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

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

如果您要调整其超时时间,可以在此行中添加以下内容:

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";

500是500毫秒.

Where 500 is 500 milliseconds.

有关 Win32_PingStatus 类,其中默认超时为1000毫秒.

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");

需要成为

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

注意a之后的空格位置和checkedValue

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

此外,逻辑是倒退的. if(status==0),则设备处于联机状态.

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

这篇关于Ping错误返回始终等于0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆