Page 1 of 1 1
Topic Options
#214142 - 2022-06-26 03:25 PM How to return IP address of pinged computer
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
I have a script that pings computers within a certain OU in Active Directory. Any computer that responds to ping is then shutdown remotely with a 5 minute timer. Works Perfectly.

My shutdown script:

 Code:
;=================================================================
;  Shutdown Computers
;=================================================================

Dim $objAdsPath, $obj, $filter[0]
$filter[0] = "computer"
$objADsPath = GetObject("LDAP://OU=Accounting,OU=Computers,DC=%Userdomain%,DC=internal")

If @Error = 0

	$objAdsPath.filter = $filter
	For Each $obj In $objAdsPath
		$PcName = SUBSTR($obj.name, 4)
		Shell '"'+%COMSPEC%+'" /c ping -n 1 ' + $PcName + ' |find /C "TTL=" >nul'		
		$OSPing = NOT @ERROR	
		If $OSPing = 1
			$ShellCMD = 'shutdown -s -f -m ' + $PcName + ' -t 300'
			Shell $ShellCMD
			? "shutting down " + $PcName
		Else
			? "computer not online"
		Endif
	Next

Else
	? "Not able to connect to LDAP path."
Endif


However, I don't want any computers that are connected via the VPN to be shutdown. So I want to be able to exclude any computers on IP Address range 192.168.10.

I have managed to find an example of how to output the IP Address of a ping command to a variable, but cannot get this to work in my script. Posted below is the example I found on the Internet. However I need to replace http://www.google.de with + $PcName + as well as convert the code below to kixtart.

What the script should be doing is looking inside [] of the ping reply for the IP Address and outputting it to the variable $IP.

Example Script found on the Internet:

 Code:
for /f "tokens=2 delims=[]" %%a in ('ping -4 -n 1 www.google.de ^|find "["') do set "IP=%%a"
echo %IP%


My kixtart conversion from batch file:

 Code:
For Each
Shell '"'+%COMSPEC%+'" /c ping -n 1 ' + $PcName + ' |find /C  "[=" $IP'
? $IP
Next


My code gives error expected variable! Obviously has to do with "[=" not being correct for Kix to read what is in the bracket [] of the ping reply.

Top
#214143 - 2022-06-27 06:13 AM Re: How to return IP address of pinged computer [Re: Robdutoit]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
I'd start with the For Each line missing the rest of the vars...

For each $item in $array
...

Next

Top
#214144 - 2022-06-28 11:22 AM Re: How to return IP address of pinged computer [Re: Allen]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
I have now put the coding into my script. It no longer complains about a missing variable. But the output is not the output of the IP Address. I believe this is because I need the "delims=" and %%A, which I cannot get to work in Kixtart as it doesn't understand the command.

I think I will have to find another way to achieve this aim as the documentation on Ping and |find is pretty much non existent.

 Code:

;=================================================================
;  Shutdown Computers
;=================================================================

Dim $objAdsPath, $obj, $filter[0]
$filter[0] = "computer"
$objADsPath = GetObject("LDAP://OU=Accounting,OU=Computers,DC=%Userdomain%,DC=internal")

If @Error = 0

	$objAdsPath.filter = $filter
	For Each $obj In $objAdsPath
		$PcName = SUBSTR($obj.name, 4)
		? $PcName
;		Shell '"'+%COMSPEC%+'" /c ping -n 1 ' + $PcName + ' |find /C "TTL=" >nul'
		Shell '"'+%COMSPEC%+'" /c ping -n 1 ' + $PcName + ' |find /C  "[=" $IP'
		? $IP
		$OSPing = NOT @ERROR	
		? $OSPing
		If $OSPing = 1
			$ShellCMD = 'shutdown -s -f -m ' + $PcName + ' -t 300'
			Shell $ShellCMD
			? "shutting down " + $PcName
		Else
			? "computer not online"
		Endif
	Next

Else
	? "Not able to connect to LDAP path."
Endif


Top
#214145 - 2022-06-28 05:24 PM Re: How to return IP address of pinged computer [Re: Robdutoit]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
I think the problem is getting the output back from comspec into kix. There used to be a function called WshPipe, but I couldn't get it to work on my work laptop (may just be locked down).

Might be easier to redirect the output of the comspec portion to a temporary text file, then read the info from the file using kix. Then can delete the file.

Top
#214146 - 2022-06-28 08:55 PM Re: How to return IP address of pinged computer [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
When searching the forum, I happened upon this UDF. Pretty much does what you're wanting to do, using a temp file read.

http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=208164

So I would think your code could become something like...

 Code:
;=================================================================
;  Shutdown Computers
;=================================================================
Dim $objAdsPath, $obj, $filter[0]
$filter[0] = "computer"
$objADsPath = GetObject("LDAP://OU=Accounting,OU=Computers,DC=%Userdomain%,DC=internal")

If @Error = 0

	$objAdsPath.filter = $filter
	For Each $obj In $objAdsPath
		$PcName = SUBSTR($obj.name, 4)
		? $PcName

