jscript - Ping error return always equals 0 -
having trouble getting code work. can run script (with changes) in vbscript , runs fine. cant version return ping error return value other 0. appreciated.
this scanning list of remote machines checkboxes , returning checked values. want run ping verify remote machine there before continuing. error return of 0 queries 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 + " turned off or off domain"); s.close(); } else`
here's function use test connectivity. use vbscript version of this, rewrote in javascript.
function reachable(strcomputer) { var wmiquery = "select * win32_pingstatus address = '" + strcomputer + "'"; var objwmiservice = getobject("winmgmts://./root/cimv2"); var colitems = objwmiservice.execquery(wmiquery); var enumitems = new enumerator(colitems) (; !enumitems.atend(); enumitems.movenext()) { var objstatus = enumitems.item(); if ((objstatus.statuscode == null) || (objstatus.statuscode != 0)) { return false //if computer unreachable, return false } else { return true //'if computer reachable, return true } } }
usage:
vbscript:
if reachable(strcomputer) msgbox "online" else msgbox "offline"
javascript:
if (reachable(strcomputer)) { alert("online") } else { alert("offline") }
edit:
if you'd adjust timeout of this, can add following line:
var wmiquery = "select * win32_pingstatus address = '" + strcomputer + "' , timeout=500";
where 500 500 milliseconds.
here's more on win32_pingstatus class, says default timeout 1000 milliseconds.
another edit address original question:
it looks have syntax issues original code:
var status = wshshell.run ("ping -n 1 -a" + checkedvalue + ",0 ,true");
needs be
var status = wshshell.run ("ping -n 1 -a " + checkedvalue, 0,true);
notice location of space after a
, quotation marks after checkedvalue
also, logic backwards. if(status==0)
device online.