Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VBScript - call function and create IP address range

Posted on 2012-09-13
8
Medium Priority
?
1,285 Views
Last Modified: 2012-09-20
Rather than list each IP address of an IP scope individually in an array, how could I implement a function that would effectively create the array or dictionary object based on an explicitly stated beggining IP address and ending IP address?

For example, this is what I have now:
arrComputers = Array( _
"10.108.8.100","10.108.8.101","10.108.8.102","10.108.8.103","10.108.8.104","10.108.8.105","10.108.8.106","10.108.8.107",..."10.108.8.254")

This gets rather messy when using multiple arrays that consist of large IP scopes. It's also error prone. I'd like to just say SCOPE1 = 10.108.8.100 - 10.108.8.254 and have a function do the work. Does that make sense?
0
Comment
Question by:jcb431
8 Comments
 
LVL 11

Expert Comment

by:netballi
ID: 38396459
Following code should do the trick as long as the following assumption are taken care of

1.

input format should be strictly followed

2.

First three octate remains the same.

Code
=======================================================
Dim NUM, a,b,c,d,a1,b1,c1,d1,a2,b2,c2,d2
NUM=InputBox("Enter the Scope as XXX.XXX.XXX.XXX - XXX.XXX.XXX.XXX")
numarray=Split(NUM,"-")
oct1=Split(numarray(0),".")
oct2=Split(numarray(1),".")

for i = oct1(3) to oct2(3)
Wscript.echo oct1(0)&"."&oct1(1)&"."&oct1(2)&"."&i
Next

===============================================================

Just copy the code in a notepad and save the file as vbscript to test it latter you can use it in your own script.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 38397342
==> SCOPE1 = 10.108.8.100 - 10.108.8.254

So how do you want the "list" if IPs returned, in a single string as you show above, or an array, or what?  What do you do with the set of IPs once they are generated?

~bp
0
 

Author Comment

by:jcb431
ID: 38398850
Bill - this is the same code that I posted under the question "VBScript-- select from multiple arrays". I left a comment there for you.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:jcb431
ID: 38406198
An array, because the script performs multiple WMI queries against each of the IP addresses and writes the output to a spreadsheet.

Hard coding the IP addresses for 12 subnets creates a lengthy mess that is difficult to debug if you mistype or make an omission.

I also need to be able to specify which subnets I want to execute the script against, either one, many, or all subnets.
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 38415990
Here is a way to have your script run through your IP address ranges.  I don't know about your scripts that were mentioned above so I am going with just the bare essentials here.  

Edit the script with your address ranges.  See lines 12-15 for examples on how this looks.  

Edit line 17 so that the filter is pulling the address ranges you need.  The block of lines 20-24 should be your code that does something with the IP addresses.

I suppose it would be helpful if you could post your code so that everything can be merged in to one file.

CONST MIN = 0 'For building IP address range
CONST MAX = 255 'For building IP address range
CONST adVarChar = 200 'For building recordset
CONST varCharMaxCharacters = 255 'For building recordset

DIM DataList
SET DataList = CreateObject("ADOR.Recordset") 'Create a recordset
DataList.Fields.Append "Group", adVarChar, VarCharMaxCharacters 'Field for group names
DataList.Fields.Append "IP", adVarChar, VarCharMaxCharacters 'Field for IP addresses
DataList.Open

CALL setAddresses("Test1", "192.168.1.100", "192.168.1.200") 'Add to the list of IP addresses
CALL setAddresses("Test2", "192.168.2.200", "192.168.4.100") 'Add to the list of IP addresses
CALL setAddresses("Test3", "10.1.254.100", "10.2.3.50") 'Add to the list of IP addresses
CALL setAddresses("Test4", "10.255.254.100", "11.0.0.50") 'Add to the list of IP addresses

DataList.Filter = "Group = 'Test1' OR Group = 'Test3'" 'Create a filter - here I am filtering by group names: 'Test1' OR 'Test3'
DataList.MoveFirst 'Move to the first data record in the array/recordset
Do Until DataList.EOF
	'You code would follow.
	'This Do Until loop will behave like your array loop.  Whatever you had there should fit here.
	'Access the IP address from the recordset by using -> DataList.Fields.Item("IP")
	'Perhaps you already have a function that you pass an IP address to. In that case you could use
	'something like this -> CALL myFunction(DataList.Fields.Item("IP"))
	Wscript.Echo DataList.Fields.Item("Group") & vbTab & DataList.Fields.Item("IP")
	
	DataList.MoveNext 'Move to the next record in the recordset
