home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2004 April
/
PCWorld_2004-04_cd.bin
/
software
/
temacd
/
remotany
/
RemotelyAnywhere.msi
/
Ping.sma
< prev
next >
Wrap
Text File
|
2003-08-19
|
6KB
|
193 lines
////////////////////////////////////////////////////////////////////////////////
// //
// RemotelyAnywhere Sample 'SMALL' Scripts //
// //
// RemotelyAnywhere provides a mechanism for scripting via a built-in //
// language. The language is called Small, and more information about //
// it can be found at http://compuphase.com/small.htm. //
// //
// Before you use these sample scripts, you should tailor their //
// behavior to better suit your configuration and your needs. //
// //
// THIS SOFTWARE IS PROVIDED BY 3AM LABS LTD ``AS IS'' AND //
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE //
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE //
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE //
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL //
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS //
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) //
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT //
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY //
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF //
// SUCH DAMAGE. //
// //
////////////////////////////////////////////////////////////////////////////////
//
// Ping.sma
//
// Sample script to execute the built-in ping command and parse its output
// Returns values:
// 0 - at least one of the last MAXFAULT pings' round trip time was
// smaller than MAXPING
// >0 - the last MAXFAULT pings were unsuccessful (lost or gt. MAXPING)
// the actual return value is the maximum round trip time
// (or LOSTPACKET, if there were lost packets)
// <0 - an error has occurred
//
#include <ra>
// maximum number of consecutively failed packets
#define MAXFAULT 5
// maximum round trip time (in milliseconds)
#define MAXPING 500
// round trip time to indicate a lost packet (must be greater than MAXPING)
#define LOSTPACKET 1000000
// file name to store status
new STATFILE[] = "%Temp%\\ra_ping.tmp"
// log round-trip time of every ping attempt?
new log = true;
// log file base name (date and .csv will be appended, for example: C:\Ping2003-08-15.csv
// every line in the log will be formatted like:
// 2003-08-15 21:37:22, 123
// which means that the round-trip time of the ping request at 2003-08-15 21:37:22 were 123 ms
// if the request has timed out, a value of -1 will be written
new LOGFILE[] = "%SystemDrive%\\Ping"
public main()
{
new html;
new output[1024], num[8];
new start, end, lost, trip;
new stats[MAXFAULT], i;
new f;
new fail, maxping;
// call ping with 10 seconds timeout
// replace localhost with the name or IP address of the computer to ping
if (!raExecuteCmd("ping -n 1 localhost", output, 1024, 10000))
return -1;
html = htmlBeginOutput();
if (html) {
htmlWrite("Ping output:<br></center><pre>");
htmlWrite(output);
htmlWrite("</pre><center><br>");
}
// find "Packets: ..." line
start = strstr(output, "Packets:");
if (start == -1) return -2;
// extract and convert num of lost packets
start = strstr(output, "Lost = ", start);
if (start == -1) return -3;
start += 7; // length of "Lost = "
end = strstr(output, " (", start);
if (end == -1) return -4;
strmid(num, output, start, end - start);
lost = atoi(num);
if (html) {
htmlWrite("lost = ");
htmlWrite(num);
htmlBR();
}
if (lost) {
// the packet has been lost
trip = LOSTPACKET;
} else {
// extract and convert round trip time
start = strstr(output, "time=");
if (start == -1) {
start = strstr(output, "time<");
if (start == -1) return -5;
}
start += 5; // length of "time="
end = strstr(output, "ms", start);
if (end == -1) return -6;
strmid(num, output, start, end - start);
trip = atoi(num);
if (html) {
htmlWrite("time = ");
htmlWrite(num);
htmlWrite("ms<br>");
}
}
// load status file (does not exist: first run or failure)
for (i = 0; i < MAXFAULT; i++)
stats[i] = 0;
f = fopen(STATFILE, FILE_READ);
if (f != 0) {
fread(f, stats, MAXFAULT * 4);
fclose(f);
}
// step values
for (i = 1; i < MAXFAULT; i++)
stats[i - 1] = stats[i];
stats[MAXFAULT - 1] = trip;
// write status file
f = fopen(STATFILE, FILE_WRITE);
if (f == 0) return -10;
fwrite(f, stats, MAXFAULT * 4);
fclose(f);
// determine return value
fail = 1;
maxping = 0;
for (i = 0; i < MAXFAULT; i++) {
if (stats[i] < MAXPING) {
fail = 0;
break;
}
if (maxping < stats[i])
maxping = stats[i];
}
if (html) htmlEndOutput();
// write log
if (log) {
new name[256];
new msg[256];
new msglen;
// format file name
new t = raGetTime();
sprintf(name, "%s%Y.csv", LOGFILE, t);
f = fopen(name, FILE_APPEND);
if (f != 0) {
// format and write line
msglen = sprintf(msg, "%Y %t, %d\r\n", t, t, trip == LOSTPACKET ? -1 : trip);
strize(msg);
fwrite(f, msg, msglen);
fclose(f);
}
}
return (fail ? maxping : 0);
}