PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2008 Calculating All IP Addresses in a Range (Pretty Good Start)-VBForums
Results 1 to 3 of 3

Thread: Calculating All IP Addresses in a Range (Pretty Good Start)

  1. #1

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Question Calculating All IP Addresses in a Range (Pretty Good Start)

    Good morning,

    I've been trying to calculate all IP addresses in a document that has been setup as 3 different sets of arrays. One array, individual ranges, ie. 127.0.0.1-255.255.255.255, preceded by just a name of that range. Each one of these individual portions are delimited by "~".

    "This range:10.0.0.1-10.1.0.0~Otherrange:192.0.0.1-196.0.0.1~"..... and so on. Just felt a little backgroud was necessary to understand the beginning.

    I have been fairly successful with the code, but I am now not seeing where the issue is arising from that is causing me to occasionally miss IP addresses towards the end of the range, and why I occasionally get a "256" in some of the octets.

    vb.net Code:
    1. Imports System.IO
    2. Imports System
    3.  
    4. Public Class Form1
    5.     Dim import1 As String = File.ReadAllText(Environment.ExpandEnvironmentVariables("%homepath%\Desktop\lists\ips.txt"))
    6.     Dim i As String = 0
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Dim theout As String = Nothing
    9.         Dim ary() As String = import1.Split("~"c)
    10.         Dim total As String = Nothing
    11.         'Split into Array all Names/Addresses by "~"
    12.         For Each ar As String In ary
    13.             'Split Names of Addresses from the IP Range
    14.             Dim arsplit() As String = ar.Split(":"c)
    15.             'Determine isrange or issingle
    16.             If arsplit(1).ToString.Contains("-") Then
    17.                 'Split ranges
    18.                 Dim a() As String = arsplit(1).ToString.Split("-"c)
    19.                 'Break octets
    20.                 Dim segments1() As String = a(0).ToString.Replace(" ", "").Split("."c)
    21.                 Dim segments2() As String = a(1).ToString.Replace(" ", "").Split("."c)
    22.  
    23.                 'If first oct greater range
    24.                 If segments2(0) > segments1(0) Then
    25.                     Dim h As String = segments1(0)
    26.                     Dim q As String = segments1(1)
    27.                     Dim p As String = segments1(2)
    28.                     Dim t As String = segments1(3)
    29.  
    30.                     'Attempt to Make all range between two IP addresses
    31.                     Do Until h = segments2(0) And q = segments2(1) And p = segments2(2) _
    32.                     And t = segments2(3)
    33.  
    34.                         If q = "256" Then
    35.                             'If 2nd oct hits 256 then 1st + 1, 2nd = 0
    36.                             q = "0"
    37.                             h = h + 1
    38.                             'MsgBox(h & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
    39.                         End If
    40.  
    41.                         If p = "256" Then
    42.                             'If 3rd oct hits 256 then 2nd + 1, 3rd = 0
    43.                             q = q + 1
    44.                             p = "0"
    45.                         End If
    46.  
    47.                         If t = "256" Then
    48.                             'If 4th oct hits 256 then 3rd + 1, 4th = 0
    49.                             p = p + 1
    50.                             t = "0"
    51.                         End If
    52.                         Dim temp As String = "127.0.01" & vbTab & h & "." & q & "." & p & "." _
    53.                         & t & Environment.NewLine
    54.  
    55.                         'Write out IP to a cumulative string
    56.                         total &= temp
    57.  
    58.                         '+1 IP to last oct
    59.                         t = t + 1
    60.  
    61.                         'Was using this trying to determine how to stop getting a 256 in last oct.
    62.                         'If h = segments2(0) = True Then
    63.                         '    Exit Do
    64.                         'End If
    65.                     Loop
    66.  
    67.                     'If first oct greater range
    68.                 ElseIf segments2(1) > segments1(1) Then
    69.                     Dim q As String = segments1(1)
    70.                     Dim p As String = segments1(2)
    71.                     Dim t As String = segments1(3)
    72.  
    73.                     'MsgBox(segments1(0) & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
    74.  
    75.                     Do Until q = segments2(1) And p = segments2(2) And t = segments2(3)
    76.                         If p = "256" Then
    77.                             q = q + 1
    78.                             p = "0"
    79.                         End If
    80.                         If t = "256" Then
    81.                             p = p + 1
    82.                             t = "0"
    83.                             'MsgBox(segments1(0) & "." & q & "." & p & "." & t, _
    84.                             'MsgBoxStyle.Information, i)
    85.                         End If
    86.                         Dim temp As String = "127.0.0.1" & vbTab & segments1(0) & "." & q & "." & p _
    87.                         & "." & t & Environment.NewLine
    88.                         total &= temp
    89.                         t = t + 1
    90.                         If q = segments2(1) And p = segments2(2) And t = segments2(3) = True Then
    91.                             Exit Do
    92.                         End If
    93.                     Loop
    94.                 ElseIf segments2(2) > segments1(2) Then
    95.                     Dim p As String = segments1(2)
    96.                     Dim t As String = segments1(3)
    97.                     'MsgBox(segments1(0) & "." & segments1(1) & "." & p & "." & t, _
    98.                     'MsgBoxStyle.Information, arsplit(1))
    99.                     Do Until p = segments2(2) And t = segments2(3)
    100.                         If t = "256" Then
    101.                             p = p + 1
    102.                             t = "0"
    103.                         End If
    104.                         total &= "127.0.0.1" & vbTab & segments1(0) & "." & segments1(1) & "." & _
    105.                         p & "." & t & Environment.NewLine
    106.                         t = t + 1
    107.                         If p = segments2(2) And t = segments2(3) = True Then
    108.                             Exit Do
    109.                         End If
    110.                     Loop
    111.                 ElseIf segments2(3) > segments1(3) Then
    112.                     Dim lastt As String = segments1(3)
    113.                     Do Until lastt = segments2(3)
    114.                         total &= "127.0.0.1" & vbTab & segments1(0) & "." & segments1(1) & "." & _
    115.                         segments1(2) & "." & lastt & Environment.NewLine
    116.                         lastt = lastt + 1
    117.                         If lastt > segments2(3) = True Then
    118.                             Exit Do
    119.                         End If
    120.                     Loop
    121.                 End If
    122.             Else
    123.                 total &= "127.0.0.1" & vbTab & arsplit(1).ToString & Environment.NewLine
    124.             End If
    125.             i = i + 1
    126.         Next
    127.         'Write out a text file
    128.         File.WriteAllText(Environment.ExpandEnvironmentVariables("%homepath%\Desktop\lists\" & _
    129.                                                                  "output.txt"), total)
    130.     End Sub
    131. End Class

    Thank you very much to anyone that can help, I originally started this code trying to help someone else with this question on another forum, and now I too am stuck and really would like to see it completed. Or heaven forbid there is an easier way to which someone could refer me to, I would be glad to look at that route.

  2. #2

    Thread Starter
    Addicted Member condonethis's Avatar
    Join Date
    Apr 2010
    Location
    TX
    Posts
    133

    Resolved Re: Calculating All IP Addresses in a Range (Pretty Good Start)

    Hello everyone, looks like I figured it out. Please let me know if anyone sees something I missed, but It found all of the ranges that I was looking for just now! It seems I was doing my + 1 to octets backwards, lol sure pays to sleep on it.
    vb.net Code:
    1. Imports System.IO
    2. Imports System
    3.  
    4. Public Class Form1
    5.     Dim import1 As String = File.ReadAllText(Environment.ExpandEnvironmentVariables("%homepath%\Desktop\lists\IPList.txt"))
    6.     Dim i As String = 0
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Dim theout As String = Nothing
    9.         Dim ary() As String = import1.Split("~"c)
    10.         Dim total As String = Nothing
    11.         'Split into Array all Names/Addresses by "~"
    12.         For Each ar As String In ary
    13.             'Split Names of Addresses from the IP Range
    14.             Dim arsplit() As String = ar.Split(":"c)
    15.             'Determine isrange or issingle
    16.             If arsplit(1).ToString.Contains("-") Then
    17.                 'Split ranges
    18.                 Dim a() As String = arsplit(1).ToString.Split("-"c)
    19.                 'Break octets
    20.                 Dim segments1() As String = a(0).ToString.Replace(" ", "").Split("."c)
    21.                 Dim segments2() As String = a(1).ToString.Replace(" ", "").Split("."c)
    22.  
    23.                 'If first oct greater range
    24.                 If segments2(0) > segments1(0) Then
    25.                     Dim h As String = segments1(0)
    26.                     Dim q As String = segments1(1)
    27.                     Dim p As String = segments1(2)
    28.                     Dim t As String = segments1(3)
    29.  
    30.                     'Attempt to Make all range between two IP addresses
    31.                     Do Until h = segments2(0) And q = segments2(1) And p = segments2(2) And t = segments2(3) + 1
    32.                         If t = "256" Then
    33.                             'If 4th oct hits 256 then 3rd + 1, 4th = 0
    34.                             p = p + 1
    35.                             t = "0"
    36.                         End If
    37.                         If p = "256" Then
    38.                             'If 3rd oct hits 256 then 2nd + 1, 3rd = 0
    39.                             q = q + 1
    40.                             p = "0"
    41.                         End If
    42.                         If q = "256" Then
    43.                             'If 2nd oct hits 256 then 1st + 1, 2nd = 0
    44.                             q = "0"
    45.                             h = h + 1
    46.                             'MsgBox(h & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
    47.                         End If
    48.  
    49.                         Dim temp As String = "127.0.01" & vbTab & h & "." & q & "." & p & "." & t & Environment.NewLine
    50.  
    51.                         'Write out IP to a cumulative string
    52.                         total &= temp
    53.  
    54.                         '+1 IP to last oct
    55.                         t = t + 1
    56.  
    57.                         'Was using this trying to determine how to stop getting a 256 in last oct.
    58.                         'If h = segments2(0) = True Then
    59.                         '    Exit Do
    60.                         'End If
    61.                     Loop
    62.  
    63.                     'If first oct greater range
    64.                 ElseIf segments2(1) > segments1(1) Then
    65.                     Dim q As String = segments1(1)
    66.                     Dim p As String = segments1(2)
    67.                     Dim t As String = segments1(3)
    68.  
    69.                     'MsgBox(segments1(0) & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
    70.  
    71.                     Do Until q = segments2(1) And p = segments2(2) And t = segments2(3) + 1
    72.                         If t = "256" Then
    73.                             p = p + 1
    74.                             t = "0"
    75.                             'MsgBox(segments1(0) & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
    76.                         End If
    77.                         If p = "256" Then
    78.                             q = q + 1
    79.                             p = "0"
    80.                         End If
    81.                         Dim temp As String = "127.0.0.1" & vbTab & segments1(0) & "." & q & "." & p & "." & t & Environment.NewLine
    82.                         total &= temp
    83.                         t = t + 1
    84.                         'If q = segments2(1) And p = segments2(2) And t = segments2(3) = True Then
    85.                         '    Exit Do
    86.                         'End If
    87.                     Loop
    88.                 ElseIf segments2(2) > segments1(2) Then
    89.                     Dim p As String = segments1(2)
    90.                     Dim t As String = segments1(3)
    91.                     'MsgBox(segments1(0) & "." & segments1(1) & "." & p & "." & t, MsgBoxStyle.Information, arsplit(1))
    92.                     Do Until p = segments2(2) And t = segments2(3) + 1
    93.                         If t = "256" Then
    94.                             'MsgBox(p)
    95.                             'MsgBox(segments2(2))
    96.                             p = p + 1
    97.                             t = "0"
    98.                         End If
    99.                         total &= "127.0.0.1" & vbTab & segments1(0) & "." & segments1(1) & "." & p & "." & t & Environment.NewLine
    100.                         t = t + 1
    101.                         'If p = segments2(2) And t = segments2(3) = True Then
    102.                         '    Exit Do
    103.                         'End If
    104.                     Loop
    105.                 ElseIf segments2(3) > segments1(3) Then
    106.                     Dim lastt As String = segments1(3)
    107.                     Do Until lastt = segments2(3) + 1
    108.                         total &= "127.0.0.1" & vbTab & segments1(0) & "." & segments1(1) & "." & segments1(2) & "." & lastt & Environment.NewLine
    109.                         lastt = lastt + 1
    110.                         If lastt > segments2(3) = True Then
    111.                             Exit Do
    112.                         End If
    113.                     Loop
    114.                 End If
    115.             Else
    116.                 total &= "127.0.0.1" & vbTab & arsplit(1).ToString & Environment.NewLine
    117.             End If
    118.             i = i + 1
    119.         Next
    120.         'Write out a text file
    121.         File.WriteAllText(Environment.ExpandEnvironmentVariables("%homepath%\Desktop\lists\" & "output.txt"), total)
    122.     End Sub
    123. End Class

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,226

    Re: Calculating All IP Addresses in a Range (Pretty Good Start)

    FWIW - if you are working with IPv4 addresses you can treat the ip address as a number.

    Code:
        ''' <summary>
        ''' converts a Net.IPAddress to an Integer
        ''' </summary>
        ''' <param name="IP">a Net.IPAddress</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function IPtoNum(IP As Net.IPAddress) As Integer
            'convert IP address to integer
            Dim addrByts As List(Of Byte) = IP.GetAddressBytes.ToList 'get the address bytes
            addrByts.Reverse() 'reverse so number is correct
            'convert to IP addr to number
            Return BitConverter.ToInt32(addrByts.ToArray, 0) 'return the integer
        End Function
    
        ''' <summary>
        ''' converts an integer to a Net.IPAddress
        ''' </summary>
        ''' <param name="Num"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function NumToIP(Num As Integer) As Net.IPAddress
            Dim netwkNum() As Byte = BitConverter.GetBytes(Num) 'get bytes for number
            Array.Reverse(netwkNum) 'in correct order
            Return New Net.IPAddress(netwkNum)
        End Function
    To see how this works
    Code:
            For x As Integer = IPtoNum(Net.IPAddress.Parse("192.168.1.254")) To IPtoNum(Net.IPAddress.Parse("192.168.2.10"))
                Dim s As String = NumToIP(x).ToString
                Debug.WriteLine(s)
            Next
    Last edited by dbasnett; Mar 26th, 2015 at 06:16 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width