Loop

'setAddresses(name, lowerRange, upperRange)
'name = a name to assign the IP address to for further filtering
'lowerRange = the IP address which is the lower of the two
'upperRange = the IP address which is the greater of the two
'example: CALL setAddresses("Test1", "192.168.1.100", "192.168.1.103")
'This will produce records;
'	Test1	192.168.1.100
'	Test1	192.168.1.101
'	Test1	192.168.1.102
'	Test1	192.168.1.103
function setAddresses(name, lowerRange, upperRange)
	octetLower = Split(lowerRange,".")
	octetUpper = Split(upperRange,".")
	
	aLower = octetLower(0)
	bLower = octetLower(1)
	cLower = octetLower(2)
	dLower = octetLower(3)
	
	for a = aLower to MAX
		for b = bLower to MAX
			for c = cLower to MAX
				for d = dLower to MAX - 1
					strIP = a & "." & b & "." & c & "." & d
					DataList.AddNew
					DataList("Group") = name
					DataList("IP") = strIP
					DataList.Update
					if (strIP = upperRange) then
						exit for
					end if
				next
				dLower = MIN + 1
				if (strIP = upperRange) then
					exit for
				end if
			next
			cLower = MIN
			if (strIP = upperRange) then
				exit for
			end if
		next
		bLower = MIN
		if (strIP = upperRange) then
			exit for
		end if
	next
end function

Open in new window

0
 

Author Comment

by:jcb431
ID: 38418151
Here is the code. What I'd like to do is have the option (perhaps an input box) that prompts me for which subnet I want to execute the code against or if I want to run the script against all of the subnets.
master-sample.txt
0
 
LVL 14

Accepted Solution

by:
rejoinder earned 2000 total points
ID: 38418863
Thanks for posting that code.

Here is everything in one file.  When launched, you will be asked what sites to poll.  Either leave it with "All" or enter sites by name seperated with a comma.

Between lines 13-25 are where you will enter your subnet details.  The script will build the list of IP addresses based on that information.  Say for example you don't want to bother polling every IP in 10.108.8.X you would write the line such as this...
CALL setAddresses("Site A", "10.108.8.X", "10.108.8.100", "10.108.8.200")
That way you are not wasting time trying to connect to switches, printers and other network gear.

CONST MIN = 0 'For building IP address range
CONST MAX = 255 'For building IP address range
CONST adVarChar = 200 'For building recordset
CONST varCharMaxCharacters = 255 'For building recordset

DIM DataList
SET DataList = CreateObject("ADOR.Recordset") 'Create a recordset
DataList.Fields.Append "Group", adVarChar, VarCharMaxCharacters 'Field for group names
DataList.Fields.Append "Subnet", adVarChar, VarCharMaxCharacters 'Field for subnet name
DataList.Fields.Append "IP", adVarChar, VarCharMaxCharacters 'Field for IP addresses
DataList.Open

CALL setAddresses("Site A", "10.108.8.X", "10.108.8.1", "10.108.8.254")
CALL setAddresses("Site A", "10.108.9.X", "10.108.9.1", "10.108.9.254")
CALL setAddresses("Site B", "10.108.28.X", "10.108.28.1", "10.108.28.254")
CALL setAddresses("Site C", "10.108.27.X", "10.108.27.1", "10.108.27.254")
CALL setAddresses("Site D", "10.108.18.X", "10.108.18.1", "10.108.18.254")
CALL setAddresses("Site E", "10.93.5.X", "10.93.5.1", "10.93.5.254")
CALL setAddresses("Site F", "10.93.30.X", "10.93.30.1", "10.93.30.254")
CALL setAddresses("Site G", "10.108.25.X", "10.108.25.1", "10.108.25.254")
CALL setAddresses("Site H", "10.93.82.X", "10.93.82.1", "10.93.82.254")
CALL setAddresses("Site I", "10.108.5.X", "10.108.5.1", "10.108.5.254")
CALL setAddresses("Site J", "10.108.20.X", "10.108.20.1", "10.108.20.254")
CALL setAddresses("Site K", "10.108.16.X", "10.108.16.1", "10.108.16.254")
CALL setAddresses("Site L", "10.209.120.X", "10.209.120.1", "10.209.120.254")

