Hello,
Has anybody written anything to calculate IP subnets / Valid Host Ranges?
I've starting working on a project that I need the calculations it. I started creating a table based version but see that there will be some problems with it (IE invalid IPs with in the range will not be removed).
Below is what I have exporting to a text file.
If anybody has something already or knows how to go about actually doing the binary conversions to do true calculations please share 8).
Thanks, Danzig
Has anybody written anything to calculate IP subnets / Valid Host Ranges?
I've starting working on a project that I need the calculations it. I started creating a table based version but see that there will be some problems with it (IE invalid IPs with in the range will not be removed).
Below is what I have exporting to a text file.
Code:
strDestinationFile = InputBox("Please Enter The Path To The Destination File:","*IP Network Calc*")
Dim oFso
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFso = CreateObject("Scripting.FileSystemObject")
oFso.CreateTextFile (strDestinationFile)
Set MakeDestFile = oFso.GetFile(strDestinationFile)
Set WriteDestFile = MakeDestFile.OpenAsTextStream(2, True)
frm_S1 = 10
frm_S2 = 80
frm_S3 = 92
frm_S4 = 1
Select Case frm_S1
Case 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126
strClass = "A"
Case 127
strClass = "INVALID"
Case 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191
strClass = "B"
Case 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223
strClass = "C"
Case 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239
strClass ="D"
Case 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254
strClass = "E"
Case Else
strClass = "INVALID"
End Select
'WScript.Echo "The IP Class is: "&strClass
strdb_NetworkMask = "255.255.254.0"
If strClass = "A" Then
Select Case strdb_NetworkMask
Case "255.192.0.0"
strCidr = "/10"
strSubnetHost = "4194302"
Case "255.224.0.0"
strCidr = "/11"
strSubnetHost = "2097150"
Case "255.240.0.0"
strCidr = "/12"
strSubnetHost = "1048574"
Case "255.248.0.0"
strCidr = "/13"
strSubnetHost = "524286"
Case "255.252.0.0"
strCidr = "/14"
strSubnetHost = "262142"
Case "255.254.0.0"
strCidr = "/15"
strSubnetHost = "131070"
Case "255.255.0.0"
strCidr = "/16"
strSubnetHost = "65534"
Case "255.255.128.0"
strCidr = "/17"
strSubnetHost = "32766"
Case "255.255.192.0"
strCidr = "/18"
strSubnetHost = "16382"
Case "255.255.224.0"
strCidr = "/19"
strSubnetHost = "8190"
Case "255.255.240.0"
strCidr = "/20"
strSubnetHost = "4094"
Case "255.255.248.0"
strCidr = "/21"
strSubnetHost = "2046"
Case "255.255.252.0"
strCidr = "/22"
strSubnetHost = "1022"
Case "255.255.254.0"
strCidr = "/23"
strSubnetHost = "510"
Case "255.255.255.0"
strCidr = "/24"
strSubnetHost = "254"
Case "255.255.255.128"
strCidr = "/25"
strSubnetHost = "126"
Case "255.255.255.192"
strCidr = "/26"
strSubnetHost = "62"
Case "255.255.255.224"
strCidr = "/27"
strSubnetHost = "30"
Case "255.255.255.240"
strCidr = "/28"
strSubnetHost = "14"
Case "255.255.255.248"
strCidr = "/29"
strSubnetHost = "6"
Case "255.255.255.252"
strCidr = "/30"
strSubnetHost = "2"
Case Else
strCidr = "/UNKNOWN"
strSubnetHost = "UNKNOWN"
End Select
End If
If strClass = "B" Then
Select Case strdb_NetworkMask
Case "255.255.192.0"
strCidr = "/18"
strSubnetHost = "16382"
Case "255.255.224.0"
strCidr = "/19"
strSubnetHost = "8190"
Case "255.255.240.0"
strCidr = "/20"
strSubnetHost = "4094"
Case "255.255.248.0"
strCidr = "/21"
strSubnetHost = "2046"
Case "255.255.252.0"
strCidr = "/22"
strSubnetHost = "1022"
Case "255.255.254.0"
strCidr = "/23"
strSubnetHost = "510"
Case "255.255.255.0"
strCidr = "/24"
strSubnetHost = "254"
Case "255.255.255.128"
strCidr = "/25"
strSubnetHost = "126"
Case "255.255.255.192"
strCidr = "/26"
strSubnetHost = "62"
Case "255.255.255.224"
strCidr = "/27"
strSubnetHost = "30"
Case "255.255.255.240"
strCidr = "/28"
strSubnetHost = "14"
Case "255.255.255.248"
strCidr = "/29"
strSubnetHost = "6"
Case "255.255.255.252"
strCidr = "/30"
strSubnetHost = "2"
Case Else
strCidr = "/UNKNOWN"
strSubnetHost = "UNKNOWN"
End Select
End If
If strClass = "C" Then
Select Case strdb_NetworkMask
Case "255.255.255.192"
strCidr = "/26"
strSubnetHost = "62"
Case "255.255.255.224"
strCidr = "/27"
strSubnetHost = "30"
Case "255.255.255.240"
strCidr = "/28"
strSubnetHost = "14"
Case "255.255.255.248"
strCidr = "/29"
strSubnetHost = "6"
Case "255.255.255.252"
strCidr = "/30"
strSubnetHost = "2"
Case Else
strCidr = "/UNKNOWN"
strSubnetHost = "UNKNOWN"
End Select
End If
If strClass = "D" Then
strCidr = "/UNKNOWN"
strSubnetHost = "UNKNOWN"
End If
If strClass = "E" Then
strCidr = "/UNKNOWN"
strSubnetHost = "UNKNOWN"
End If
WScript.Echo "The Class: "&strClass&" Network of:"&frm_S1&"."&frm_S2&"."&frm_S3&"."&frm_S4&strCidr&" ("&strdb_NetworkMask&") Will Genterate "&strSubnetHost&" Valid Hosts"
frm_E1 = 10
frm_E2 = 80
frm_E3 = 93
frm_E4 = 255
strD1 = frm_E1 - frm_S1
strD2 = frm_E2 - frm_S2
strD3 = frm_E3 - frm_S3
strD4 = frm_E4 - frm_S4
strEnded = 0
If strD1 >0 Then
For i1 = frm_S1 To frm_S1+strD1
If strEnded = 1 Then
Exit For
End If
If strD1 >0 Then
strEnd2 = 255
strStart2 = 1
Else
strEnd2 = frm_S2+strD2
strStart2 = frm_S2
End If
For i2 = strStart2 To strEnd2
If strEnded = 1 Then
Exit For
End If
If strD2 >0 Then
strEnd3 = 255
strStart3 =1
Else
strEnd3 = frm_S3+strD3
strStart3 = frm_S3
End If
For i3 = strStart3 To strEnd3
If strEnded = 1 Then
Exit For
End If
If strD3 >0 Then
strEnd4 = 255
Else
strEnd4 = frm_S4+strD4
End If
For i4 = 1 To strEnd4
strIP=i1&"."&i2&"."&i3&"."&i4
If strIP = frm_E1&"."&frm_E2&"."&frm_E3&"."&frm_E4 Then
WriteDestFile.WriteLine(strIP)
strEnded = 1
Exit For
Else
WriteDestFile.WriteLine(strIP)
End If
Next
Next
Next
Next
Else
If strD2 >0 Then
For i2 = frm_S2 To frm_S2+strD2
If strEnded = 1 Then
Exit For
End If
If strD2 >0 Then
strEnd3 = 255
strStart3 =1
Else
strEnd3 = frm_S3+strD3
strStart3 =frm_3
End If
For i3 = strStart3 To strEnd3
If strEnded = 1 Then
Exit For
End If
If strD3 >0 Then
strEnd4 = 255
Else
strEnd4 = frm_S4+strD4
End If
For i4 = 1 To strEnd4
strIP=frm_S1&"."&i2&"."&i3&"."&i4
If strIP = frm_E1&"."&frm_E2&"."&frm_E3&"."&frm_E4 Then
WriteDestFile.WriteLine(strIP)
strEnded = 1
Exit For
Else
WriteDestFile.WriteLine(strIP)
End If
Next
Next
Next
Else
If strD3 >0 Then
For i3 = frm_S3 To frm_S3+strD3
If strEnded = 1 Then
Exit For
End If
If strD3 >0 Then
strEnd4 = 255
Else
strEnd4 = frm_S4+strD4
End If
For i4 = 1 To strEnd4
strIP=frm_S1&"."&frm_S2&"."&i3&"."&i4
If strIP = frm_E1&"."&frm_E2&"."&frm_E3&"."&frm_E4 Then
WriteDestFile.WriteLine(strIP)
strEnded = 1
Exit For
Else
WriteDestFile.WriteLine(strIP)
End If
Next
Next
Else
If strD4 >0 Then
For i4 = 1 To 255
strIP=frm_S1&"."&frm_S2&"."&frm_S3&"."&i4
If strIP = frm_E1&"."&frm_E2&"."&frm_E3&"."&frm_E4 Then
WriteDestFile.WriteLine(strIP)
Exit For
Else
WriteDestFile.WriteLine(strIP)
End If
Next
End If
End If
End If
End If
If anybody has something already or knows how to go about actually doing the binary conversions to do true calculations please share 8).
Thanks, Danzig