Trim IP address

Simon Scott 306 Reputation points
2021-04-06T16:00:15.857+00:00

Good afternoon,

I'm wondering if someone can help me with some VB.net code?

I'm looking to trim a PC's IP address so i can then do certain functionality.

I have the IP address stored in the string StrIPaddress and i need the code to basically run like so...

If IP address is 10.10.11.x or 10.10.12.x then run command
if IP address is 10.140.10.x or 10.140.11.x then run 2nd command
etc

Can anyone help me at all?

Thanks
Simon

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,546 Reputation points
    2021-04-06T17:47:59.287+00:00
    Sub RunByIP(ipString As String)
        Dim ipArray() As String = Split(ipString, ".")
        If ipArray(0) <> "10" Then
            Exit Sub
        End If
        If ipArray(1) = "10" And (ipArray(2) = "11" Or ipArray(2) = "12") Then
            ' run command
        Else
            If ipArray(1) = "140" Then
                If ipArray(2) = "10" Or ipArray(2) = "11" Then
                    ' run 2nd command
                End If
            End If
        End If
    End Sub
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. DonR 26 Reputation points
    2021-04-06T16:05:34.64+00:00
    1 person found this answer helpful.

  2. Peter Fleischer (former MVP) 19,341 Reputation points
    2021-04-06T18:03:23.93+00:00

    Hi,
    you can use System.Net.IPAddress. Try following code:

    Module Module72
    
      Sub Main()
        Try
          Dim c As New Demo
          c.Execute()
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
      Friend Class Demo
    
        Friend Sub Execute()
          Try
            Dim adr() As String = New String() {"10.10.11.5", "10.10.12.111", "10.140.10.55", "10.140.11.66"}
            For i = 0 To adr.Length - 1
              Dim ipAdr As String = adr(i)
              Dim b = System.Net.IPAddress.Parse(ipAdr).GetAddressBytes ' Address Bytes '
              If b(0) = 10 And b(1) = 10 And (b(2) = 11 Or b(2) = 12) Then Console.WriteLine($"{ipAdr}: 1st command")
              If b(0) = 10 And b(1) = 140 And (b(2) = 10 Or b(2) = 11) Then Console.WriteLine($"{ipAdr}: 2nd command")
            Next
          Catch ex As Exception
            Console.WriteLine(ex.Message)
          End Try
        End Sub
    
      End Class
    
    End Module
    

    Result:

    10.10.11.5: 1st command
    10.10.12.111: 1st command
    10.140.10.55: 2nd command
    10.140.11.66: 2nd command
    Continue enter key
    
    0 comments No comments

  3. Simon Scott 306 Reputation points
    2021-04-16T10:35:59.647+00:00

    Thanks for all your assistance with this.

    I have used the code from SimpleSamples which works a treat :)


  4. Dewayne Basnett 1,381 Reputation points
    2021-04-16T13:45:26.163+00:00

    A general IP address range check

    ''' <summary>
    ''' check IP4 in range
    ''' </summary>
    ''' <param name="rngLow">low IP address</param>
    ''' <param name="rngHigh">high IP address</param>
    ''' <param name="ChkIP">IP address to check</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IPAddrRangeCheck(rngLow As String,
                                          rngHigh As String,
                                          ChkIP As String) As Boolean
        Dim rv As Boolean = False
        Dim rL As Net.IPAddress
        Dim rH As Net.IPAddress
        Dim chk As Net.IPAddress
        'verify three IP addresses
        If Net.IPAddress.TryParse(rngLow, rL) AndAlso
                Net.IPAddress.TryParse(rngHigh, rH) AndAlso
                Net.IPAddress.TryParse(ChkIP, chk) Then
    
            rv = IPAddrRangeCheck(rL, rH, chk)
        End If
        Return rv
    End Function
    
    ''' <summary>
    ''' check IP4 in range
    ''' </summary>
    ''' <param name="rngLow">low IP address</param>
    ''' <param name="rngHigh">high IP address</param>
    ''' <param name="ChkIP">IP address to check</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IPAddrRangeCheck(rngLow As Net.IPAddress,
                                      rngHigh As Net.IPAddress,
                                      ChkIP As Net.IPAddress) As Boolean
        Dim rv As Boolean = False
    
        Dim _rlN As UInt32 = BitConverter.ToUInt32(rngLow.GetAddressBytes.Reverse.ToArray, 0)
        Dim _rhN As UInt32 = BitConverter.ToUInt32(rngHigh.GetAddressBytes.Reverse.ToArray, 0)
        Dim _chkN As UInt32 = BitConverter.ToUInt32(ChkIP.GetAddressBytes.Reverse.ToArray, 0)
        rv = _chkN >= _rlN AndAlso _chkN <= _rhN
        Return rv
    End Function
    

    Usage:

        '10.10.11.x or 10.10.12.x then run command
        If IPAddrRangeCheck("10.10.11.0", "10.10.11.255", "10.10.12.7") OrElse
             IPAddrRangeCheck("10.10.12.0", "10.10.12.255", "10.10.12.7") Then
            'run command
        End If
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.