Determining class of IP address

C

call_me_sol

Well, today's project is learning how to read the classful boundry of
an IP address.

Looking at the first octet of an IP address, I need to know the class
of that address:

Class A = 1-126
Class B = 128 - 191
Class C = 192 - 223

Looking at the first octet to determine the class:
64.192.29.2 = Class A = 64.0.0.0
172.16.32.4 = Clsss B = 172.16.0.0
208.16.14.1 = Class C = 208.16.14.0

I need a userform such that if user inputs any IP address into
textbox1 the form will return the Class of the written address as a
docvariable.

Can anyone show me how to do this?

Thank you!!
 
J

Jay Freedman

Well, today's project is learning how to read the classful boundry of
an IP address.

Looking at the first octet of an IP address, I need to know the class
of that address:

Class A = 1-126
Class B = 128 - 191
Class C = 192 - 223

Looking at the first octet to determine the class:
64.192.29.2 = Class A = 64.0.0.0
172.16.32.4 = Clsss B = 172.16.0.0
208.16.14.1 = Class C = 208.16.14.0

I need a userform such that if user inputs any IP address into
textbox1 the form will return the Class of the written address as a
docvariable.

Can anyone show me how to do this?

Thank you!!

Make a userform with a textbox named txtIP, a command button named
cmdClass, and a label named lblClass (plus any other controls you
like) and give the button this code:

Private Sub cmdClass_Click()
Dim IPaddr As Variant

If Len(txtIP.Text) > 0 Then
IPaddr = Split(txtIP.Text, ".")
If UBound(IPaddr) <> 3 Then
lblClass = "Not a valid IP address"
Exit Sub
End If

Select Case (CLng(IPaddr(0)))
Case 1 To 126
lblClass = "Class A"
Case 128 To 191
lblClass = "Class B"
Case 192 To 223
lblClass = "Class C"
Case Else
lblClass = "Not classified"
End Select
End If
End Sub

The Split function makes an array in the Variant, consisting of the
nodes of the address separated by periods (the periods aren't stored
in the Variant). The Select Case then figures out which range the
first node belongs to, and assigns the corresponding value to the
label.

The check for the upper bound of the variant makes sure there are four
nodes (numbering starts at 0). You could also throw in checks to make
sure all the nodes are numeric and that none of them have values
greater than 255; failing either test would also make the IP address
invalid.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
R

Russ

Call_me_sol,
Greg Maxey showed you last month how get your data and work with select case
with the macro below.
So what are you having a problem with now?
You can use the left() function to return the first three characters.

Private Sub CommandButton1_Click()
Dim myArray
Dim lngCount As Long
Dim i As Long
Dim pString As String
Dim pString2
myArray = Split(Me.TextBox1.Text, Chr(13))
lngCount = UBound(myArray) + 1
Select Case lngCount
Case Is < 10
pString2 = "ip prefix maximum 100"
Case Is > 11
Select Case lngCount
Case Is < 100
pString2 = "ip prefix list maximum 1000"
Case Is > 100
pString2 = "ip prefix list maximum 10000"
Case Else
End Select
Case Else
pString = "Whatever"
End Select
For i = 0 To UBound(myArray)
pString = myArray(i)
ActiveDocument.Range.InsertAfter "ip prefix list AS2345 permit ip "
& pString & vbCr
Next i
ActiveDocument.Range.InsertAfter pString2
Me.Hide
End Sub
 
C

call_me_sol

Hi -

Thanks for the reply. The part I'm specifically having a problem with
is taking the value that's input into the textbox and returning it in
its classful boundary.

So, if the user enters 64.192.29.2, I want the form to return 64.0.0.0
(because it's part of a class A block).
If the user enters 208.16.14.1, I want the form to return 208.16.14.0
(because it's parrt of a class C block).

I guess I don't know how to take inputted value, dissect it, and then
return it formatted as described above.

Thanks!!
 
J

Jay Freedman

Hi -

Thanks for the reply. The part I'm specifically having a problem with
is taking the value that's input into the textbox and returning it in
its classful boundary.

So, if the user enters 64.192.29.2, I want the form to return 64.0.0.0
(because it's part of a class A block).
If the user enters 208.16.14.1, I want the form to return 208.16.14.0
(because it's parrt of a class C block).

I guess I don't know how to take inputted value, dissect it, and then
return it formatted as described above.

Thanks!!

The Split function has already separated the input into its separate
nodes. All you need now is to put the appropriate pieces (depending on
class) back together, followed by the necessary ".0" nodes:

Private Sub cmdClass_Click()
Dim IPaddr As Variant

If Len(txtIP.Text) > 0 Then
IPaddr = Split(txtIP.Text, ".")
If UBound(IPaddr) <> 3 Then
lblClass = "Not a valid IP address"
Exit Sub
End If

Select Case (CLng(IPaddr(0)))
Case 1 To 126
lblClass = "Class A: " & _
IPaddr(0) & ".0.0.0"
Case 128 To 191
lblClass = "Class B: " & _
IPaddr(0) & "." & IPaddr(1) & ".0.0"
Case 192 To 223
lblClass = "Class C: " & _
IPaddr(0) & "." & IPaddr(1) & "." & IPaddr(2) & ".0"
Case Else
lblClass = "Not classified"
End Select
End If
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top