• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

Get ip addresses from a given range (192.168.2.1-192.168.2.255)

First off, let me say that I HAVE been looking for an example of how to do this, but surprisingly it is not a very popular topic\subject.

In VB.Net:

I need to get a list of IP addresses from a given range (192.168.2.1-192.168.2.255)

A VERY VAGUE EXAMPLE:

dim minimum_val as integer = textbox1.text
dim max_value as integer = textbox2.text

???
listbox1.items.add(current_ip)
???
0
APag96
Asked:
APag96
  • 4
  • 2
1 Solution
 
Bob LearnedCommented:
Here is what I use:

Sample usage:

        Dim ipAddressRange As List(Of String) = IpAddressHelper.GetAddressRange("192.168.1.1", "192.168.1.100")

Public Class IpAddressHelper

    Public Shared Function GetAddressRange(ByVal startAddress As String, ByVal endAddress As String) As List(Of String)
        Dim list As New List(Of String)

        Dim n1 As Long = IpAddressToNumber(startAddress)
        Dim n2 As Long = IpAddressToNumber(endAddress)

        For i As Long = n1 To n2
            list.Add(NumberToIpAddress(i))
        Next i
        Return list
    End Function

    Public Shared Function IpAddressToNumber(ByVal ipAddress As String) As Long
        Dim parts() As String = ipAddress.Split(".")
        Dim sum As Long
        If parts.Length <> 4 Then
            Throw New ArgumentException("Invalid IP address:  " & ipAddress)
        End If

        For i As Integer = parts.Length - 1 To 0 Step -1
            sum += (Int(parts(i)) Mod 256) * Math.Pow(256, 3 - i)
        Next i
        Return sum
    End Function

    Public Shared Function NumberToIpAddress(ByVal ipAddressNumber As Long) As String
        Dim sb As New StringBuilder()
        For i As Integer = 1 To 4
            Dim factor As Integer = (256 ^ (4 - i))
            Dim number As Long = ipAddressNumber \ factor
            ipAddressNumber -= number * factor
            sb.Append(number & ".")
        Next i
        Return sb.ToString().TrimEnd(".")
    End Function

End Class

Open in new window

0
 
APag96Author Commented:
Looks good. I'll try this out and get back to you. Thanks.
0
 
APag96Author Commented:
Thank you very much! That is exactily what I was looking for. 50 points for you!
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
APag96Author Commented:
Perfect.
0
 
Bob LearnedCommented:
"I don't need no stinkin' points"  The "Thank you very much!" means more to me.
0
 
APag96Author Commented:
:) You deserve the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now