#188290 - 2008-06-19 09:33 PM
Help with nslookup output
|
GSUK
Starting to like KiXtart
Registered: 2004-03-10
Posts: 125
|
Hi there,
I want to be able to see when an nslookup fails to resolve an FQDN. For example: nslookup bbccccccc.co.uk will return: Non-existent domain at the end of its report. This is the part I want to detect.
I tried exporting the shell results to a txt file
Shell '%COMSPEC% /C nslookup ' + $x + ' >Result.txt'
but for some reason it doesn't capture all the text. @ERROR is no good either as it always returns code 0 as the nslookup command itself has not failed.
Any help on this would be great!
Thanks in advance. Glenn.
|
|
Top
|
|
|
|
#188292 - 2008-06-19 09:50 PM
Re: Help with nslookup output
[Re: Glenn Barnas]
|
GSUK
Starting to like KiXtart
Registered: 2004-03-10
Posts: 125
|
I'm playing around with your nslookup() UDF now but I'm getting stuck on
; get the command result and extract what is needed
$_sR = Trim(Split(Join(Split($_oExec.StdOut.ReadAll, @CRLF), ''), 'name:')[1])
How do I get it show the bit I want?
|
|
Top
|
|
|
|
#188293 - 2008-06-19 10:06 PM
Re: Help with nslookup output
[Re: GSUK]
|
GSUK
Starting to like KiXtart
Registered: 2004-03-10
Posts: 125
|
Ok so I've hacked your UDF (hope you don't mind! ) and I've inserted the following:
$MyArray = Split($_oExec.StdOut.ReadAll, @CRLF)
For Each $Element in $MyArray
? $Element
Next
Interestingly though, the array is not picking up the "Non-existent domain" text.
All I get from the array is:
Server: blah.blah.com Address: 10.5.0.10
|
|
Top
|
|
|
|
#188298 - 2008-06-19 11:38 PM
Re: Help with nslookup output
[Re: Glenn Barnas]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
Flight is delayed - try this version:
Function NSLookup($_ID)
Dim $_sR, $_sE ; Return string
Dim $_oExec ; WScript object
; Use WScript to execute the command and check the result
$_oExec = CreateObject("WScript.Shell").Exec('nslookup ' + $_ID)
If Not VarType($_oExec) = 9
$NSLookup = ''
Exit 10
EndIf
; get the command result and extract what is needed
; $_sR holds name result from StdOut, $_sE holds StdErr as single string
$_sR = Trim(Split(Join(Split($_oExec.StdOut.ReadAll, @CRLF), ''),'name:')[1])
$_sE = Trim(Join(Split($_oExec.StdErr.ReadAll, @CRLF), ''))
; return alternate status based on errors in StdErr output
Select
Case InStr($_sE, 'Non-existent')
$NSLookup = 'Non-existent'
Exit 67
EndSelect
; process the return string
If Not $_sR
$NSLookup = ''
Exit 67
Else
$NSLookup = Split($_sR, 'Address:')[0] + ',' + Trim(Split($_sR, 'Address:')[1])
Exit 0
EndIf
EndFunction
This should get you going. The result you were looking for was in StdErr, which wasn't being processed. It's returning a "non-existent" string, which is NOT a good idea, since it breaks the original functionality: $X = NsLookup('fqdn') If Not $X @SERROR ? ; handle error Else ; handle successful lookup EndIf
You'd be much better to return an error code that you identify with "non-existent" instead of "Network Name not found" (which is error 67 that is returned now). You might want to consider using exit value 487. 
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#188300 - 2008-06-20 09:55 AM
Re: Help with nslookup output
[Re: Glenn Barnas]
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
You can also use WMI ping to check name resolution.
I prefer this method as I am usually also checking for the state of the host so it makes sense to do both at the same time.
$oWMI = GetObject("winmgmts:root\cimv2") For Each $sAddress in Split("10.9.99.99 foo.bar.com mail.harsco.com") $colItems = $oWMI.ExecQuery("Select PrimaryAddressResolutionStatus,ProtocolAddress,ResponseTime,StatusCode From Win32_PingStatus Where Address='" + $sAddress + "' And TimeOut=1") For Each $oItem In $colItems If $oItem.ProtocolAddress="" $sResolvedAddress=$sAddress ELSE $sResolvedAddress=$oItem.ProtocolAddress EndIf Select case $oItem.PrimaryAddressResolutionStatus "Could not resolve "+$sAddress+@CRLF case @ERROR OR $oItem.StatusCode "Cannot ping "+$sAddress+" ["+$sResolvedAddress+"]"+@CRLF case "Default" "Address "+$sAddress+" resolves to "+$sResolvedAddress+" and responds in "+$oItem.ResponseTime+" ms"+@CRLF EndSelect Next Next
|
One word of warning though - this method will allow the name to be resolved by all the methods that Windows uses so for example if the name was present in you local host file then you would get a successful resolution even if the name was not present in DNS.
|
|
Top
|
|
|
|
#188308 - 2008-06-20 08:47 PM
Re: Help with nslookup output
[Re: Glenn Barnas]
|
GSUK
Starting to like KiXtart
Registered: 2004-03-10
Posts: 125
|
Nice!
That did the trick!
Thanks for your help Glenn.
Glenn.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1636 anonymous users online.
|
|
|