Link to home
Start Free TrialLog in
Avatar of SnAkEhIpS
SnAkEhIpSFlag for United States of America

asked on

VBScript - call function and create IP address range

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?
Avatar of netballi
netballi
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of Bill Prew
Bill Prew

==> 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
Avatar of SnAkEhIpS

ASKER

Bill - this is the same code that I posted under the question "VBScript-- select from multiple arrays". I left a comment there for you.
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.
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

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
ASKER CERTIFIED SOLUTION
Avatar of rejoinder
rejoinder
Flag of Canada image

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
Outstanding!!! I really appreciate your work! Much, much, better than the way it was before. Thank you!