Script to check internet conectivity

Your script gives me the ping file in c: and return value of rv:-1073741510

Would settle for reading from text file if can not have the cmd window open every time run…

function exec() {
  var p = 'C:\\Windows\\System32\\';
  var f = 'cmd.exe';
  var args = 'ping 8.8.8.8';
  var wdir = 'C:\\';
  var returnVal = file.Starter(p+f).With(args).WorkOn(wdir).ShellExecute();
  return 'rv:'+returnVal;
}

without the write to file the same response is returned… not sure where -1073741510 comes from though…

I am not getting the Pinglog.txt file created anywhere, and it executes and closes so fast, I cannot see what is going on. My return is 1.

If I don’t use /C ping, just a cmd window opens, no ping happens, and return is -1073741510.

hmmmm, I now get;

blank cmd window for couple of secs then closes
then get response 0
test file;

Pinging 8.8.8.8 with 32 bytes of data:
Reply from 8.8.8.8: bytes=32 time=31ms TTL=50
Reply from 8.8.8.8: bytes=32 time=30ms TTL=50
Reply from 8.8.8.8: bytes=32 time=30ms TTL=50
Reply from 8.8.8.8: bytes=32 time=31ms TTL=50

Ping statistics for 8.8.8.8:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 30ms, Maximum = 31ms, Average = 30ms

Delete the text file. See if it really gets created. It could be from previous testing.

I am not getting anything other than quick flash of cmd window. Might be permissions, though I am Admin. Maybe Win 10 has to do with it?

Yep, same again.

Deleted txt and empties recycle bin.

function exec2() {
  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).ShellExecute();
  return 'rv:'+returnVal;
}

Gives the same;

function exec() {
  var p = 'C:\\Windows\\System32\\';
  var f = 'cmd.exe';
  var args = 'ping 8.8.8.8';
  var wdir = 'C:\\';
  var returnVal = file.Starter(p+f).With(args).WorkOn(wdir).ShellExecute();
  return 'rv:'+returnVal;
}

Gives me cmd window with C:> normal enty when opening whihc doesnt close and then when closing get the -1073741510 response value

In theory your;

function exec2() {
  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).ShellExecute();
  return 'rv:'+returnVal;
}

Would work if I pull from the test file… although would need to delay the reading for ping to execute…

function exec2() {
  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();
}

Runs in background and creates the text file… Hidden works like you said issue was cmd wand closing.

function exec2() {
  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')
return listLine[8];
}

seems to do the job, well as far as getting a packet success/loss etc.

1 Like
function exec2() {
  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 response	= listLine[8].substr(47,58-listLine[8].length);
  return response;
}

Returns the package % loss number.

Perticulatly proud of this line; :smile:
var response = listLine[8].substr(47,58-listLine[8].length);

1 Like

Try this out, with Powershell cmdlet …

Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. All rights reserved.

PS C:\Users\Quentin> Test-Connection -computer 8.8.8.8  -count 4

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
------        -----------     -----------      -----------                              -----    --------
ASUSBOOK      8.8.8.8         8.8.8.8                                                   32       357
ASUSBOOK      8.8.8.8         8.8.8.8                                                   32       587
ASUSBOOK      8.8.8.8         8.8.8.8                                                   32       88
ASUSBOOK      8.8.8.8         8.8.8.8                                                   32       251


PS C:\Users\Quentin> return Test-Connection -computer 8.8.8.8  -count 4 -quiet
True
PS C:\Users\Quentin>

Using the -quiet parameter/switch makes the cmdlet return True or False.

1 Like

Nice :smile:

At the minute have;

function exec2() {
	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+'"';
}

function exec3() {
	var p 			= 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\';
	var f 			= 'powershell.exe';
	var args 		= 'return Test-Connection -computer 8.8.8.8  -count 4 -quiet';
	var wdir 		= 'C:\\';
	var response 	= file.Starter(p+f).With(args).WorkOn(wdir).ShellExecute();
	return response;
}

Gives a response of 0 again :frowning:

This creates a file called pl.txt and it contains the word True

Test-Connection -computer 8.8.8.8  -count 4 -quiet > pl.txt

I still cannot get the cmd/ping method to create a file.

1 Like

LOL strange…

function exec2() {
	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+'"';
}

Works fine for me :smile: .txt file and everything HEHE :stuck_out_tongue:

Not working for me. Cannot figure out why. TXT file is not being created.

LOL, I don’t understand that error message at all.

Ok, figured it out. This is a permissions issue on ‘C:’.

If I set the workingDir and pinglog.txt file to be created in a different location, everything works fine…

function internetCheck() {
  var wdir          = 'C:\\Users\\Quentin\\'; // need write/modify permision to this path
  var logfile       = 'pinglog.txt';
  var p             = 'C:\\Windows\\System32\\';
  var f             = 'cmd.exe';
  var args          = '/c ping 8.8.8.8 > ' + wdir + logfile;
  var returnVal     = file.Starter(p+f).With(args).WorkOn(wdir).Hidden.ShellExecute();
  var listFile      = file.ReadFromFile(wdir+logfile);
  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+'"';
}

You should mention this caveat in your Tutorial, and maybe parameterize with variables as shown above.

Powershell version that simply returns True or False…

function internetCheckPS() {
  var wdir = 'C:\\Users\\Quentin\\'; // need write/modify permision to this path
  var logfile = 'pinglog.txt'; // resuts are logged to this file
  var address = '8.8.8.8'; // name or IP of computer to ping
  var attempts = 4 // number of times to ping the computer
  
  var p = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\';
  var f = 'powershell.exe';
  var args = 'Test-Connection -computer '+address+' -count '+attempts+' -quiet > ' + wdir+logfile;
  var returnVal = file.Starter(p+f).With(args).WorkOn(wdir).Hidden.ShellExecute();
  var listFile      = file.ReadFromFile(wdir+logfile);
  var listLine      = listFile.split('\n')
  var pingResult= listLine[0];
  return 'Internet Up: '+pingResult;
}

1 Like