473,806 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6023

Hi Network,
What is your application?

Robin
May 18 '07 #2

"Robin Tucker" <rt******@remov ehotmail.comwro te in message
news:f2******** ***********@new s.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.networki nformation, but I don't see anything that
looks promising.
May 18 '07 #3

"NetworkElf " <Ne********@nos pam.nospamwrote in message
news:OK******** ******@TK2MSFTN GP06.phx.gbl...
>
"Robin Tucker" <rt******@remov ehotmail.comwro te in message
news:f2******** ***********@new s.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.networki nformation, 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.Appen d(Oct1.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct2.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct3.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct4.ToString )
SubToScanAddres s(ipAddress.ToS tring)
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******@enter procity.comwrot e in message
news:%2******** *******@TK2MSFT NGP03.phx.gbl.. .
>
"NetworkElf " <Ne********@nos pam.nospamwrote in message
news:OK******** ******@TK2MSFTN GP06.phx.gbl...
>>
"Robin Tucker" <rt******@remov ehotmail.comwro te in message
news:f2******* ************@ne ws.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.i p* and system.networki nformation, 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.Appen d(Oct1.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct2.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct3.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct4.ToString )
SubToScanAddres s(ipAddress.ToS tring)
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.Appen d(Oct1.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct2.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct3.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct4.ToString )
SubToScanAddres s(ipAddress.ToS tring)
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/580b74b01ab29ad d/faa701a914673db a

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******@enter procity.com>
Newsgroups: microsoft.publi c.dotnet.langua ges.vb
Sent: Saturday, May 19, 2007 2:09 PM
Subject: Re: looping through IP address range

>
"Ray Cassick" <rc******@enter procity.comwrot e in message
news:%2******** *******@TK2MSFT NGP03.phx.gbl.. .
>>
"NetworkElf " <Ne********@nos pam.nospamwrote in message
news:OK******* *******@TK2MSFT NGP06.phx.gbl.. .
>>>
"Robin Tucker" <rt******@remov ehotmail.comwro te in message
news:f2****** *************@n ews.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.networki nformation, 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.Appen d(Oct1.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct2.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct3.ToString )
ipAddress.Appen d(".")
ipAddress.Appen d(Oct4.ToString )
SubToScanAddres s(ipAddress.ToS tring)
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).ToStri ng
End Function

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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 LoopAddressRang e(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.Writeli ne("{0}.{1}.{2} .{3}", _node0, _node1, _node2,
_node3)
Next
Next
Next
Next

End Sub

LoopAddressRang e("172.16.1.1 ", "172.20.1.1 ")

LoopAddressRang e("172.16.1.1 ", "172.16.1.2 54")

LoopAddressRang e("172.16.1.5 ", "172.16.1.1 5")
"Andrew Morton" <ak*@in-press.co.uk.inv alidwrote in message
news:uJ******** ********@TK2MSF TNGP06.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).ToStri ng
End Function

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
9119
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
491
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 "dotted triad", e.g. (pseudo SQL) SELECT myTable.IP_Address FROM myTable GROUP BY 123.123.33.*;
9
1980
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 SQL. This seems to work fine. The problem is now I have a situation where the internet connect is on another server with firewall. When I listen to the ports on the server my software is running on it seems I can not listen to the internet....
2
2751
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: <location path = "Page1.aspx"> <system.web> <authorization>
1
1601
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 (172.16/12 prefix) 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) thanks a lot
20
2884
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 me..
3
1956
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) 172.17.17.1 172.17.17.2 172.17.17.3 172.18.196.12
69
3345
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
3360
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 the range 100-199 Now, to my knowledge myself and everyone else on the network is using DHCP (non techies). some things I have tried are: - I changed the address range from 100-199 to 150-199 to force my laptop to obtain a new IP (and not...
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10372
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.