Найти по MAC адресу IP компьютера

Как-то случайно натыкался на полезные скрипты для определения по мак адресу айпи компьютера:

такой вот коротенький батничек, пингующий группу адресов и записывающий в файлик arp_table.txt

@echo off
if exist arp_table.txt (
if exist arp_table.bak del arp_table.bak
ren arp_table.txt arp_table.bak
)
for /L %%i in (1,1,254) do (
echo 192.168.0.%%i
ping -n 1 192.168.0.%%i>nul
arp -a 192.168.0.%%i>arp_string.tmp
for /F "skip=3 tokens=*" %%j in (arp_string.tmp) do (
echo %%j|find /V "invalid">>arp_table.txt
)
)
del arp_string.tmp

и более тотальный скрипт:

  1. '******************************************************************************
  2. '* File: mac2ip.vbs
  3. '* Purpose: Finds IP of the host with given MAC address.
  4. '* Syntax: cscript mac2ip.vbs /mac:MAC_ADDRESS /ip:IP_BLOCK [/p:pause]
  5. '* Version: 1.0.2
  6. '*
  7. '* Technology: VBSCRIPT,WSH,WMI
  8. '* Requirements: Windows XP or newer (uses Win32_PingStatus WMI class)
  9. '*
  10. '* Authors: 4u3u (asuhovey mtu-net ru)
  11. '*
  12. '******************************************************************************
  13. Option Explicit
  14. 'On Error Resume Next
  15. Dim objShell,strOutput,i,strMAC,objOutput,objAdapter,intPause
  16. Dim strSTime,strIP,strNameSpace,strQuery,colAdapters,PausePattern
  17. Dim colResult,IPPattern,MACPattern,strArpCommand,strOMAC
  18.  
  19. '* Matches valid MAC address
  20. MACPattern = "^([0-9a-f][0-9a-f]-){5}([0-9a-f][0-9a-f])$"
  21. '*Matches valid IP block
  22. IPPattern = "^b((25[0-5]|2[0-4]d|[01]dd|d?d).){3}$"
  23. '*Matches positive integer
  24. PausePattern = "^d+$"
  25.  
  26. '****************************************
  27. 'Parse arguments
  28. '****************************************
  29. strOMAC = WScript.Arguments.Named("mac")
  30. strMAC = Replace(LCase(strOMAC),":","-")
  31. strIP = Wscript.Arguments.Named("ip")
  32. intPause = Wscript.Arguments.Named("p")
  33. If intPause="" Then intPause=1000
  34. If Wscript.Arguments.Count<2 Then
  35. Syntax()
  36. End If
  37. If not ArgVrf(strMAC,MACPattern) Then
  38. WScript.Echo "ERROR: Invalid MAC: '" & strOMAC & "' Check syntax..." & VbCrLf
  39. Syntax()
  40. End If
  41. If not ArgVrf(strIP,IPPattern) Then
  42. WScript.Echo "ERROR: Invalid IP: '" & strIP & "' Check syntax..." & VbCrLf
  43. Syntax()
  44. End If
  45. If not ArgVrf(intPause,PausePattern) Then
  46. WScript.Echo "ERROR: Invalid Pause: '" & intPause & "' Must be positive integer." & VbCrLf
  47. Syntax()
  48. End If
  49.  
  50. ''****************************************
  51. 'Check if script is running by cscript.
  52. 'If not, restart script using cscript.
  53. '****************************************
  54. Set objShell = WScript.CreateObject("WScript.Shell")
  55. If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
  56. objShell.Run "cmd /k cscript.exe /nologo """ & WScript.ScriptFullName & """ /mac:" &_
  57. strOMAC & " /ip:" & strIP & " /p:" & intPause, 1, False
  58. WScript.Quit
  59. End If
  60.  
  61. '****************************************
  62. 'Check if given MAC is owned by local host
  63. '****************************************
  64. strNameSpace = "winmgmts:{impersonationLevel=impersonate}//./root/cimv2"
  65. strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"
  66. Set colAdapters = GetObject(strNameSpace). ExecQuery(strQuery)
  67. For Each objAdapter in colAdapters
  68. If LCase(Replace(objAdapter.MACAddress,":","-")) = strMAC Then
  69. WScript.Echo strOMAC & " is YOUR MAC address!"
  70. WScript.Quit
  71. End If
  72. Next
  73.  
  74. '****************************************
  75. 'Ping all IPs in range.
  76. '****************************************
  77. strSTime = Now()
  78. strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & strIP
  79. For i = 1 To 254 Step 1
  80. Set colResult = GetObject(strNameSpace). ExecQuery(strQuery & i & "'")
  81. WScript.StdOut.Write "."
  82. If i/51-Int(i/51)=0 Then
  83. WScript.StdOut.WriteLine " " & Int(100*i/254) & "%"
  84. End If
  85. Next
  86. WScript.Echo ". 100%" & VbCrLf & "Ping completed in " & DateDiff("s",strSTime,Now()) & " seconds." & VbCrLf
  87.  
  88. '****************************************
  89. 'Get IP-MAC pair from ARP cache.
  90. '****************************************
  91. WScript.Sleep(intPause)
  92. strArpCommand = "%comspec% /c arp.exe -a | find /i """ & strMAC & """"
  93. Set objOutput=objShell.Exec(strArpCommand).StdOut
  94. WScript.Echo String(56,"=")
  95. i=0
  96. Do While Not objOutput.AtEndOfStream
  97. i=i+1
  98. strOutput = objOutput.ReadLine
  99. WScript.Echo StrOMAC & " is " & RTrim(Left(LTrim(strOutput),15))
  100. Loop
  101. If i=0 Then
  102. WScript.Echo strOMAC & " was NOT FOUND" &_
  103. " on the local network" & VbCrLf &_
  104. "Either host is unreachable or " & strIP &_
  105. "xxx is not" & VbCrLf & "your local network."
  106. End If
  107. WScript.Echo String(56,"=")
  108.  
  109. '****************************************
  110. 'Subroutines
  111. '****************************************
  112. '*Syntax
  113. Sub Syntax()
  114. Dim s
  115. s = "Finds IP of the host with given MAC address." & VbCrLf & VbCrLf
  116. s = s & "SYNTAX: cscript /nologo " & UCase(WScript.ScriptName) &_
  117. " /mac:MAC_ADDRESS /ip:IP_BLOCK [/p:pause]" & VbCrLf & VbCrLf
  118. s = s & " MAC_ADDRESS MAC address to search for. MAC_ADDRESS" & VbCrLf
  119. s = s & " is in XX-XX-XX-XX-XX-XX format. Both" & VbCrLf
  120. s = s & " ':' and '-' delimiters allowed." & VbCrLf
  121. s = s & " IP_BLOCK First three octets of local ip range to" & VbCrLf
  122. s = s & " scan. E.g. '192.168.0.' Only /24 subnets" & VbCrLf
  123. s = s & " are supported in current version." & VbCrLf
  124. s = s & " pause Pause after ping in milliseconds." & _
  125. VbCrLf & " Default is 1000 (1 second)" & VbCrLf & VbCrLf
  126. s = s & "Example:" & VbCrLf
  127. s = s & " cscript /nologo " & UCase(WScript.ScriptName) &_
  128. " /mac:01-23-45-67-89-AB /ip:192.168.0." & VbCrLf
  129. WScript.Echo s
  130. WScript.Quit 1
  131. End Sub
  132.  
  133. '*Arguments validation
  134. Function ArgVrf(arg,pattern)
  135. Dim objRegExp
  136. Set objRegExp = New RegExp
  137. objRegExp.IgnoreCase = true
  138. objRegExp.Pattern = pattern
  139. ArgVrf = objRegExp.Test(arg)
  140. Set objRegExp = Nothing
  141. End Function
  142.  
  143. '****************************************
  144. 'End of script
  145. '****************************************