get FQDN using a script


Once upon a time, I redesigned a LAN : merge two LAN's together after we replaced a slow leased line with modems and routers by a fast optic fiber link. This of course involved some changes to the hosts' network configuration, which we did by DHCP. While we were at it, we redefined address scopes, scope options (default router, prefered DNS servers, etc), new exclusion ranges etc.

This should have worked, except that it did not. Apparently some hosts tended to keep their old addresses, and the dynamic update of WINS or DNS got messy and we ended up with discrepancies between netBIOS hostnames and DNS Fyully Qualified Domain Names. We therefore used a script to force selected hosts to renew their DHCP lease, and used a second script to check for discrepancies between host names. That second script is the following :


'*************************************************************************************
'  Koen Noens, January 2006
'
'  CHeck DNS names and NetBIOS host names
'
'*************************************************************************************

' main
	logfile="D:\suspiciousnames.txt"			'use shared file for LAN-wide log
	
	hostname = gethostname
	strIPAddress = getIPAddress
	'strIPAddress = "216.163.137.3"				'test, proof-of-concept
	FQDN = GetFQDN(strIPAddress)
	
	set fso = createObject("scripting.filesystemobject")
	

	'debug
	wscript.echo hostname
	wscript.echo strIPAddress
	wscript.echo FQDN


	if checkNames(hostname, FQDN) = False Then

		strResult = "CHECK name mismatch: " & strIPAddress & vbTab & hostname & vbTab & FQDN

		'log it
		set oLog = fso.OpenTextFile (logfile, 8, True)
		oLog.WriteLine strResult
		oLog.Close
		set oLog = Nothing

	end if
	
	set fso = Nothing
	wscript.quit

'--------------------------------------------------------------------------------------------------------------
Function GetFQDN(ipaddress)

' Returns Fully Qualified Domain Name
' from reverse DNS lookup via nslookup.exe
' only implemented on NTx
'
'	based on http://www.databasejournal.com/img/FQDN.vbs_
'
' against DNS server with WMI support, you can query WMI. 
' this script does not require WMI on DNS server


	Set objShell = createobject("wscript.shell")
	Set objExec = objShell.Exec("%comspec% /C " & "nslookup " & ipaddress)

	strLookup = objExec.StdOut.ReadAll
	if instr(1,strLookup,"Name:",1) >0 then
		
		'find trigger and get rid of garbage. look at "nslookup IP_ADDR" output to grasp what we do here
		trigger = "Name:"		
		position = Instr (1, strLookup, trigger)
		strLookup = Right (StrLookup, (Len(strLookup) - position - Len(Trigger)))

		trigger = "Address:"		
		position = Instr (strLookup, trigger)
		strLookup = Left (StrLookup, position-1)
		FQDN = cleanupString(strLookup)
	else 
		FQDN = "FQDN not found" 
	end if

	set objExec = Nothing
	getFQDN = FQDN
End Function

'------------------------------------------------------------------------------------------------
Function getHostName()
	'returns computername of computer where script runs
	Set WshNetwork = WScript.CreateObject("WScript.Network")
	getHostName = WshNetwork.ComputerName
End Function

'------------------------------------------------------------------------------------------------
Function checkNames(sHostname, sFQDN)
	
	'compare hostname with FQDN
	If Ucase(Trim(sHostname)) = Ucase(Left(Trim(sFQDN),Len(sHostname))) Then
		checkNames = True
		Exit Function
	Else
		checkNames = False
		Exit Function
	End If
End Function

'------------------------------------------------------------------------------------------------
Function GetIPAddress
	strComputer = "."
	Set objWMIService = GetObject( "winmgmts:\\" & strComputer & "\root\cimv2")
	Set IPConfigSet = objWMIService.ExecQuery _
  		 ("Select IPAddress from Win32_NetworkAdapterConfiguration" & " where IPEnabled=TRUE")
 
	For Each IPConfig in IPConfigSet
		If Not IsNull(IPConfig.IPAddress) Then 
			For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
				GetIPAddress =  IPConfig.IPAddress(i)
				Exit For
			Next
    		End If
	Next

End Function

'------------------------------------------------------------------------------------------------
Function cleanupString (theString)
	' clean up a string from StdOut etc : remove spaces, tabs and newlines / carriage returns
	
	theString = Trim (theString)

	Do While  (	ASC(Right(theString,1)) < 33  OR _
			ASC(Right(theString,1)) > 127 		)

		theString = Left (TheString, Len(TheString)-1)
	Loop

	Do While  (	ASC(Left(theString,1)) < 33  OR _
			ASC(Left(theString,1)) > 127 		)

		theString = Right (TheString, Len(TheString)-1)
		theString= Trim (TheString)
	Loop

	cleanupString = theString
End Function