strFilter = InputBox("Enter a site to run the script against." & vbCRLF & _
	"Use:" & vbCRLF & _
	"         All - poll all sites and subnets" & vbCRLF & _
	"         Site A, Site B, Site L - comma delimited sites" & vbCRLF _
	,"Gather Computer Info","All") 

'setAddresses(name, subnet, lowerRange, upperRange)
'name = a name to assign the IP address to for further filtering
'subnet = a title to add to worksheet
'lowerRange = the IP address which is the lower of the two
'upperRange = the IP address which is the greater of the two
'example: CALL setAddresses("Test1", "192.168.1.X", "192.168.1.100", "192.168.1.200")
function setAddresses(name, subnet, lowerRange, upperRange)
	octetLower = Split(lowerRange,".")
	octetUpper = Split(upperRange,".")
	
	aLower = octetLower(0)
	bLower = octetLower(1)
	cLower = octetLower(2)
	dLower = octetLower(3)
	
	for a = aLower to MAX
		for b = bLower to MAX
			for c = cLower to MAX
				for d = dLower to MAX - 1
					strIP = a & "." & b & "." & c & "." & d
					DataList.AddNew
					DataList("Group") = name
					DataList("Subnet") = subnet
					DataList("IP") = strIP
					DataList.Update
					if (strIP = upperRange) then
						exit for
					end if
				next
				dLower = MIN + 1
				if (strIP = upperRange) then
					exit for
				end if
			next
			cLower = MIN
			if (strIP = upperRange) then
				exit for
			end if
		next
		bLower = MIN
		if (strIP = upperRange) then
			exit for
		end if
	next
end function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dtmDDate = Date
strMonth = Month(Date)
strDay = Day(Date)
strYear = Right(Year(Date),4)
strExcelFileName = "C:\Temp2\TK\Logs\ScanSUBNET-" & strMonth & "-" & strDay & "-" & strYear & ".xlsx"
Set objExcel = CreateObject("Excel.Application")
CONST xlUp = -4162
CONST xlCalculationManual = -4135
CONST xlCalculationAutomatic = -4105
Set objWB = objExcel.Workbooks.Add
Set objSheet = objWB.Sheets(1)
objExcel.Visible = True
'objExcel.Calculaton = xlCalculationManual
objExcel.DisplayAlerts = False
While objWB.Sheets.Count > 1
	objWB.Sheets(objWB.Sheets.Count).Delete
Wend
objExcel.DisplayAlerts = True
strCombinedFilter = ""
if uCase(strFilter) <> "ALL" then
	arrFilter = split(strFilter,",")
	for each strSiteName in arrFilter
		if strCombinedFilter <> "" then
			strCombinedFilter = strCombinedFilter & " OR "
		end if
		strCombinedFilter = strCombinedFilter & "Group = '" & strSiteName & "'"
	next
