By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,963 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,963 IT Pros & Developers. It's quick & easy.

looping through IP address range

P: n/a
Hi all,

Does anyone have some code that shows an example of how to loop through a
range of IP addresses? I'm using text boxes to get a start and end value for
the range. I was thinking about using 4 nested for-next loops, but I seem to
be having trouble working out the logic to validate the octets in the
beginning and ending address so the range works correctly.

i.e. 172.16.1.1/172.20.1.1 should loop 172-172,16-20,1-254,1-254
172.16.1.1/172.16.1.254 should loop 172-172,16-16,1-1,1-254
172.16.1.5/172.16.1.15 should loop 172-172,16-16,1-1,5-15

and so on...

Is there an easier way to do this without for-next loops?

Thanks,

ne.
May 18 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a

Hi Network,
What is your application?

Robin
May 18 '07 #2

P: n/a

"Robin Tucker" <rt******@removehotmail.comwrote in message
news:f2*******************@news.demon.co.uk...
>
Hi Network,
What is your application?
I'm writing a little app to scan our network address by address. I want to
be able to specify a start and end address so that the app can touch each
address in between, inclusive of the end addresses.

It may well be that that for-next thing is the wrong approach, but I've not
come up with anything better as of yet. I've been going through
system.net.ip* and system.networkinformation, but I don't see anything that
looks promising.
May 18 '07 #3

P: n/a

"NetworkElf" <Ne********@nospam.nospamwrote in message
news:OK**************@TK2MSFTNGP06.phx.gbl...
>
"Robin Tucker" <rt******@removehotmail.comwrote in message
news:f2*******************@news.demon.co.uk...
>>
Hi Network,
What is your application?

I'm writing a little app to scan our network address by address. I want to
be able to specify a start and end address so that the app can touch each
address in between, inclusive of the end addresses.

It may well be that that for-next thing is the wrong approach, but I've
not come up with anything better as of yet. I've been going through
system.net.ip* and system.networkinformation, but I don't see anything
that looks promising.
Whats wrong with the nested loops? Loosk like it would work fine:

Dim Oct1 As Integer = 0
Dim Oct2 As Integer = 0
Dim Oct3 As Integer = 0
Dim Oct4 As Integer = 0

For Oct1 = 0 to 255
For Oct2 = 0 to 255
For Oct3 = 0 to 255
For Oct4 = 0 to 255
Dim ipAddress As New StringBuilder
ipAddress.Append(Oct1.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct2.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct3.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct4.ToString)
SubToScanAddress(ipAddress.ToString)
Next Oct4
Next Oct3
Next Oct2
Next Oct1

You might want to do some filtering on things like the addresses used for
multicast and broadcast stuff but this would seem to work.

>

May 19 '07 #4

P: n/a

"Ray Cassick" <rc******@enterprocity.comwrote in message
news:%2***************@TK2MSFTNGP03.phx.gbl...
>
"NetworkElf" <Ne********@nospam.nospamwrote in message
news:OK**************@TK2MSFTNGP06.phx.gbl...
>>
"Robin Tucker" <rt******@removehotmail.comwrote in message
news:f2*******************@news.demon.co.uk...
>>>
Hi Network,
What is your application?

I'm writing a little app to scan our network address by address. I want
to be able to specify a start and end address so that the app can touch
each address in between, inclusive of the end addresses.

It may well be that that for-next thing is the wrong approach, but I've
not come up with anything better as of yet. I've been going through
system.net.ip* and system.networkinformation, but I don't see anything
that looks promising.

Whats wrong with the nested loops? Loosk like it would work fine:

Dim Oct1 As Integer = 0
Dim Oct2 As Integer = 0
Dim Oct3 As Integer = 0
Dim Oct4 As Integer = 0

For Oct1 = 0 to 255
For Oct2 = 0 to 255
For Oct3 = 0 to 255
For Oct4 = 0 to 255
Dim ipAddress As New StringBuilder
ipAddress.Append(Oct1.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct2.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct3.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct4.ToString)
SubToScanAddress(ipAddress.ToString)
Next Oct4
Next Oct3
Next Oct2
Next Oct1

