Page 1 of 1 1
Topic Options
#188290 - 2008-06-19 09:33 PM Help with nslookup output
GSUK Offline
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

 Code:
 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
#188291 - 2008-06-19 09:39 PM Re: Help with nslookup output [Re: GSUK]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
You can try using kfNsLookup() if you install the Kixforms DLL. There's also a nslookup() UDF that uses WSH to capture the results.

I think both are posted here, and are in the Resources/KixUDF section of my web site. The kf version is similar, but is more silent than the nslookup(), which sometimes pops up a brief command window.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#188292 - 2008-06-19 09:50 PM Re: Help with nslookup output [Re: Glenn Barnas]
GSUK Offline
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

 Code:
 
; 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 Offline
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:

 Code:
	$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
#188297 - 2008-06-19 11:26 PM Re: Help with nslookup output [Re: GSUK]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
That's why I was filtering on "Name:" - that's the data returned from NSLookup.

I might consider something like:
 Code:
$Intermediate = Split($_oExec.StdOut.ReadAll, @CRLF)
Select
 Case AScan($Intermediate, 'Non-existent')
  $NsLookup = ''
  Exit -4
EndSelect

Don't have time now to flesh this out, but basically - create a case statement to look in the array for specific errors, then do the trim/join/split/join to return the valid data. Define a separate exit code for each error condition - use negative numbers to indicate that the data is invalid, as opposed to positive numbers to indicate a "real" error condition.

Hack away, and post it back here - I'll pitch in when I have some time, but it might not be for a day or so.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#188298 - 2008-06-19 11:38 PM Re: Help with nslookup output [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Flight is delayed - try this version:
 Code:
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! \:D

Top
#188300 - 2008-06-20 09:55 AM Re: Help with nslookup output [Re: Glenn Barnas]
Richard H. Administrator Offline
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 Offline
Starting to like KiXtart

Registered: 2004-03-10
Posts: 125
Nice!

That did the trick!

Thanks for your help Glenn.

Glenn.

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 1471 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.06 seconds in which 0.025 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