Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Can i get the mac address added into this script.

hi,

Can i get the mac  address added into this script.
Mac address into the "E" colum.

Regards
sharath
Set objExcel = CreateObject("Excel.Application")
 
objExcel.Visible = True
 
objExcel.Workbooks.Add
 
intRow = 2
 
objExcel.Cells(1, 1).Value = "Machine Name"
 
objExcel.Cells(1, 2).Value = "IPAdress"
 
objExcel.Cells(1, 3).Value = "Alive"
 
objExcel.Cells(1, 4).Value = "Dead"
 
 
Set Fso = CreateObject("Scripting.FileSystemObject")
 
Set InputFile = fso.OpenTextFile("c:\servers.txt")
 
 
 
Do While Not (InputFile.atEndOfStream)
 
HostName = InputFile.ReadLine
 
If Not HostName = "" then 
 
Set WshShell = WScript.CreateObject("WScript.Shell")
 
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)
 
 
 
objExcel.Cells(intRow, 1).Value = HostName
 
objExcel.Cells(intRow, 2).Value = ResolveIP(HostName) 
 
Select Case Ping
 
Case 0 objExcel.Cells(intRow, 3).Value = "On Line"
 
Case 1 objExcel.Cells(intRow, 4).Value = "Off Line"
 
End Select
 
 
 
intRow = intRow + 1
 
End if
 
Loop
 
 
 
objExcel.Range("A1:B1:C1:D1").Select
 
objExcel.Selection.Interior.ColorIndex = 19
 
objExcel.Selection.Font.ColorIndex = 11
 
objExcel.Selection.Font.Bold = True
 
objExcel.Cells.EntireColumn.AutoFit
 
 
 
Function ResolveIP(computerName)
   Dim objShell  :  Set objShell = CreateObject("WScript.Shell")
   Dim objExec   :  Set objExec = objShell.Exec("ping " & computerName & " -n 1")
   Dim strOutput : strOutput = objExec.StdOut.ReadAll
   Dim RegEx     :  Set RegEx = New RegExp
   RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
   RegEx.Global = True
   If RegEx.Test(strOutput) Then
       ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
   Else
       ResolveIP = "IP Address could not be resolved."
   End If
End Function

Open in new window

Avatar of mmarcuse
mmarcuse

I had to read this twice to see that what you wanted to do was get the MAC address of the remote machine.
Since you are already pinging the remote machine, its mac address will be in the ARP cache. The code below will extract the Mac address for a given recently pinged IP address (in the example, 192.168.1.117) If you want the '-' characters in the Mac Address string, then leave out the Replace function. Hope this helps.
' lookup MAC address of an IP address recently pinged.
'
dim objMacLookup
MacAddress="NOT FOUND"
 
set wshShell = CreateObject("WScript.Shell")
ipaddress="192.168.1.117"
command = "%comspec% /c arp -a | FIND "&Chr(34)&ipaddress&Chr(34)
set objMacLookup = wshShell.Exec(command)
MacAddress = Replace(Mid(objMacLookup.StdOut.Readline(),25,17),"-","")

Open in new window

Avatar of bsharath

ASKER

Where should i fit in this code in the main code...
Will it get ip address if the machine is Offline

Why i ask is if it cannot get then can the Mac address be fetched from the DHCP server
SOLUTION
Avatar of mmarcuse
mmarcuse

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i took mmarcuse code and used it in the main code

here :
Set objExcel = CreateObject("Excel.Application")
 
objExcel.Visible = True
 
objExcel.Workbooks.Add
 
intRow = 2
 
objExcel.Cells(1, 1).Value = "Machine Name"
 
objExcel.Cells(1, 2).Value = "IPAdress"
 
objExcel.Cells(1, 3).Value = "Alive"
 
objExcel.Cells(1, 4).Value = "Dead"
 
objExcel.Cells(1, 5).Value = "MacAdress"
 
 
Set Fso = CreateObject("Scripting.FileSystemObject")
 
Set InputFile = fso.OpenTextFile("c:\servers.txt")
 
 
 
Do While Not (InputFile.atEndOfStream)
 
HostName = InputFile.ReadLine
 
If Not HostName = "" then 
 
Set WshShell = WScript.CreateObject("WScript.Shell")
 
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)
 
 
 
objExcel.Cells(intRow, 1).Value = HostName
 
objExcel.Cells(intRow, 2).Value = ResolveIP(HostName) 
 
Select Case Ping
 
Case 0 objExcel.Cells(intRow, 3).Value = "On Line"
 
Case 1 objExcel.Cells(intRow, 4).Value = "Off Line"
 
End Select
 
objExcel.Cells(intRow, 5).Value = FindMac(ResolveIP(HostName))  
 
intRow = intRow + 1
 
End if
 
Loop
 
 
 
objExcel.Range("A1:B1:C1:D1").Select
 
objExcel.Selection.Interior.ColorIndex = 19
 
objExcel.Selection.Font.ColorIndex = 11
 
objExcel.Selection.Font.Bold = True
 
objExcel.Cells.EntireColumn.AutoFit
 
 
 
Function ResolveIP(computerName)
   Dim objShell  :  Set objShell = CreateObject("WScript.Shell")
   Dim objExec   :  Set objExec = objShell.Exec("ping " & computerName & " -n 1")
   Dim strOutput : strOutput = objExec.StdOut.ReadAll
   Dim RegEx     :  Set RegEx = New RegExp
   RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
   RegEx.Global = True
   If RegEx.Test(strOutput) Then
       ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
   Else
       ResolveIP = "IP Address could not be resolved."
   End If
