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

• Mar 25th, 2015, 09:36 AM
condonethis
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:
```Imports System.IO
Imports System
Public Class Form1
Dim import1 As String = File.ReadAllText(Environment.ExpandEnvironmentVariables("%homepath%\Desktop\lists\ips.txt"))
Dim i As String = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim theout As String = Nothing
Dim ary() As String = import1.Split("~"c)
Dim total As String = Nothing
'Split into Array all Names/Addresses by "~"
For Each ar As String In ary
'Split Names of Addresses from the IP Range
Dim arsplit() As String = ar.Split(":"c)
'Determine isrange or issingle
If arsplit(1).ToString.Contains("-") Then
'Split ranges
Dim a() As String = arsplit(1).ToString.Split("-"c)
'Break octets
Dim segments1() As String = a(0).ToString.Replace(" ", "").Split("."c)
Dim segments2() As String = a(1).ToString.Replace(" ", "").Split("."c)
'If first oct greater range
If segments2(0) > segments1(0) Then
Dim h As String = segments1(0)
Dim q As String = segments1(1)
Dim p As String = segments1(2)
Dim t As String = segments1(3)
'Attempt to Make all range between two IP addresses
Do Until h = segments2(0) And q = segments2(1) And p = segments2(2) _
And t = segments2(3)
If q = "256" Then
'If 2nd oct hits 256 then 1st + 1, 2nd = 0
q = "0"
h = h + 1
'MsgBox(h & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
End If
If p = "256" Then
'If 3rd oct hits 256 then 2nd + 1, 3rd = 0
q = q + 1
p = "0"
End If
If t = "256" Then
'If 4th oct hits 256 then 3rd + 1, 4th = 0
p = p + 1
t = "0"
End If
Dim temp As String = "127.0.01" & vbTab & h & "." & q & "." & p & "." _
& t & Environment.NewLine
'Write out IP to a cumulative string
total &= temp
'+1 IP to last oct
t = t + 1
'Was using this trying to determine how to stop getting a 256 in last oct.
'If h = segments2(0) = True Then
'    Exit Do
'End If
Loop
'If first oct greater range
ElseIf segments2(1) > segments1(1) Then
Dim q As String = segments1(1)
Dim p As String = segments1(2)
Dim t As String = segments1(3)
'MsgBox(segments1(0) & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
Do Until q = segments2(1) And p = segments2(2) And t = segments2(3)
If p = "256" Then
q = q + 1
p = "0"
End If
If t = "256" Then
p = p + 1
t = "0"
'MsgBox(segments1(0) & "." & q & "." & p & "." & t, _
'MsgBoxStyle.Information, i)
End If
Dim temp As String = "127.0.0.1" & vbTab & segments1(0) & "." & q & "." & p _
& "." & t & Environment.NewLine
total &= temp
t = t + 1
If q = segments2(1) And p = segments2(2) And t = segments2(3) = True Then
Exit Do
End If
Loop
ElseIf segments2(2) > segments1(2) Then
Dim p As String = segments1(2)
Dim t As String = segments1(3)
'MsgBox(segments1(0) & "." & segments1(1) & "." & p & "." & t, _
'MsgBoxStyle.Information, arsplit(1))
Do Until p = segments2(2) And t = segments2(3)
If t = "256" Then
p = p + 1
t = "0"
End If
total &= "127.0.0.1" & vbTab & segments1(0) & "." & segments1(1) & "." & _
p & "." & t & Environment.NewLine
t = t + 1
If p = segments2(2) And t = segments2(3) = True Then
Exit Do
End If
Loop
ElseIf segments2(3) > segments1(3) Then
Dim lastt As String = segments1(3)
Do Until lastt = segments2(3)
total &= "127.0.0.1" & vbTab & segments1(0) & "." & segments1(1) & "." & _
segments1(2) & "." & lastt & Environment.NewLine
lastt = lastt + 1
If lastt > segments2(3) = True Then
Exit Do
End If
Loop
End If
Else
total &= "127.0.0.1" & vbTab & arsplit(1).ToString & Environment.NewLine
End If
i = i + 1
Next
'Write out a text file
File.WriteAllText(Environment.ExpandEnvironmentVariables("%homepath%\Desktop\lists\" & _
"output.txt"), total)
End Sub
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.
• Mar 25th, 2015, 10:06 AM
condonethis
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:
```Imports System.IO
Imports System
Public Class Form1
Dim import1 As String = File.ReadAllText(Environment.ExpandEnvironmentVariables("%homepath%\Desktop\lists\IPList.txt"))
Dim i As String = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim theout As String = Nothing
Dim ary() As String = import1.Split("~"c)
Dim total As String = Nothing
'Split into Array all Names/Addresses by "~"
For Each ar As String In ary
'Split Names of Addresses from the IP Range
Dim arsplit() As String = ar.Split(":"c)
'Determine isrange or issingle
If arsplit(1).ToString.Contains("-") Then
'Split ranges
Dim a() As String = arsplit(1).ToString.Split("-"c)
'Break octets
Dim segments1() As String = a(0).ToString.Replace(" ", "").Split("."c)
Dim segments2() As String = a(1).ToString.Replace(" ", "").Split("."c)
'If first oct greater range
If segments2(0) > segments1(0) Then
Dim h As String = segments1(0)
Dim q As String = segments1(1)
Dim p As String = segments1(2)
Dim t As String = segments1(3)
'Attempt to Make all range between two IP addresses
Do Until h = segments2(0) And q = segments2(1) And p = segments2(2) And t = segments2(3) + 1
If t = "256" Then
'If 4th oct hits 256 then 3rd + 1, 4th = 0
p = p + 1
t = "0"
End If
If p = "256" Then
'If 3rd oct hits 256 then 2nd + 1, 3rd = 0
q = q + 1
p = "0"
End If
If q = "256" Then
'If 2nd oct hits 256 then 1st + 1, 2nd = 0
q = "0"
h = h + 1
'MsgBox(h & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
End If
Dim temp As String = "127.0.01" & vbTab & h & "." & q & "." & p & "." & t & Environment.NewLine
'Write out IP to a cumulative string
total &= temp
'+1 IP to last oct
t = t + 1
'Was using this trying to determine how to stop getting a 256 in last oct.
'If h = segments2(0) = True Then
'    Exit Do
'End If
Loop
'If first oct greater range
ElseIf segments2(1) > segments1(1) Then
Dim q As String = segments1(1)
Dim p As String = segments1(2)
Dim t As String = segments1(3)
'MsgBox(segments1(0) & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
Do Until q = segments2(1) And p = segments2(2) And t = segments2(3) + 1
If t = "256" Then
p = p + 1
t = "0"
'MsgBox(segments1(0) & "." & q & "." & p & "." & t, MsgBoxStyle.Information, i)
End If
If p = "256" Then
q = q + 1
p = "0"
End If
Dim temp As String = "127.0.0.1" & vbTab & segments1(0) & "." & q & "." & p & "." & t & Environment.NewLine
total &= temp
t = t + 1
'If q = segments2(1) And p = segments2(2) And t = segments2(3) = True Then
'    Exit Do
'End If
Loop
ElseIf segments2(2) > segments1(2) Then
Dim p As String = segments1(2)
Dim t As String = segments1(3)
'MsgBox(segments1(0) & "." & segments1(1) & "." & p & "." & t, MsgBoxStyle.Information, arsplit(1))
Do Until p = segments2(2) And t = segments2(3) + 1
If t = "256" Then
'MsgBox(p)
'MsgBox(segments2(2))
p = p + 1
t = "0"
End If
total &= "127.0.0.1" & vbTab & segments1(0) & "." & segments1(1) & "." & p & "." & t & Environment.NewLine
t = t + 1
'If p = segments2(2) And t = segments2(3) = True Then
'    Exit Do
'End If
Loop
ElseIf segments2(3) > segments1(3) Then
Dim lastt As String = segments1(3)
Do Until lastt = segments2(3) + 1
total &= "127.0.0.1" & vbTab & segments1(0) & "." & segments1(1) & "." & segments1(2) & "." & lastt & Environment.NewLine
lastt = lastt + 1
If lastt > segments2(3) = True Then
Exit Do
End If
Loop
End If
Else
total &= "127.0.0.1" & vbTab & arsplit(1).ToString & Environment.NewLine
End If
i = i + 1
Next
'Write out a text file
File.WriteAllText(Environment.ExpandEnvironmentVariables("%homepath%\Desktop\lists\" & "output.txt"), total)
End Sub
End Class```
• Mar 25th, 2015, 06:25 PM
dbasnett
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```