PDA

View Full Version : NSLookup



Knut Sparhell
15-Sep-2005, 10:13 AM
I wanted to (reverse) look up a host name in DNS for my web application,
found that the ASP many methods to do this was unusable for several
reasons, so I created my own function insdide my webapp:

Function NSLookup String sIp Returns String
String sDir sBat sOut
Get psProgramPath Of (phoWorkspace(ghoApplication)) To sDir
Set_Directory (sDir+'\')
Make_Temp_File 'bat' sBat
Make_Temp_File 'out' sOut
Direct_Output sBat
Writeln '@nslookup.exe %1>%2'
Close_Output
Move ;
(ShellExecute(Window_handle(Self),'Open',sBat,(sIp *'"'+sOut+'"'),sDir,False));
To strlen
Sleep 1
EraseFile (sDir+'\'+sBat)
Direct_Input sOut
Readln sBat
Readln sBat
Readln sBat
Readln sBat
Close_Input
EraseFile (sDir+'\'+sOut)
If (sBat>"") Function_Return (Mid(sBat,256,10))
Else Function_Return sIp
End_Function


What it does:

1. It's a function taking an IP address as input and returning the host
name, or the same IP if it can't be resolved.

2. Sets the working folder to the Programs folder. This is assumed to
have write access for SYSTEM. DataPath or any may be used, but be aware
of multiple paths in there.

3. Creates two temporary files in it, one .bat file and one .out file

4. Writes a command to the .bat file, instruction NSLookup to look up an
IP address given by a parameter and to output it's result ot a file
given by a parameter

5. Executes the .bat file (NSLookup), giving the IP and the output file
name (in quotes) as parameteres. Discards the return value for the
execution.

6. The .bat executes invisibly in the background with no black command
prompt window

7. Waits until it has probably finished (guessing involved), as there is
no wait function here

8. Reads the output file and extracts the result (a host name or nothing)

9. Deletes the temporary files used

10. Returns the input string if nothing is found

ShellExecute has to be defined as external, usually it is already.

Some may find it useful. Works great with my webapps. I use it for
logging and About information, checking the
Request.ServerVariables("remote_host").

--
Knut Sparhell, Norway