End Function
 
Function FindMac(ip)
MacAddress="NOT FOUND"
 
set wshShell = CreateObject("WScript.Shell")
ipaddress=ip
command = "%comspec% /c arp -a | FIND "&Chr(34)&ipaddress&Chr(34)
set objMacLookup = wshShell.Exec(command)
FindMac = Replace(Mid(objMacLookup.StdOut.Readline(),25,17),"-","")
End Function

Open in new window

Thank U...

If offline can't i get the mac address from the DHCP server
Thank U...

If offline can't i get the mac address from the DHCP server
maybe with the netsh command, but to this correctly for a spesific ip address
it's out of my knowledge.

any way if you find a way i'm the first person you need tell him the solution :-)
yehudaha

I get the mac address for 900 out of 3000 system names

I guess its getting the data of 1 range of ip's
Or a DHCP
Not sure but it shows as its getting the mac of online systems from one range of ip addresses and not the others that get the ip's from different DHCP servers even when they are online
yehudaha

I get the mac address for 900 out of 3000 system names

I guess its getting the data of 1 range of ip's
Or a DHCP
Not sure but it shows as its getting the mac of online systems from one range of ip addresses and not the others that get the ip's from different DHCP servers even when they are online
try this
Set objExcel = CreateObject("Excel.Application")
 
objExcel.Visible = True
 
objExcel.Workbooks.Add
 
intRow = 2
 
objExcel.Cells(1, 1).Value = "Machine Name"
 
objExcel.Cells(1, 2).Value = "IPAdress"
 
objExcel.Cells(1, 3).Value = "Alive"
 
objExcel.Cells(1, 4).Value = "Dead"
 
objExcel.Cells(1, 5).Value = "MacAdress"
 
 
Set Fso = CreateObject("Scripting.FileSystemObject")
 
Set InputFile = fso.OpenTextFile("c:\servers.txt")
 
 
 
Do While Not (InputFile.atEndOfStream)
 
HostName = InputFile.ReadLine
 
If Not HostName = "" then 
 
Set WshShell = WScript.CreateObject("WScript.Shell")
 
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)
 
 
 
objExcel.Cells(intRow, 1).Value = HostName
 
objExcel.Cells(intRow, 2).Value = ResolveIP(HostName) 
 
Select Case Ping
 
Case 0 objExcel.Cells(intRow, 3).Value = "On Line"
 
Case 1 objExcel.Cells(intRow, 4).Value = "Off Line"
 
End Select
 
If per(HostName) Then  
objExcel.Cells(intRow, 5).Value = FindMac(HostName)  
Else
objExcel.Cells(intRow, 5).Value = "You Dont Have Permission To Query " & HostName
End if
 
intRow = intRow + 1
 
End if
 
Loop
 
 
 
objExcel.Range("A1:B1:C1:D1").Select
 
objExcel.Selection.Interior.ColorIndex = 19
 
objExcel.Selection.Font.ColorIndex = 11
 
objExcel.Selection.Font.Bold = True
 
objExcel.Cells.EntireColumn.AutoFit
 
 
 
Function ResolveIP(computerName)
   Dim objShell  :  Set objShell = CreateObject("WScript.Shell")
   Dim objExec   :  Set objExec = objShell.Exec("ping " & computerName & " -n 1")
   Dim strOutput : strOutput = objExec.StdOut.ReadAll
   Dim RegEx     :  Set RegEx = New RegExp
   RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
   RegEx.Global = True
   If RegEx.Test(strOutput) Then
       ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
   Else
       ResolveIP = "IP Address could not be resolved."
   End If
End Function
 
Function FindMac(ip)
strComputer = ip
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colItems = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
For Each objItem in colItems
    FindMac = objItem.MACAddress
Next
End Function
 
Function per(computer)
	strcomputer = computer
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	If err.number <> 0 Then
		err.Clear
		per = False
		On Error goto 0
	Else
		per = True
		On Error goto 0
	End If
End Function

Open in new window

I get this in colum E
You Dont Have Permission To Query machine name
I get this in colum E
You Dont Have Permission To Query machine name
true

your not admin in the machine your trying to get the mac address.
mmarcuse code doesn't qury remote subnets.

so i changed the code to wmi syntax that has no prblem to query any subnet,
but the limitation you need to be with admin rights on the machine.

so you need to choose whats best for you.
yehudaha

I am the Domain admin of the Domain

I am running it from a Machine logged in as Administrator

I guess i am getting this because the systems are off
For the machines which are ON i get the mac adddress

Its become very slow now. Before it was fast.

From the last comment till now its scanned just 300+ systems. Is there any way to speed it up.

I guess that would solve my issues
As mentioned mac addresses cannot be pulled from the DHCP server if the machines are off
yehudaha

I am the Domain admin of the Domain

I am running it from a Machine logged in as Administrator

I guess i am getting this because the systems are off
For the machines which are ON i get the mac adddress

Its become very slow now. Before it was fast.

From the last comment till now its scanned just 300+ systems. Is there any way to speed it up.

I guess that would solve my issues
As mentioned mac addresses cannot be pulled from the DHCP server if the machines are off
i see

i can improve another thing so the wmi querys will be more faster
and you will not get the error to turn off machine.

ok with you ?
ya that would be fine....
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank U... :-)))