Tutorial: Internet connectivity check

Anyone using online services may find this useful.

Have been working on an integration with cloud based service and wanted an internet connectivity test script as part of my system to check against should the expected response not be returned to see if it was internet issue, script issue or cloud service issue.

Anyway, this script will ping 8.8.8.8 (google dns server) to check internet connectivity.
Using the response you can diagnose the api response issue within the script (handy for logs)

function internetCheck() {
	var p 			= 'C:\\Windows\\System32\\';
	var f 			= 'cmd.exe';
	var args 			= '/c ping 8.8.8.8 > C:\\Pinglog.txt';
	var wdir 			= 'C:\\';
	var returnVal 	= file.Starter(p+f).With(args).WorkOn(wdir).Hidden.ShellExecute();
	var listFile		= file.ReadFromFile("C:\\Pinglog.txt");
	var listLine		= listFile.split('\n')
	var packetLoss	= listLine[8].substr(47,58-listLine[8].length);
	if 	(packetLoss == 0)
		{
		var response = 'OK';
		} else if (packetLoss == 100) {
		var response = 'Down';
		} else {
		var response = 'Issues';
		}
	return 'Internet Conectivity "'+response+'"';
}

Will return one of the following;

  1. Internet Connectivity “OK”
  2. Internet Connectivity “Down”
  3. Internet Connectivity “Issues”
3 Likes

This is good if you want to display the status to a user.

As a suggestion, maybe it’s better to return just an integer value, or boolean (false being for cases 2 and 3), then it’s easier to call the function from another and work with the return value.

For example:

Return integer values:

function internetCheck() {
	var p 			= 'C:\\Windows\\System32\\';
	var f 			= 'cmd.exe';
	var args 			= '/c ping 8.8.8.8 > C:\\Pinglog.txt';
	var wdir 			= 'C:\\';
	var returnVal 	= file.Starter(p+f).With(args).WorkOn(wdir).Hidden.ShellExecute();
	var listFile		= file.ReadFromFile("C:\\Pinglog.txt");
	var listLine		= listFile.split('\n')
	var packetLoss	= listLine[8].substr(47,58-listLine[8].length);
	if 	(packetLoss == 0)
		{
		var response = 1;
		} else if (packetLoss == 100) {
		var response = 2;
		} else {
		var response = 3;
		}
	return response;
}

Return boolean value (true = OK, false = Down or Issues):

function internetCheck() {
	var p 			= 'C:\\Windows\\System32\\';
	var f 			= 'cmd.exe';
	var args 			= '/c ping 8.8.8.8 > C:\\Pinglog.txt';
	var wdir 			= 'C:\\';
	var returnVal 	= file.Starter(p+f).With(args).WorkOn(wdir).Hidden.ShellExecute();
	var listFile		= file.ReadFromFile("C:\\Pinglog.txt");
	var listLine		= listFile.split('\n')
	var packetLoss	= listLine[8].substr(47,58-listLine[8].length);
	if 	(packetLoss == 0)
		{
		var response = true;
		} else {
		var response = false;
		}
	return response;
}
2 Likes

I know what your saying, I used a more descript set of values so people who used would see more clearly the posible values.

Agree a yes or no value would be easier to work with.
QMcKays powershell version returned a more simple True/False which is nice.

FYI to anyone using this, you may need to turn of Windows UAC to allow the command prompt to work.
Also think an adjustment was needed in the packetLoss line, have used indexOf(’%’) to cut trailing characters

function internetCheck() {
	var p 			= 'C:\\Windows\\System32\\';
	var f 			= 'cmd.exe';
	var args 			= '/c ping 8.8.8.8 > C:\\Pinglog.txt';
	var wdir 			= 'C:\\';
	var returnVal 	= file.Starter(p+f).With(args).WorkOn(wdir).Hidden.ShellExecute();
	var listFile		= file.ReadFromFile("C:\\Pinglog.txt");
	var listLine		= listFile.split('\n')
	var packetLoss	= listLine[8].substr(47,listLine[8].indexOf('%')-47);
	if 	(packetLoss == 0)
		{
		var response = true;
		} else {
		var response = false;
		}
	return response;
}