end if
DataList.Filter = strCombinedFilter
DataList.MoveFirst
Do Until DataList.EOF
	strSubNet = DataList.Fields.Item("Subnet")
	strComputer = DataList.Fields.Item("IP")
	If objSheet.Name = "Sheet1" Then
		objSheet.Name = strSubNet
		objSheet.Cells(1, 1) = "Computer Name"
		objSheet.Cells(1, 2) = "Make"
		objSheet.Cells(1, 3) = "Model"
		objSheet.Cells(1, 4) = "Serial Number"
		objSheet.cells(1, 5) = "Operating System"
		objSheet.cells(1, 6) = "Service Pack"
		objSheet.cells(1, 7) = "Date Imaged"
		objSheet.cells(1, 8) = "Build"
		objSheet.cells(1, 9) = "Tumbleweed Revision"
		objSheet.Rows("1:1").Font.Bold = True
	ElseIf objSheet.Name <> strSubNet Then
		objWB.Sheets.Add , objWB.Sheets(objWB.Sheets.Count)
		Set objSheet = objWB.Sheets(objWB.Sheets.Count)
		objSheet.Name = strSubNet
		objSheet.Cells(1, 1) = "Computer Name"
		objSheet.Cells(1, 2) = "Make"
		objSheet.Cells(1, 3) = "Model"
		objSheet.Cells(1, 4) = "Serial Number"
		objSheet.cells(1, 5) = "Operating System"
		objSheet.cells(1, 6) = "Service Pack"
		objSheet.cells(1, 7) = "Date Imaged"
		objSheet.cells(1, 8) = "Build"
		objSheet.cells(1, 9) = "Tumbleweed Revision"
		objSheet.Rows("1:1").Font.Bold = True
	End If
	If Ping(strComputer) = True Then
		On Error Resume Next
		Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
		Set colComputer = objWMIService.ExecQuery ("Select Name,Manufacturer,Model,TotalPhysicalMemory from Win32_ComputerSystem")
		Set colSerial = objWMIService.ExecQuery ("Select IdentifyingNumber from Win32_ComputerSystemProduct")
		Set colDescription = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem",,48)
		If Err.Number = 0 Then
			For Each objComputer In colComputer
		  		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row + 1, 1).Value = objComputer.Name
				objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 2).Value = objComputer.Manufacturer
				objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 3).Value = objComputer.Model
			Next
			For Each objItem In colSerial
				objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 4).Value = objItem.IdentifyingNumber
			Next
			For Each objItem in colDescription
				dtmDate = objItem.InstallDate
				strOS_InstallDate = WMIDateStringToDate(dtmDate)
				objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 5).Value = objItem.Caption
				objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 6).Value = objItem.CSDVersion
				objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 7).Value = strOS_InstallDate
			Next
			const HKEY_LOCAL_MACHINE = &H80000002	
			Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
			strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\OEMInformation"
			strValueName = "Model"
			oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue1
			strKeyPath = "SOFTWARE\Tumbleweed\Desktop Validator\ConfigExpImp"
			strValueName = "Path"
			oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue2
			For Each objItem In strValue1
				objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 8).Value = strValue1
			Next
			For Each objItem In strValue2
				objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 9).Value = strValue2
			Next
		Else
			objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row + 1, 1).Value = strComputer
			objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 2).Value = "WMI ERROR"
			objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 3).Value = "WMI ERROR"
			objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 4).Value = "WMI ERROR"
			objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 5).Value = "WMI ERROR"
			objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 6).Value = "WMI ERROR"
			objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 7).Value = "WMI ERROR"
			objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 8).Value = "WMI ERROR"
			objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 9).Value = "WMI ERROR"
		End If
	Else
		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row + 1, 1).Value = strComputer
		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 2).Value = "OFFLINE"
		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 3).Value = "OFFLINE"
		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 4).Value = "OFFLINE"
		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 5).Value = "OFFLINE"
		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 6).Value = "OFFLINE"
		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 7).Value = "OFFLINE"
		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 8).Value = "OFFLINE"
		objSheet.Cells(objSheet.Cells(65536, 1).End(xlUp).Row, 9).Value = "OFFLINE"
	End If
	DataList.MoveNext
Loop
'objExcel.Calculaton = xlCalculationAutomatic
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
objWB.SaveAs(strExcelFileName)
MsgBox "Finished"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function WMIDateStringToDate(dtmDate)
	WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) & " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate, 13, 2))
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ConvertSize(Size) 
	Do While InStr(Size,",") 'Remove commas from size 
		CommaLocate = InStr(Size,",") 
		Size = Mid(Size,1,CommaLocate - 1) & Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate) 
	Loop 
	Suffix = " Bytes" 
	If Size >= 1024 Then suffix = " KB" 
	If Size >= 1048576 Then suffix = " MB" 
	If Size >= 1073741824 Then suffix = " GB" 
	If Size >= 1099511627776 Then suffix = " TB"
	Select Case Suffix 
		Case " KB" Size = Round(Size / 1024, 1) 
		Case " MB" Size = Round(Size / 1048576, 1) 
		Case " GB" Size = Round(Size / 1073741824, 1) 
		Case " TB" Size = Round(Size / 1099511627776, 1) 
	End Select 
	ConvertSize = Size & Suffix 
End Function

Open in new window

0
 

Author Closing Comment

by:jcb431
ID: 38419006
Outstanding!!! I really appreciate your work! Much, much, better than the way it was before. Thank you!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question