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.