looping through IP address range

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • NetworkElf

    #1

    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.


  • Robin Tucker

    #2
    Re: looping through IP address range


    Hi Network,


    What is your application?



    Robin


    Comment

    • NetworkElf

      #3
      Re: looping through IP address range


      "Robin Tucker" <rtgroups@remov ehotmail.comwro te in message
      news:f2kdv5$c7t $1$8302bc10@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.


      Comment

      • Ray Cassick

        #4
        Re: looping through IP address range


        "NetworkElf " <NetworkElf@nos pam.nospamwrote in message
        news:OKS7gLXmHH A.4316@TK2MSFTN GP06.phx.gbl...
        >
        "Robin Tucker" <rtgroups@remov ehotmail.comwro te in message
        news:f2kdv5$c7t $1$8302bc10@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.





        >

        Comment

        • Ray Cassick

          #5
          Re: looping through IP address range


          "Ray Cassick" <rcassick@enter procity.comwrot e in message
          news:%23Moee8bm HHA.960@TK2MSFT NGP03.phx.gbl.. .
          >
          "NetworkElf " <NetworkElf@nos pam.nospamwrote in message
          news:OKS7gLXmHH A.4316@TK2MSFTN GP06.phx.gbl...
          >>
          >"Robin Tucker" <rtgroups@remov ehotmail.comwro te in message
          >news:f2kdv5$c7 t$1$8302bc10@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


          Comment

          • Steven Cheng[MSFT]

            #6
            Re: looping through IP address range

            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:


            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.

            Comment

            • NetworkElf

              #7
              Re: looping through IP address range

              ----- Original Message -----
              From: "Ray Cassick" <rcassick@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" <rcassick@enter procity.comwrot e in message
              news:%23Moee8bm HHA.960@TK2MSFT NGP03.phx.gbl.. .
              >>
              >"NetworkElf " <NetworkElf@nos pam.nospamwrote in message
              >news:OKS7gLXmH HA.4316@TK2MSFT NGP06.phx.gbl.. .
              >>>
              >>"Robin Tucker" <rtgroups@remov ehotmail.comwro te in message
              >>news:f2kdv5$c 7t$1$8302bc10@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.


              Comment

              • Steven Cheng[MSFT]

                #8
                Re: looping through IP address range

                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.

                Comment

                • Andrew Morton

                  #9
                  Re: looping through IP address range

                  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


                  Comment

                  • Stephany Young

                    #10
                    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).

                    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" <akm@in-press.co.uk.inv alidwrote in message
                    news:uJttY%23Tn HHA.3656@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
                    >

                    Comment

                    • Andrew Morton

                      #11
                      Re: looping through IP address range

                      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...got o 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


                      Comment

                      • Steven Cheng[MSFT]

                        #12
                        Re: looping through IP address range

                        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.

                        Comment

                        • NetworkElf

                          #13
                          Re: looping through IP address range


                          ----- Original Message -----
                          From: "Stephany Young" <noone@localhos t>
                          Newsgroups: microsoft.publi c.dotnet.langua ges.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.


                          Comment

                          • NetworkElf

                            #14
                            Re: looping through IP address range

                            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]" <stcheng@online .microsoft.comw rote in message
                            news:Wz9EPyQoHH A.564@TK2MSFTNG HUB02.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.
                            >

                            Comment

                            • Steven Cheng[MSFT]

                              #15
                              Re: looping through IP address range

                              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.

                              Comment

                              Working...