473,320 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

looping through IP address range

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
14 5965

Hi Network,
What is your application?

Robin
May 18 '07 #2

"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

"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

"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
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
----- 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
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
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
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
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
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

----- 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chandrashekar Tippur | last post by:
Pros, I was wondering if there is a API for generating ip address range from a given subnet mask (VLSM). Thanks, Shekar
1
by: Brent | last post by:
I store IP addresses using the INET_ATON function, e.g. INSERT INTO myTable(IP_Address) Values (INET_ATON(123.123.33.12)); I would like to create a daily report that groups IP addresses by a...
9
by: John J. Hughes II | last post by:
I have a system where my software sits on one server and interacts with another server running MS SQL. My software recieves connects via a socket layer on the internet and does it's thing with the...
2
by: Chumma Dede | last post by:
Hi, I have a page called Page1.aspx on an ASP.Net 1.1 website which has Forms Authentication enabled. Role1 has been setup in the Web.config to have access to Page1.aspx as follows:...
1
by: yqyq22 | last post by:
hello, Just a simple question, I would like to define a network address range. How can i code this please? 10.0.0.0 - 10.255.255.255 (10/8 prefix) 172.16.0.0 - 172.31.255.255 ...
20
by: sethukr | last post by:
hi, i need to store a value to a particular memory location without having a variable. So, how can i access a 'memory address' without using variables?? Is it possible in C??? Plz, help...
3
by: Shyckymn | last post by:
Dudes, How can I make a GROUP BY function into an SQL instruction, to count how much machines by ip address range (until the 3rd range)? Ex: (Existent registers for a field called ip_addr) ...
69
by: Horacius ReX | last post by:
Hi, I have the following program structure: main ..... int A=5; int* ptr= A; (so at this point ptr stores address of A) ..... .....
10
by: themadjester | last post by:
This is weird, I know what an IP address conflict is, and how to avoid it, but this problem seems atypical - and apparently client side? Basically I have a router internet network, it is DHCP and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.