You might want to do some filtering on things like the addresses used for
multicast and broadcast stuff but this would seem to work.

Oopps.. forgot one of your requirements, to pick a starting point...

Dim Oct1 As Integer = 0
Dim Oct2 As Integer = 0
Dim Oct3 As Integer = 0
Dim Oct4 As Integer = 0

Dim Oct1Start As Integer = 0
Dim Oct2Start As Integer = 0
Dim Oct3Start As Integer = 0
Dim Oct4Start As Integer = 0

For Oct1 = Oct1Start to 255
For Oct2 = Oct2Start to 255
For Oct3 = Oct3Start to 255
For Oct4 = Oct4Start to 255
Dim ipAddress As New StringBuilder
ipAddress.Append(Oct1.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct2.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct3.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct4.ToString)
SubToScanAddress(ipAddress.ToString)
Next Oct4
Next Oct3
Next Oct2
Next Oct1
May 19 '07 #5

P: n/a
Thanks for Ray's informative input.

Hi Ne,

Here is a former thread discussing on calculating IP address through a
network address and subnet mask:

http://groups.google.com/group/micro...rk/browse_thre
ad/thread/580b74b01ab29add/faa701a914673dba

you can also have a look to see whether you can get some additional hint
there.

Hope this also helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 21 '07 #6

P: n/a
----- Original Message -----
From: "Ray Cassick" <rc******@enterprocity.com>
Newsgroups: microsoft.public.dotnet.languages.vb
Sent: Saturday, May 19, 2007 2:09 PM
Subject: Re: looping through IP address range

>
"Ray Cassick" <rc******@enterprocity.comwrote in message
news:%2***************@TK2MSFTNGP03.phx.gbl...
>>
"NetworkElf" <Ne********@nospam.nospamwrote in message
news:OK**************@TK2MSFTNGP06.phx.gbl...
>>>
"Robin Tucker" <rt******@removehotmail.comwrote in message
news:f2*******************@news.demon.co.uk...

Hi Network,
What is your application?

I'm writing a little app to scan our network address by address. I want
to be able to specify a start and end address so that the app can touch
each address in between, inclusive of the end addresses.

It may well be that that for-next thing is the wrong approach, but I've
not come up with anything better as of yet. I've been going through
system.net.ip* and system.networkinformation, but I don't see anything
that looks promising.

Whats wrong with the nested loops? Loosk like it would work fine:
Nothing is wrong with them, per se. I just thought that there might be a
better way to handle it that I didn't know about.
>
Oopps.. forgot one of your requirements, to pick a starting point...

Dim Oct1 As Integer = 0
Dim Oct2 As Integer = 0
Dim Oct3 As Integer = 0
Dim Oct4 As Integer = 0

Dim Oct1Start As Integer = 0
Dim Oct2Start As Integer = 0
Dim Oct3Start As Integer = 0
Dim Oct4Start As Integer = 0

For Oct1 = Oct1Start to 255
For Oct2 = Oct2Start to 255
For Oct3 = Oct3Start to 255
For Oct4 = Oct4Start to 255
Dim ipAddress As New StringBuilder
ipAddress.Append(Oct1.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct2.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct3.ToString)
ipAddress.Append(".")
ipAddress.Append(Oct4.ToString)
SubToScanAddress(ipAddress.ToString)
Next Oct4
Next Oct3
Next Oct2
Next Oct1
This is pretty close to what I had. The problem I ran into was building in
all of the logic that I needed. For example, if the start is 172.16.2.1 and
the end is 172.16.3.100 then the end of the range for the last octet would
equal 254 when the 3rd octet equals 2 and, when the 3rd octet equals 3, the
end of the range would be 100.

This is where I've been running into problems getting the kinks out of my
loop logic, thus my original post looking for ideas. On the surface, it
sounds like it should be straightforward, but it's not shaping up that way
thus far.

Am I making this harder than it really needs to be?

Thanks for your input!

ne.
May 22 '07 #7

P: n/a
Hi NE,

For the following problem you mentioned:

