viewpoint-particle

Author Topic: Get hostname/IP using MAC Address?  (Read 3469 times)

Secondlaw

  • Newbie
  • *
  • Posts: 9
Get hostname/IP using MAC Address?
« on: January 15, 2014, 06:30:56 am »
Is it possible via Winbatch to obtain a list of remote hostnames/ip address if only give the MAC address's?

Deana

  • Wilson WindowWare Tech Support
  • Tech Support
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: Get hostname/IP using MAC Address?
« Reply #1 on: January 15, 2014, 07:40:40 am »
If looking at a single workstation, I would simply parse the results from the ARP command. The code might look something like this:

Code: Winbatch
; This UDF function executes a DOS command and returns the
; sysout, syserror and exitcode information from the DOS
; program as a 3 element array

; Elements   Contents
;    0          ErrorLevel
;    1          StdOut
;    2          StdError

;Note: No way yet known to hide the DOS window from appearing on the screen
;except to perhaps have a "Keep on top" conver screen
;(i.e. BoxesUp and other box functions + IntControl(54,1,0,0,0)

#DefineFunction CaptureDosOutput(DosCommand)
   
   DataArray=ArrDimension(3)               ;Allocate return array
   ArrInitialize(DataArray,"")             ;initialize to all null strings
   oShell = ObjectOpen("WScript.Shell")    ;open shell object
   oScriptExec = oShell.Exec(DosCommand)   ;run the command

   ;Open output objects
   oStdOut = oScriptExec.StdOut
   oStdErr = oScriptExec.StdErr

   While (oScriptExec.Status==0)           ;wait for completion

      ;Caputure StdOut data
      oStdOut = oScriptExec.StdOut
      While ! oStdOut.AtEndOfStream
         strLine = oStdOut.ReadLine
         DataArray[1] = StrCat(DataArray[1],strLine,@CRLF)
      EndWhile
   
      ;Capture StdErr data
      oStdErr = oScriptExec.StdErr
      While ! oStdErr.AtEndOfStream
         strLine = oStdErr.ReadLine
         DataArray[2] = StrCat(DataArray[2],strLine,@CRLF)
      EndWhile
   
      TimeDelay(0.1)
   EndWhile

   ;Get remainder of data, if any

      ;Caputure StdOut data
      oStdOut = oScriptExec.StdOut
      While ! oStdOut.AtEndOfStream
         strLine = oStdOut.ReadLine
         DataArray[1] = StrCat(DataArray[1],strLine,@CRLF)
      EndWhile
   
      ;Capture StdErr data
      oStdErr = oScriptExec.StdErr
      While ! oStdErr.AtEndOfStream
         strLine = oStdErr.ReadLine
         DataArray[2] = StrCat(DataArray[2],strLine,@CRLF)
      EndWhile


   DataArray[0]=oScriptExec.ExitCode         ;save errorlevel/exit code

   ;Close handles that were opened
   ObjectClose(oStdOut)
   ObjectClose(oStdErr)
   ObjectClose(oScriptExec)
   ObjectClose(oShell)

   ;Return the array
   Return(DataArray)

#EndFunction



DirChange("c:\windows")
CaptureArray=CaptureDosOutput("arp -a")       ; for normal DOS executables
;CaptureArray=CaptureDosOutput("cmd.exe /c dir *.*")      ; for built-in dos commands
result = CaptureArray[1]
;clipput(result)
;Pause(StrCat("Exit code: ",CaptureArray[0]),result)
If CaptureArray[0] !=0 Then Pause("SysErr info",CaptureArray[2])

;SAMPLE RESULTS
;Interface: 10.0.0.25 --- 0xb
;  Internet Address      Physical Address      Type
;  10.0.0.1              00-05-b2-2f-21-91     dynamic  
;  255.255.255.255       ff-ff-ff-ff-ff-ff     static    

;Locate the ipaddress using the MAC address
MACaddress = 'ff-ff-ff-ff-ff-ff'

macptr = StrIndexNc( result, MACaddress, 1, @Fwdscan )
If macptr == 0
   Message("Sorry...", "MAC Address Not found")
   Exit
EndIf

beginlineptr = StrIndexNc( result, @lf, macptr, @Backscan )
If macptr == 0
   Message("Sorry...", "MAC Address Not found")
   Exit
EndIf

spaceptr = StrIndexNc( result, " ", beginlineptr+3, @Fwdscan )
If spaceptr == 0
   Message("Sorry...", "IP Address Not found")
   Exit
EndIf

IPAddress = StrSub( result, beginlineptr, spaceptr-beginlineptr )
Pause( MACaddress, IPAddress)

However, it appears that you want to search the network looking for a particular MAC address. Unfortunately it is not that straight forward. You would need to bind to each computer and check to see if the MAC address can be found on that machine. this can be handles using WMI. Please read: http://blogs.technet.com/b/heyscriptingguy/archive/2006/01/24/how-can-i-display-the-ip-addresses-associated-with-a-given-mac-address.aspx
Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

  • Wilson WindowWare Tech Support
  • Tech Support
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: Get hostname/IP using MAC Address?
« Reply #2 on: January 15, 2014, 08:21:42 am »
Here is my TOTALLY UNDEBUGGED attempt at writing code to query the network using WMI to obtain the IPaddress of a particular MAC address.
Code: Winbatch
MACAddress = '99:99:99:AA:99:A9'

;Load Appropriate Extender
If WinMetrics(-2) == 3 Then AddExtender("WWADS64I.DLL") ; 64-bit
Else AddExtender("WWADS44I.DLL") ; 32-bit

; Construct the full domain ADSI path.
ServerDn = dsGetProperty("LDAP://rootDSE", "serverName")
ServerName = ItemExtract(1, ServerDn, ",")
ServerName = ItemExtract(2, ServerName, "=")
ServerDn = dsGetProperty("LDAP://rootDSE", "defaultNamingContext")
ServerPath = "LDAP://%ServerName%/%ServerDN%"

; Find the computers path
ComputerPaths = dsFindPath(ServerPath, "(&(objectCategory=computer))")
For x = 1 to ItemCount(ComputerPaths,@tab)
   item = ItemExtract( x, ComputerPaths, @tab )
   strComputer = ItemExtract( 2, ItemExtract( 1, item, "," ), "=" )
   ErrorMode(@off)
   objWMIService = GetObject("winmgmts:\\" : strComputer : "\root\cimv2")
   ErrorMode(@Cancel)
   If objWMIService == 0
      Pause("Notice","Unable to connect to ":strComputer)
      continue
   Endif
   Query = "Select * from Win32_NetworkAdapterConfiguration Where MACAddress = '":MACAddress:"'"
   colItems = objWMIService.ExecQuery (Query)
   
   If colItems.Count == 0 Then
       Pause(strComputer,"There are no adapters with that MAC address.")
       continue
   EndIf
   
   ForEach objItem in colItems
       ForEach strIPAddress in objItem.IPAddress
           Pause( "IP Address: " , strIPAddress )
       Next
   Next
Next

Exit
Deana F.
Technical Support
Wilson WindowWare Inc.