Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get MAC address from IP and hiode CMD

Status
Not open for further replies.

utilman

IS-IT--Management
Dec 25, 2002
35
NL
Hi,

This code works for me. Only the CMD windows are visible.
Does somebody nows how to hide these?

Thx

Mickey

Code:
On error Resume Next

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshProcessEnvironment = WshShell.Environment("Process")

LogonServer = WshProcessEnvironment("LogonServer")

'Msgbox(GetMac("10.1.252.2"))   'Msgbox the results of the GetMac Function, passings an IP Address to it

Function GetMac(ip)     'Begin GetMac function 
Set WshExec = WshShell.Exec("ping -n 4 -w 1000 " & ip) 'Run the PING command in command shell to build ARP table
WScript.Sleep(2000)     'Sleep 2 seconds, allows ARP table to catch up
Set WshExec = WshShell.Exec("arp -a")   'List the ARP table
strPingResults = LCase(WshExec.StdOut.ReadAll)  'Drop the entire ARP table into a variable
strARPList = Split(strPingResults)   'Break variable into an Array
n = UBound(strARPList,1)    'Get the size of the Array
For X = 0 To n      'Loop from 0 to the size of the Array
If strARPList(x) = ip Then    'Compare the Array single variable to the IP address, if they match...
For y = 1 To 16     'Loop for 16 (this is used to hunt down the MAC address)
If strARPList(x+y) <> "" Then   'If the array variable is NOT empty (must be the MAC ID)....
 GetMac = strARPList(x+y) 'Return the MAC ID 
 Exit For   'Exit For Next Loop
End If
Next
End If
Next
End Function


'PRODUKTIE OMGEVING (FIREWALL MAC PROD)
If (GetMac("10.1.252.2")) = "00-10-db-ff-25-21" Then
   WshShell.Run chr(34) & LogonServer & "\Netlogon\Software\Bginfo\Bginfo.exe " & chr(34) & LogonServer & "\Netlogon\Software\Bginfo\BG_PRODUKTIE.bgi /timer:0 /nolicprompt" & chr(34), 1, True
   MsgBox "Je bent ingelogd op het PRODUKTIE Netwerk1"
Else
   'MsgBox "not ok"
End If


'TEST OMGEVING (FIREWALL MAC TEST)
If (GetMac("10.1.252.2")) = "00-1f-12-48-2a-4b" Then
   WshShell.Run chr(34) & LogonServer & "\Netlogon\Software\Bginfo\Bginfo.exe " & chr(34) & LogonServer & "\Netlogon\Software\Bginfo\BG_TEST.bgi /timer:0 /nolicprompt" & chr(34), 1, True
   MsgBox "Je bent ingelogd op het TEST Netwerk2"
Else
   'MsgBox "not ok"
End If


'UITWIJK OMGEVING (FIREWALL MAC UITW)
If (GetMac("10.1.252.2")) = "00-1f-12-4a-1d-0b" Then
   WshShell.Run chr(34) & LogonServer & "\Netlogon\Software\Bginfo\Bginfo.exe " & chr(34) & LogonServer & "\Netlogon\Software\Bginfo\BG_UITWIJK.bgi /timer:0 /nolicprompt" & chr(34), 1, True
   MsgBox "Je bent ingelogd op het TEST Netwerk3"
Else
   'MsgBox "not ok"
End If
 
The BGinfo.exe screens are not visible, but the ping/arp commands generate the CMD screen. I want to hide those.

But when I change to WshShell.Exec to WshShell.Run the script is not working any longer..
 
would guess the .exec command seeing as it only takes the command line as a param does not allow you to the set it to not visible. can you use cmd.exe parameters to launch your shell commands in such as way to make them minimized or not visible? is there another way for you to get the information you want other than running a shell command? (you can certainly get away with running the ping from .Run or perhaps using Win32_PingStatus class, grabbing the output from arp might be possible from a WMI class? if not then pipe the output to a text file and read it in (not very nice but with allow the use of .Run)
 
Win32_NetworkAdapterConfiguration is your friend here:
Code:
[blue]Public Function GetMac(strComputerIP)
    Dim IPaddress
    Set objWMIService = GetObject("winmgmts:\\" & strComputerIP & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterCOnfiguration")
    For Each objItem In colItems
        If Not IsNull(objItem.IPaddress) Then
            For Each IPaddress In objItem.IPaddress
                If IPaddress = strComputerIP Then
                    GetMac = objItem.MACAddress
                End If
            Next
        End If
    Next
End Function[/blue]
 
If I change the function to one of the bellow codes,
the script will tell me that I'm connected to all 3 VLAN's.
MAC-address comparison is not correct I think. Don't see
the problem.


Code:
Function GetMac(ip)     'Begin GetMac function 
'Set WshExec = WshShell.Exec("ping -n 4 -w 1000 " & ip) 'Run the PING command in command shell to build ARP table
WshShell.Run cmd /c ("Ping -n 4 -w 1000 " & ip, 0, True)
WScript.Sleep(2000)     'Sleep 2 seconds, allows ARP table to catch up
'Set WshExec = WshShell.Exec("arp -a")   'List the ARP table
WshShell.Run cmd /c ("arp -a", 0, True)
strPingResults = LCase(WshExec.StdOut.ReadAll)  'Drop the entire ARP table into a variable
strARPList = Split(strPingResults)   'Break variable into an Array
n = UBound(strARPList,1)    'Get the size of the Array
For X = 0 To n      'Loop from 0 to the size of the Array
If strARPList(x) = ip Then    'Compare the Array single variable to the IP address, if they match...
For y = 1 To 16     'Loop for 16 (this is used to hunt down the MAC address)
If strARPList(x+y) <> "" Then   'If the array variable is NOT empty (must be the MAC ID)....
 GetMac = strARPList(x+y) 'Return the MAC ID 
 Exit For   'Exit For Next Loop
End If
Next
End If
Next
End Function

Code:
Public Function GetMac(strComputerIP)
    Dim IPaddress
    Set objWMIService = GetObject("winmgmts:\\" & strComputerIP & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterCOnfiguration")
    For Each objItem In colItems
        If Not IsNull(objItem.IPaddress) Then
            For Each IPaddress In objItem.IPaddress
                If IPaddress = strComputerIP Then
                    GetMac = objItem.MACAddress
                End If
            Next
        End If
    Next
End Function
 
penny has just dropped for me. pipe the output of arp to a text file and read it back in if you want to avoid the windows..., comment your code accordingly (so no one thinks you are dumb in 2 months time)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top