======
This is pretty close to what I had. The problem I ran into was building in
all of the logic that I needed. For example, if the start is 172.16.2.1 and
the end is 172.16.3.100 then the end of the range for the last octet would
equal 254 when the 3rd octet equals 2 and, when the 3rd octet equals 3, the
end of the range would be 100.
======

I think the original code logic still work, however, you need to add an
additional flag variable to indicate whether the outer loop is arriving the
last number, if so, the inner loop will choose the end number of the last
byte as the upper bound , other wise, use 254 as upper bound. How do you
think?
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 23 '07 #8

P: n/a
NetworkElf wrote:
Does anyone have some code that shows an example of how to loop
through a range of IP addresses? I'm using text boxes to get a start
and end value for the range. I was thinking about using 4 nested
for-next loops, but I seem to be having trouble working out the logic
to validate the octets in the beginning and ending address so the
range works correctly.
i.e. 172.16.1.1/172.20.1.1 should loop 172-172,16-20,1-254,1-254
172.16.1.1/172.16.1.254 should loop 172-172,16-16,1-1,1-254
172.16.1.5/172.16.1.15 should loop 172-172,16-16,1-1,5-15

and so on...

Is there an easier way to do this without for-next loops?
Um, can't you convert them into simple numbers and use one loop from the
first to the last, converting each integer back into the four parts?
Private Function ippify(ByVal n As Int64) As String
Return (n >24).ToString & "." & ((n And &HFF0000) >16).ToString &
"." & ((n And &HFF00) >8).ToString & "." & (n And &HFF).ToString
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim a1 As String = "1.2.3.1"
Dim a2 As String = "1.2.4.255"

Dim n1, n2 As Int64 ' I have no Uint32

' convert string representations of IP addresses to int64
Dim p() As String = a1.Split(".")
For i As Integer = 0 To UBound(p)
n1 = 256 * n1 + p(i)
Next
p = a2.Split(".")
For i As Integer = 0 To UBound(p)
n2 = 256 * n2 + p(i)
Next

' do something with the range of addresses
Dim s As New StringBuilder
For i As Int64 = n1 To n2
s.Append(ippify(i) & vbCrLf)
Next
' Assuming you have a textbox named TextBox1
TextBox1.Text = s.ToString
End Sub

Andrew
May 23 '07 #9

P: n/a
For-next loops are the easiest but the the trick is to correctly fudge the
range (substitute 1 for 254 when the the last 2 nodes of the start address
are the same as the last 2 nodes of the end address).

Sub LoopAddressRange(ByVal start As String, ByVal finish As String)

Dim _start As String() = start.Split("."c)

Dim _finish As String() = finish.Split("."c)

If _start(2) = "1" AndAlso _finish(2) = "1" AndAlso _start(3) = "1"
AndAlso _finish(3) = "1" Then
_finish(2) = "254"
_finish(3) = "254"
End If

For _node0 As Integer = Integer.Parse(_start(0)) To
Integer.Parse(_finish(0))
For _node1 As Integer = Integer.Parse(_start(1)) To
Integer.Parse(_finish(1))
For _node2 As Integer = Integer.Parse(_start(2)) To
Integer.Parse(_finish(2))
For _node3 As Integer = Integer.Parse(_start(3)) To
Integer.Parse(_finish(3))
Console.Writeline("{0}.{1}.{2}.{3}", _node0, _node1, _node2,
_node3)
Next
Next
Next
Next

End Sub

LoopAddressRange("172.16.1.1", "172.20.1.1")

LoopAddressRange("172.16.1.1", "172.16.1.254")

LoopAddressRange("172.16.1.5", "172.16.1.15")
"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:uJ****************@TK2MSFTNGP06.phx.gbl...
NetworkElf wrote:
>Does anyone have some code that shows an example of how to loop
through a range of IP addresses? I'm using text boxes to get a start
and end value for the range. I was thinking about using 4 nested
for-next loops, but I seem to be having trouble working out the logic
to validate the octets in the beginning and ending address so the
range works correctly.
i.e. 172.16.1.1/172.20.1.1 should loop 172-172,16-20,1-254,1-254
172.16.1.1/172.16.1.254 should loop 172-172,16-16,1-1,1-254
172.16.1.5/172.16.1.15 should loop 172-172,16-16,1-1,5-15