;		Shell '"'+%COMSPEC%+'" /c ping -n 1 ' + $PcName + ' |find /C "TTL=" >nul'
		$OSPing = Ping($PcName, 0, 1)
		? $OSPing 

		$IP = Ping($PcName, 1, 1)
		? $IP

		If $OSPing = 1 AND Not InStr($IP, "192.168.10")
			$ShellCMD = 'shutdown -s -f -m ' + $PcName + ' -t 300'
			Shell $ShellCMD
			? "shutting down " + $PcName
		Else
			? "computer not online, or is VPN"
		Endif
	Next

Else
	? "Not able to connect to LDAP path."
Endif

;;; http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Board=7&Number=208164 ;;;

function Ping($Computer,$GetIP,optional $LoopCount,optional $TimeOut)
	if $GetIP
		dim $ip, $ipfile, $
		$ipfile = @scriptdir + '\ip.txt'
		shell '%Comspec% /q /e:1024 /c for /F "tokens=2 delims=[]" %%i IN ('+ chr(39)
		+ '"ping ' + $Computer + ' -n 1 | find "]""' + chr(39) + ') do echo %%i >"' + $ipfile + '"'
		$ = open(10,$ipfile,2) $ip = readline(10) $ = close(10) del $ipfile
		if $ip
			$Ping = $ip
		else
			$Ping = 'Bad IP address ' + $Computer + '!'
		endif
		exit 0
	else
		if $TimeOut
			for $c = 0 to $LoopCount
				shell '%Comspec% /C ping ' + $Computer + ' -n 1 -w ' + $TimeOut + ' | find /C "TTL=" > nul'
				if @error = 0
					$Ping = 1
					exit 0
				endif
			next
		else
			for $c = 0 to $LoopCount
				shell '%Comspec% /C ping ' + $Computer + ' | find /C "TTL=" > nul'
				if @error = 0
					$Ping = 1
					exit 0
				endif
			next
		endif
		$Ping = 0
	endif
endfunction

Top
#214147 - 2022-06-28 09:05 PM Re: How to return IP address of pinged computer [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
I did find ways to use comspec, and wscript to get the IP back. But couldn't find any way to do it without flashing a black cmd window since they rely on reading the screen output to get the result. The Ping() that echos to a file instead, avoids this.

Just as an example....This code does get the IP but you'll notice the flash.

 Code:
$computer = @WkSta

$output = CreateObject("WScript.Shell").Exec("ping -4 -n 1 " + $computer).StdOut.ReadAll
$IP = Split(Split($output, "[")[1], "]")[0]

$nul = MessageBox($IP, "", 0,)

Top
#214148 - 2022-06-28 11:00 PM Re: How to return IP address of pinged computer [Re: ShaneEP]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
You sir, are a genius. I could have sworn that I had already looked at the Ping function - but probably when I wasn't looking for the tokens and delims coding.

I have tested it and the coding works without any errors and wants to try and shut down any computers online and on the local network. I remmed out the shutdown part temporarily while testing. As bad luck would have it, nobody is using the VPN at the moment - as it's nearly 10PM - that is not surprising. So I can't tell yet what the script will do when it finds a pc online but using the VPN range. But it should work.

I have made one change to the Ping Function. I have added the -n 1 so it only pings once in the last section of coding otherwise the script takes about 25 seconds per machine to complete the loop if the computer is not online.

I wonder if that function should be updated as the other two sections of that function have the -n 1 so I suspect it's a typo that it got missed out in the third section. It does make a huge difference to the run time of the script if it only pings 1 instead of 4 times.

I am happy with the Ping script because this particular script will be run as a scheduled task and nobody will be logged onto the server. I think that I may have wscript disabled anyway.

Very pleased with this. I spent hours trying to get the Ping to output the IP Address. Astonishing how difficult this was to do. I actually did look at the Ping function earlier a day or two ago, but I was obviously looking at a different aspect of the function as I don't recall seeing the tokens and delim coding when I looked at it. Ironic as this was where I was having problems.

Thank you for helping me get there.

Top
#214149 - 2022-06-28 11:06 PM (NA) Re: How to return IP address of pinged computer [Re: Robdutoit]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Glad it worked. Wish there was an easier way.

And you're probably right about that typo. It uses the $loopcount parameter, but without that -n 1 in there it's doing 4 tries per loop instead of just the one. Good catch.

Top
#214150 - 2022-06-28 11:36 PM (NA) Re: How to return IP address of pinged computer [Re: ShaneEP]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
Wasn't really hard to catch it. Sat there waiting 25 seconds for each PC to be pinged while I was running the test. You notice the lag very, very quickly!

Ya, it is a bit complicated. But hey it works. it doesn't matter that it has to create a temporary file to read the IP value. As long as it works, it can make gin and tonic for all I care lol.

Top
#214151 - 2022-07-05 09:52 AM Re: (NA) Re: How to return IP address of pinged computer [Re: Robdutoit]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Stupid question maybe but why not use the Ping() UDF? Works just file for this. Ping() does all the heavy lifting for you. I've been using it for many years now and never failed on me.

Ping() UDF
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#214153 - 2022-07-06 11:55 AM Re: (NA) Re: How to return IP address of pinged computer [Re: Mart]
Robdutoit Offline
Hey THIS is FUN
***

Registered: 2012-03-27
Posts: 363
Loc: London, England
That is the UDF that we are using. We have included the Ping UDF into the script and just added in the commands to shut the computers based on the ping reply results.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 84 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.059 seconds in which 0.023 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org