Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 12-29-2004, 11:53 AM
okstorms's Avatar
okstorms okstorms is offline
Registered User
 
Join Date: Aug 2004
Location: Wilmington, DE
Posts: 38
Subnetting in vbscript

I had a terrible time finding this info anywhere on the internet so I wrote my own, I just wanted to share it here incase anyone else has so much trouble.

There's a few problems with these functions, almost all of them revolve around the lack of error control. I thought about adding that in, but since these are for my own use I can be pretty certain I don't need any.

Code:
Function Subnet(strAddress, strMask)
  intSubnetLength = SubnetLength(strMask)
  Subnet = BinaryToString(Left(StringToBinary(strAddress), intSubnetLength) & String(32 - intSubnetLength, "0"))
End Function

Function SubnetLength(strMask)
  strMaskBinary = StringToBinary(strMask)
  SubnetLength = Len(Left(strMaskBinary, InStr(strMaskBinary, "0") - 1))
End Function

Function BinaryToString(strBinary)
  For intOctetPos = 1 To 4
    strOctetBinary = Right(Left(strBinary, intOctetPos * 8), 8)
    intOctet = 0
    intValue = 1
    For intBinaryPos = 1 To Len(strOctetBinary)
      If Left(Right(strOctetBinary, intBinaryPos), 1) = "1" Then intOctet = intOctet + intValue
      intValue = intValue * 2
    Next
    If BinaryToString = Empty Then BinaryToString = CStr(intOctet) Else BinaryToString = BinaryToString & "." & CStr(intOctet)
  Next
End Function

Function StringToBinary(strAddress)
  objAddress = Split(strAddress, ".", -1)
  For Each strOctet In objAddress
    intOctet = CInt(strOctet)
    strOctetBinary = ""
    For x = 1 To 8
      If intOctet Mod 2 > 0 Then
        strOctetBinary = "1" & strOctetBinary
      Else
        strOctetBinary = "0" & strOctetBinary
      End If
      intOctet = Int(intOctet / 2)
    Next
    StringToBinary = StringToBinary & strOctetBinary
  Next
End Function

Last edited by okstorms; 12-29-2004 at 11:56 AM.
Reply With Quote
  #2  
Old 12-31-2004, 09:55 AM
wasodg2 wasodg2 is offline
Registered User
 
Join Date: Dec 2004
Location: Spokane, WAshington
Posts: 16
Sorry to sound stupid, but what does it do?
Reply With Quote
  #3  
Old 12-31-2004, 10:08 AM
okstorms's Avatar
okstorms okstorms is offline
Registered User
 
Join Date: Aug 2004
Location: Wilmington, DE
Posts: 38
Not stupid at all, I've been meaning to clarify, I just wanted to throw it up here ASAP considering how much trouble I had. I should have spent a little more time writing something up before hand.

Basically I've create four functions to manipulate IP addresses. The first function "Subnet" is really the only one that matters, the other three make that one work.

You supply Subnet with an IP address (192.168.10.41) and a mask (255.255.255.0) and it gives you the subnet address (192.168.10.0).

Subnet("192.168.10.41", "255.255.255.0") returns "192.168.10.0"

That example is ridiculously obvious because it's a Class C subnet, but when you get into funky subnets like 255.255.255.248 the subnet address gets a little trickier.

I made this function because I was previously doing string manipulation to get to the subnet address, Trim, Left, Right, Mid, etc. This function actaully converts the IP address and mask to binary (StringToBinary) then does binary masking to provide the subnet address in binary, which is then translated back to a string (BinaryToString).
Reply With Quote
  #4  
Old 12-31-2004, 12:34 PM
wasodg2 wasodg2 is offline
Registered User
 
Join Date: Dec 2004
Location: Spokane, WAshington
Posts: 16
Talking

OK, Now I see, Thanks!
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 12:13 AM.