and so on...

Is there an easier way to do this without for-next loops?

Um, can't you convert them into simple numbers and use one loop from the
first to the last, converting each integer back into the four parts?
Private Function ippify(ByVal n As Int64) As String
Return (n >24).ToString & "." & ((n And &HFF0000) >16).ToString &
"." & ((n And &HFF00) >8).ToString & "." & (n And &HFF).ToString
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim a1 As String = "1.2.3.1"
Dim a2 As String = "1.2.4.255"

Dim n1, n2 As Int64 ' I have no Uint32

' convert string representations of IP addresses to int64
Dim p() As String = a1.Split(".")
For i As Integer = 0 To UBound(p)
n1 = 256 * n1 + p(i)
Next
p = a2.Split(".")
For i As Integer = 0 To UBound(p)
n2 = 256 * n2 + p(i)
Next

' do something with the range of addresses
Dim s As New StringBuilder
For i As Int64 = n1 To n2
s.Append(ippify(i) & vbCrLf)
Next
' Assuming you have a textbox named TextBox1
TextBox1.Text = s.ToString
End Sub

Andrew
May 23 '07 #10

P: n/a
Stephany Young wrote:
For-next loops are the easiest but the the trick is to correctly
fudge the range (substitute 1 for 254 when the the last 2 nodes of
the start address are the same as the last 2 nodes of the end
address).
Hmmm, I have a suspicion the OP's first example contained a typo where the
last two octets of the second IP address should have been 254:
i.e. 172.16.1.1/172.20.1.1 should loop 172-172,16-20,1-254,1-254
But comparing it with other examples, maybe it means it should loop
172.16-19.1-254.1-254
and end with a final value of
172.20.1.1
?
172.16.1.1/172.16.1.254 should loop 172-172,16-16,1-1,1-254
172.16.1.5/172.16.1.15 should loop 172-172,16-16,1-1,5-15
And would have been clearer as:
172.16.1.1/172.20.254.254 should loop 172.16-20.1-254.1-254
172.16.1.1/172.16.1.254 should loop 172.16.1.1-254
172.16.1.5/172.16.1.15 should loop 172.16.1.5-15

I suspect the OP didn't like the appearance of so many loops; I could
suggest an if...then...goto oops, I mean while, loop instead:

Dim j As Int64 = n1
While j <= n2
If (j And &HFF) <0 AndAlso (j And &HFF) <255 Then
s.Append(ippify(j) & vbCrLf)
End If
j += 1
End While

(OK, so I sneaked in the check for 0 and 255 that I should have put in
first-time round :-)

Let's wait and see what NetworkElf comes back with...

Andrew
May 23 '07 #11

P: n/a
Hi NE,

Have you got any progress on this issue or does the suggestion in last
reply helps a little? If there is still anything we can help, please feel
free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 28 '07 #12

P: n/a

----- Original Message -----
From: "Stephany Young" <noone@localhost>
Newsgroups: microsoft.public.dotnet.languages.vb
Sent: Wednesday, May 23, 2007 10:03 AM
Subject: Re: looping through IP address range

For-next loops are the easiest but the the trick is to correctly fudge the
range (substitute 1 for 254 when the the last 2 nodes of the start address
are the same as the last 2 nodes of the end address).

I'll try this. Thank you for the help!

ne.
May 29 '07 #13

P: n/a
Steven,

It's going slowly, but going. Our mail admin is MIA, so I've got a new hat.
Bleah.

Thanks to everyone that replied. I appreciate the input.

ne.

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:Wz*************@TK2MSFTNGHUB02.phx.gbl...
Hi NE,

Have you got any progress on this issue or does the suggestion in last
reply helps a little? If there is still anything we can help, please feel
free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no
rights.

May 29 '07 #14

P: n/a
Thanks for your reply Ne,

Sure. If you need further help later, please don't hesitate to post here.

Have a good day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

May 31 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.