Results 1 to 11 of 11

Thread: Function IsIP()

  1. #1

    Thread Starter
    Addicted Member kzatu's Avatar
    Join Date
    Aug 2003
    Location
    Nevada
    Posts
    148

    Post Function IsIP()

    I thought I'd add a little to this community. This may have been done before but I searched and didn't see this code. It's not stellar or ground breaking but it is very useful for me.

    I use this function to determine if a value passed is a valid IP address or not.

    VB Code:
    1. ' Verifies if Data qualifies as an IP address
    2. Function IsIP(IP As String) As Boolean
    3.     'Declarations
    4.     Dim tmpIP As Variant
    5.     Dim Element As Variant
    6.     Dim x As Integer, y As String
    7.    
    8.     'Verify 4 octets
    9.     tmpIP = Split(IP, ".")
    10.     If UBound(tmpIP) <> 3 Then
    11.         IsIP = False
    12.         Exit Function
    13.     End If
    14.  
    15.     'Verify all numerics
    16.     tmpIP = Replace(IP, ".", "")
    17.     For x = 1 To Len(tmpIP)
    18.         y = Mid(tmpIP, x, 1)
    19.    
    20.         If Not IsNumeric(y) Then
    21.             IsIP = False
    22.             Exit Function
    23.         End If
    24.     Next x
    25.  
    26.     'Verify octet values
    27.     tmpIP = Split(IP, ".")
    28.     For Each Element In tmpIP
    29.         If Element > 255 Then
    30.             IsIP = False
    31.             Exit Function
    32.         End If
    33.     Next
    34.  
    35.     'All tests passed
    36.     IsIP = True
    37. End Function
    Changes are not permanent, but change is. {Neil Peart}

  2. #2
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Function IsIP()

    not bad code, though if it checked to see if the IP existed that would be about 10 times better
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,802

    Re: Function IsIP()

    How about this ?

    No loops
    VB Code:
    1. Private Function IsIP(StrIP As String) As Boolean
    2.     Dim Elements() As String
    3.     On Error GoTo Exit_Sub
    4.    
    5.     Elements = Split(StrIP, ".")
    6.    
    7.     If UBound(Elements) = 3 And IsNumeric(Replace(StrIP, ".", "")) Then
    8.         IsIP = (CInt(CByte(Elements(0))) + CByte(Elements(1)) + CByte(Elements(2)) + CByte(Elements(3))) > 0
    9.     End If
    10.    
    11.     Exit Function
    12. Exit_Sub:
    13.     IsIP = False
    14. End Function

  4. #4
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Function IsIP()

    Still Doesn't check that it exists, does it?

    Tip - Use CheckInternetConnection, with the url to check as the specified ip. if the ip exists, it should return true
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  5. #5
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,802

    Re: Function IsIP()

    Quote Originally Posted by kazar
    Still Doesn't check that it exists, does it?
    I know, I know... the ultimate check, if you can ping it, it's valid... I know that... but sometimes you just want to check the format regardless if it exists or not.

  6. #6
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Function IsIP()

    True, but it'll be valid if it can connect
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  7. #7

    Thread Starter
    Addicted Member kzatu's Avatar
    Join Date
    Aug 2003
    Location
    Nevada
    Posts
    148

    Re: Function IsIP()

    I think determining if the data is a valid IP address and whether it's already in use on the network are two completely different things, so I didn't include it in this code.

    But since you asked, here is code that will tell you if the IP address is in use or not. I found this off of MSDN somewhere.

    VB Code:
    1. ' Returns True if an IP address is available
    2. Function IsIPAvailable(IP As String) As Boolean
    3.     Dim objWMIService
    4.     Dim colPings
    5.     Dim objStatus
    6.    
    7.     Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    8.     Set colPings = objWMIService.ExecQuery("Select * From Win32_PingStatus where Address = '" & IP & "'")
    9.    
    10.     For Each objStatus In colPings
    11.         If IsNull(objStatus.StatusCode) Or objStatus.StatusCode <> 0 Then
    12.             IsIPAvailable = True
    13.         End If
    14.     Next
    15. End Function
    Last edited by kzatu; May 25th, 2006 at 09:31 AM.
    Changes are not permanent, but change is. {Neil Peart}

  8. #8
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Function IsIP()

    Thats much better, nice code.
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  9. #9
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338

    Re: Function IsIP()

    Quote Originally Posted by CVMichael
    I know, I know... the ultimate check, if you can ping it, it's valid... I know that... but sometimes you just want to check the format regardless if it exists or not.
    Not true. I've once set up a firewall that drops pings. Stops a few script kiddies at the very least. Would only allow incoming SSH connections from authorised hosts (and authentication only by public keys), everything else from behind it would get routed, and nothing else allowed in (besides established connections).

    Just saying, don't assume that a ping verifies it's existence.

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Function IsIP()

    Valid IP on network and valid format are two different things... such as in cases where your testing a subnet mask and not an IP.
    Handle exponent/non-digit characters.
    VB Code:
    1. Public Function ValidIP(ByVal Value As String) As Boolean
    2. Dim strOctet() As String
    3. Dim lngPos As Long
    4. Dim bytTemp As Byte
    5.  
    6.    strOctet = Split(Value, ".")
    7.    If UBound(strOctet) <> 3 Then Exit Function
    8.    Value = Join(strOctet, "")
    9.  
    10. On Error GoTo ErrHandler
    11.    'Check for existence of non-digit characters, such as E or D
    12.    'since IsNumeric("2E2") = True, and CByte("2E2") = 200
    13.    For lngPos = 1 To Len(Value)
    14.       bytTemp = CByte(Mid(Value, lngPos, 1))
    15.    Next
    16.    'Check range of octets
    17.    For lngPos = 0 To 3
    18.       bytTemp = CByte(strOctet(lngPos))
    19.    Next
    20.  
    21.    ValidIP = True
    22.    Exit Function
    23.    
    24. ErrHandler:
    25.    Err.Clear
    26. End Function
    Last edited by leinad31; Feb 17th, 2007 at 01:29 PM.

  11. #11

    Thread Starter
    Addicted Member kzatu's Avatar
    Join Date
    Aug 2003
    Location
    Nevada
    Posts
    148

    Re: Function IsIP()

    That is a clever observation on the IsNumeric (and cbyte) function. Thanks for sharing this.
    Changes are not permanent, but change is. {Neil Peart}

Posting Permissions

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



Click Here to Expand Forum to Full Width