#214142 - 2022-06-26 03:25 PM
How to return IP address of pinged computer
|
Robdutoit
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:
;=================================================================
; 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:
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:
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
|
|
|
|
#214144 - 2022-06-28 11:22 AM
Re: How to return IP address of pinged computer
[Re: Allen]
|
Robdutoit
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.
;=================================================================
; 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
|
|
|
|
#214146 - 2022-06-28 08:55 PM
Re: How to return IP address of pinged computer
[Re: ShaneEP]
|
ShaneEP
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...
;=================================================================
; 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
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.
$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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 686 anonymous users online.
|
|
|