Flashing command window

B

bdy

Hello, I'm currently running


Code:
var config; //associative array of arrays
var last=new Array(); //stores the last status message for each URL.
var configfile="";
var fs=WScript.CreateObject("Scripting.FileSystemObject");

var NoError="Connected successfully";

var mediums={ //output mediums.
"email":email,
"netsend":netsend,
"run":run,
"runquiet":runquiet,
"console":console,
"log":log
};

var wsh; //WScript.Shell is disabled on some computers, and we won't
always need it.
try {
wsh=WScript.CreateObject("WScript.Shell");
} catch(e) {
WScript.echo("Could not create shell object. Will not be able
to run programs or send netbios messages.");
wsh=0;
}




function add(n,v) {
var a=config[n];
if(!a) a=config[n]=new Array();
a[a.length]=v;
}

function get(n) { //for getting single values, returns last in list
var c=config[n];
if(c) return c[c.length-1];
else return 0;
}

function trim(s) {
return s.replace(/^\s*|\s*$/g,"");
}

function getFile(file) {
var f=fs.OpenTextFile(file,1,false);
var s=f.readAll();
return s;
}

function loadConfig(file) {
config=new Array();
add("delay",5000); //default delay
add("from","[email protected]"); //some smtp servers would block this

var s=getFile(file);

//The following line removes C style /* */ comments. Not fully
tested
s=s.replace(/\/\*([^\*]|\*[^\/])*\*\//g,"");

s=s.split("\n"); //split into lines
for(var i=0;i<s.length;i++) {
var c=trim(s[i]);
if(c.length>0) {
var k=c.indexOf(" ");
var name,value;
if(k>0) {
name=trim(c.slice(0,k)).toLowerCase();
value=trim(c.slice(k+1));
} else {
name=c;
value=true;
}

add(name,value);
}
}
}

function pingSite(url) {
var x;
try {
x=WScript.CreateObject("Microsoft.XMLHTTP");
} catch(e) {
try {
x=WScript.CreateObject("MSXML2.XMLHTTP");
} catch(e2) {
WScript.echo("Could not instantiate XMLHTTP.
Make sure it's installed.");
}
}
try {
x.open("HEAD",url,false);
x.send();
} catch(e3) {
return "Could not connect";
}
if(x.status>=400) {
return x.status+" "+x.statusText;
}
return NoError;
}

// Uses Net.exe to send message over network.
// Message should popup on screen of the receiving computer.
// Unless the messenger service has been disabled.
function netsend(addr, url, message) {
if(wsh) wsh.run("net send "+addr+" \""+message+": "+url+"\"",
0,true)
}

// Uses CDOSYS to send email. Requires Windows 2000 or XP.
function email(addr, url, message) {
try {
var smtp=get("smtp");
var prefix="http://schemas.microsoft.com/cdo/
configuration/";

var msg=WScript.CreateObject("CDO.Message");

if(smtp) {
var conn=WScript.CreateObject
("CDO.Configuration");
conn.Fields(prefix+"smtpserver")=smtp;
conn.Fields(prefix+"sendusing")=2; //
SendUsingPort.
conn.Fields(prefix+"smtpserverport")=25;
conn.Fields.Update();
msg.Configuration=conn;
}

msg.To=addr;
msg.From=get("from");
msg.Subject="Site Alert";
msg.TextBody=
"Date: "+new Date()+"\n"+
"URL: "+url+"\n"+
"Message: "+message+"\n";
msg.Send();

} catch(e) {
WScript.echo("Error sending email:\n
"+e.description);
}

}

// Runs a command. Passes url and message as %url% and %message%.
// Remember that a "Connected successfully" is sent whenever an
offline server starts responding again.
function runquiet(cmd,url,message) {
cmd=cmd.replace(/\%url%/g,url);
cmd=cmd.replace(/\%message%/g,message);
if(wsh) wsh.run(cmd,0,false);
}

// Runs a command. Passes url and message as %url% and %message%.
// Remember that a "Connected successfully" is sent whenever an
offline server starts responding again.
function run(cmd,url,message) {
cmd=cmd.replace(/\%url%/g,url);
cmd=cmd.replace(/\%message%/g,message);
if(wsh) wsh.run(cmd,1,false);
}


function log(file,url,message) {
var f=fs.OpenTextFile(file,8,true);
f.WriteLine(""+new Date()+": "+message+": "+url);
f.Close();
}

function console(blah,url,message) {
WScript.echo(""+new Date()+": "+message+": "+url);
}

// Sends the url and message to all addresses on all mediums (email,
netsend, log, etc.)
function send(url,message) {
var out;
for(out in mediums) {
var list=config[out]; //get list of addresses for that
medium
if(list) {
var f=mediums[out]; //get function to use
for(var i=0;i<list.length;i++) {
f(list[i],url,message); //send url and
message to that address on that medium
}
}
}
}


try {
configfile=WScript.Arguments(0);
} catch(e) {}

if(configfile=="") {
WScript.echo("Usage: cscript DownAlerts.js configfile.ext");
} else {
for(;;) {
//refreshes config file after every cycle.
//allows for realtime configuration without
interruption.
loadConfig(configfile);

var urls=config["url"];
for(var i=0;i<urls.length;i++) {
try {
var url=urls[i];
var message=pingSite(url);

//sends messages upon change in status
if((last[url] || message!=NoError) &&
message!=last[url]) {
send(url,message);
}
last[url]=message;
} catch(e) {
write("Uncaught exception");
}
WScript.sleep(parseInt(""+get("delay")));
}
}
}

from a command prompt, and although the window's minimized, it flahes
briefly when a bad connection is reported. I'm running it from the
command line by typing "DownAlerts.js sample.cfg" at the DOS prompt.

Any ideas why this may be happening and how I can fix it; I also tried
executing from a batch files; same results.

Is this a problem that can be resolved with Javascript, or is this a
wscript or cscript issue?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top