martin9700
(Martin9700)
1
Description
This script was written for a specific request, here: http://community.spiceworks.com/topic/83220
Edit first 2 lines of text in the script to match your needs.
startAddress: IP address to begin search at
numberAddresses: number of nodes to search
Source Code
startAddress = "192.168.0.1"
numberAddresses = 10
'******* Do not edit beyond this point!! ********
Dim objExplorer, txtOutput, fs, ResFile, CSVFile
const crlf="<BR>"
Setup
LoopSearch
ResFile.Close
CSVFile.Close
showText(crlf & "Finished!")
Wscript.quit
Sub LoopSearch
'Break down the IP
Dim IP(4)
strIP = startAddress
For i = 1 to 3
IP(i) = Left(strIP, Instr(strIP, ".") - 1)
strIP = Right(strIP, Len(strIP) - Instr(strIP, "."))
Next
IP(4) = strIP
If IP(4) = 0 then IP(4) = 1
For loopIP = 1 to numberAddresses
tmpIP = ""
For i = 1 to 3
tmpIP = tmpIP & IP(i) & "."
Next
tmpIP = tmpIP & IP(4)
ResPing = Ping(tmpIP)
If ResPing = "Failed" then
ResParsed = "No Response"
Else
If IsNull(ResPing) or ResPing = "" or ResPing = tmpIP then
ResParsed = "Ping Response, No Name Resolution"
Else
ResParsed = ResPing
discoverDevice ResParsed, tmpIP
End If
End If
IP(4) = IP(4) + 1
For i = 4 to 1 Step - 1
If IP(i) > 254 and i > 1 then
IP(i) = 1
IP(i - 1) = IP(i - 1) + 1
Else
If IP(1) > 254 then
showText("Inputted IP range ran past valid IP range")
wscript.Quit(0)
End If
End If
Next
Next
End Sub
Function Ping(strHost)
Dim objPing, objRetStatus
showText("Pinging " & strHost & "...")
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & strHost & "' AND ResolveAddressNames = TRUE")
For Each objRetStatus in objPing
If IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode <> 0 then
Ping = "Failed"
Else
Ping = objRetStatus.ProtocolAddressResolved
End if
Next
Set objPing = Nothing
End Function
Sub Setup
Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 400
objExplorer.Height = 200
objExplorer.Left = 100
objExplorer.Top = 100
Do While (objExplorer.Busy)
Wscript.Sleep 200
Loop
objExplorer.Visible = 1
txtOutput=""
Set fs = CreateObject ("Scripting.FileSystemObject")
Set ResFile = fs.CreateTextFile (".\IPDevices.txt")
resFile.WriteLine "IP Address Node Name MAC Address"
resFile.WriteLine "=============================================================="
resFile.WriteLine
Set CSVFile = fs.CreateTextFile (".\IPDevices.csv")
CSVFile.WriteLine "Host,IP Address,MAC"
End Sub
Sub ShowText(txtInput)
txtOutput = "Network Discovery In Progress:" & crlf & "==================================" & crlf
txtOutput = txtOutput & txtInput
objExplorer.Document.Body.InnerHTML = txtOutput
End Sub
Sub writeTxt(txtIP, txtRes, txtMAC)
strPad = " "
CSVFile.WriteLine txtRes & "," & txtIP & "," & txtMAC
ResFile.WriteLine (txtIP & Left(strPad, 21 - Len(txtIP)) & txtRes & Left(strPad,29 - Len(txtRes)) & txtMAC)
End Sub
Sub discoverDevice(strDeviceName, strIP)
showText(strDeviceName)
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strDeviceName & "\root\cimv2")
Set colNICS = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem in colNICS
writeTxt strIP, strDeviceName, objItem.MACAddress
Next
End Sub
2 Spice ups
levi
(levi.lohrman)
2
Will this only work with AD, or can it be used in a workgroup as well?
martin9700
(Martin9700)
3
It should work fine for a workgroup but you’ll have to watch permissions. Since you’re not running AD you’d need to log in with a username common to all workstations in the workgroup (administrator) and that username would have to have a common password.
So Martin I hate to show my ignorance but how do I use this? Do I add it in Spiceworks and run it somehow? or Do I just run it cmd line? Thanks in advance.
justin-e
(_Justin_)
5
You just save it as a VBScript (.vbs) and can run it by double clicking or from command line. It opens an IE window for display.
pabolu
(pradeep.mctip)
6
can u add the user details who is logged in and to which DC ? thanks in advance
This script is great. I was wondering as I am NEW to VBScripts is there a way to take this same script and incorporate the Serial Number and Vendor and model number. I think it that infromation would come from something like this. Select * from Win32_computersystemproduct
Wow, I forgot I even wrote this script! Yeah, you’ll need to load the Select statement into a variable, just like in line 132, then modify the writeTXT line and the writeTXT sub. You might also want to look at the Network Discovery script, it might have more the information you hunting for: http://community.spiceworks.com/scripts/show/30-network-discovery I also have a Powershell version of ND brewing, but not ready to be released yet.
Greetings Martin, This script is great. But can you modify it to list its interface name and the second interface IP as well? All of our servers have 2 interfaces with different IP network. When I scan 192.168.16.1 network, I get the mac addresses of all servers. Since they will have 2 interface Mac addresses its hard to recognize which interface they belong to. Here is how it looks: 192.168.16.3----------Prod-Server-1.IN.DTS----------00:50:56:97:00:55 192.168.16.3----------Prod-Server-1.IN.DTS----------00:50:56:97:00:36 192.168.16.4----------Prod-Server-2.IN.DTS----------00:50:56:97:00:45 192.168.16.4----------Prod-Server-2.IN.DTS----------00:50:56:97:00:93 The second and fourth mac address here is the local area connection 2 interface and its acutall IP is 192.168.17.x network. If you can modify it please make it look like this and its interface name at the end if possible? 192.168.16.3----------Prod-Server-1.IN.DTS----------00:50:56:97:00:55 192.168.17.3----------Prod-Server-1.IN.DTS----------00:50:56:97:00:36 192.168.16.4----------Prod-Server-2.IN.DTS----------00:50:56:97:00:45 192.168.17.4----------Prod-Server-2.IN.DTS----------00:50:56:97:00:93 Please do let me know if can do that or not? If not, I’ll get the mac addresses manually for now. Regards!
I don’t really work in vbScript anymore and I haven’t looked at this script in 3 years
Post your question over in the Powershell forum and I bet we can work something up pretty easily!
jalley
(jalley)
11
I am trying to use a hostname file, that lists the names of the hosts, to generate a live list of IP and MAC Addresses for the hosts in the hostname. How would I modify this script to do that?
Sorry, don’t really know what you’re asking for there. Maybe you can post something in the IT Programming forum and be a bit more clear? It’s tough to do things here in comments. http://community.spiceworks.com/programming/it-programming (Don’t forget to read the announcement thread too)
Awesome script, and I like that it creates a spreadsheet too
It duplicated some entries, but no biggie for our small network 