#1
|
||||
|
||||
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 10:56 AM. |
#2
|
|||
|
|||
Sorry to sound stupid, but what does it do?
|
#3
|
||||
|
||||
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). |
#4
|
|||
|
|||
![]()
OK, Now I see, Thanks!
|
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|