Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Use VB Script to list a range of IP addresses

Post image for Use VB Script to list a range of IP addresses

by Steve Wiseman on April 18, 2012 · 2 comments

in Scripts,Tips


.

I got a question from Murali:

“Suppose if I have a 100+ work group machines in the same network how can I run a script remotely against them? I would like to enter a range of IP address for my selection”

This is a tough one since Workgroup computers (Systems not joined to a domain) are not configured to allow this. This means we need to make sure:

-All the computers have the same administrator username and password

-File and printer sharing is opened in the firewall

-On Windows XP systems, simple file sharing must be disabled

Finally, none of the systems can be the “Home” edition of windows.

Why? Because Microsoft removes some network functionality that would make our task impossible.

Here are a few good guides on getting file and printer sharing open:

Windows XP File And Printer Sharing

Vista / 2008 File And Printer Sharing

Windows 7 / 2008 R2 File And Printer Sharing

OK, Now that we got that covered, how do we get our list of computers in the script?

First, our script will take two arguments – the starting IP address, and the ending IP address. Then it will loop through all the ip addresses between them.

We do this by pulling each octet of the IP:

IPList = Split(StartIP, ".")

iS1 = cint(IPList(0))
iS2 = cint(IPList(1))
iS3 = cint(IPList(2))
iS4 = cint(IPList(3))

Then we run it through a for loop that calls a function for each IP we calculate. In the script I built, I called it ExecuteAction. For a test, I simply put an echo statement:

Function ExecuteAction(HostName)
WScript.Echo "Host: " & HostName
End Function

Running it from the command line you can see it listed all of the IP addresses in the range I gave it (10.10.10.7 – 10.10.10.15):

IP Range List

So now we have a script that can enumerate our IP addresses. Just change the body of “ExecuteAction” and it will run against the range of IP addresses you provide.

You can get a copy of my script from here:

EnumerateIP.dat

Just make sure to rename it to .vbs after downloading

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 2 comments… read them below or add one }

1 Angus S-F April 18, 2012 at 3:34 pm

Steve,

This may be a Silly Question. Is there reason I’m not seeing why you coded the loop this way?

for iC1=iS1 to 255
for iC2=iS2 to 255
for iC3=iS3 to 255
for iC4=iS4 to 255
ExecuteAction(iC1 & “.” & iC2 & “.” & iC3 & “.” & iC4)
if ((iC1>=iE1) and (iC2>=iE2) and (iC3>=iE3) and (iC4>=iE4)) then
iC1=255
iC2=255
iC3=255
iC4=255
end if
next
iS4=1
next
iS3=1
next
iS2=1
next

I tried simplifying it to this:

for iC1=iS1 to iE1
for iC2=iS2 to iE2
for iC3=iS3 to iE3
for iC4=iS4 to iE4
ExecuteAction(iC1 & “.” & iC2 & “.” & iC3 & “.” & iC4)
next
next
next
next

and it worked fine. Are there some special cases I’m not seeing where you need to do it the original way?

2 Steve Wiseman April 23, 2012 at 10:43 am

Yes…there are some special cases.

For example with your code if you go from 10.10.29.129 – 10.10.30.240

Your loop will go up to 10.10.29.240 and then instead of going to 10.10.29.241 it will go to 10.10.30.129

Having the loop coded in this way will miss a chunk of IPs in these type of situations 🙂

Leave a Comment

Category Links - Windows Forum - Exchange Forum