Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IP Subnet Caclulations

Status
Not open for further replies.

DANZIG

Technical User
Mar 8, 2001
142
US
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.
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
 
Ok Scratch that I had to re-write to follow the ipv4 rules. Thought I'd share the finished product incase anybody else would need it. Its probably not the tightest code but it works very well.

Happy Friday, Enjoy
Code:
'Subent-Creator.Vbs Rev.1
'By Danzig 1/27/2005
'
'	Creates a valid ipv4 subnet based on ipv4 subnet rules
'	given the network address and the subent mask to be used.

Public WriteDestFile, strDestinationFile
Dim oWsh
Set oWsh = CreateObject("WScript.Shell")
MakeFile()
	
	strNetworkIPAddress = InputBox("Please Enter The Network IP Address:","*Subnet-Creator.Vbs*")
	strdb_IPStartingArray = Split(strNetworkIPAddress, ".")
	For Each strOct In strdb_IPStartingArray
		If strOct >=256 Then
			oWsh.Popup "Invalid IP Network or Subnet Mask!",,"*Subnet-Creator.Vbs*",16
			WScript.Quit
		ElseIf strOct <0 Then
			oWsh.Popup "Invalid IP Network or Subnet Mask!",,"*Subnet-Creator.Vbs*",16
			WScript.Quit
		End If
	Next
		
	strBinMask = BinMask()
		strCidrMask = InStr(1, strBinMask, "0")-1
		strHostBits=32-strCidrMask
		strMaxHosts=(2^strHostBits)-2
		For Idx1 = 0 To strMaxHosts+1
			If Idx1 >0 Then
				strdb_IPStartingArray(3)=strdb_IPStartingArray(3)+1
				If strdb_IPStartingArray(3) = 256 Then
					strdb_IPStartingArray(2) = strdb_IPStartingArray(2)+1
					strdb_IPStartingArray(3) = 0
					If strdb_IPStartingArray(2) = 256 Then
						strdb_IPStartingArray(1) = strdb_IPStartingArray(1)+1
						strdb_IPStartingArray(2) = 0
						If strdb_IPStartingArray(1) = 256 Then
							strdb_IPStartingArray(0) = strdb_IPStartingArray(0)+1
							strdb_IPStartingArray(1) = 0
							If strdb_IPStartingArray(0) = 256 Then
								oWsh.Popup "Invalid IP Network or Subnet Mask!",,"*Subnet-Creator.Vbs*",16
								WScript.Quit
							End If
						End If
					End If
				End If
			End If
			
			strBinIPOct1=DecToBin(Int(strdb_IPStartingArray(0)))
			strBinIPOct2=DecToBin(Int(strdb_IPStartingArray(1)))
			strBinIPOct3=DecToBin(Int(strdb_IPStartingArray(2)))
			strBinIPOct4=DecToBin(Int(strdb_IPStartingArray(3)))
				strBinIP=strBinIPOct1&strBinIPOct2&strBinIPOct3&strBinIPOct4
			
			strIP =BinToDec(strBinIPOct1)&"."&BinToDec(strBinIPOct2)&"."&BinToDec(strBinIPOct3)&"."&BinToDec(strBinIPOct4)
			
			If Right(strBinIP,strHostBits) = Right("00000000000000000000000000000000",strHostBits) Then
				If Idx1 = 0 Then
					WriteDestFile.WriteLine(strIP&" - *Reserved Address For Network*")
				End If
			ElseIf Right(strBinIP,strHostBits) = Right("11111111111111111111111111111111",strHostBits) Then
				If Idx1 = strMaxHosts+1 Then
					WriteDestFile.WriteLine(strIP&" - *Reserved Address For Network Broadcast*")
				End If
			Else
				WriteDestFile.WriteLine(strIP)
			End If
		Next
		oWsh.Run "Notepad.exe "&strDestinationFile, 9
		WScript.Quit
	
Function MakeFile()

	Dim oFso
	Const ForReading = 1, ForWriting = 2, ForAppending = 8 
		Set oFso = CreateObject("Scripting.FileSystemObject")
		
		strDestinationFile = InputBox("Please Enter The Path To The Destination File:","*Subnet-Creator.Vbs*")
		oFso.CreateTextFile (strDestinationFile)
			Set MakeDestFile = oFso.GetFile(strDestinationFile)
			Set WriteDestFile = MakeDestFile.OpenAsTextStream(2, True)

End Function

Function BinMask()

	strSubNetMask = InputBox("Please Enter The Subnet Mask To Be Used:","*Subnet-Creator.Vbs*")
	strdb_IPMaskArray = Split(strSubNetMask, ".")
	For Each strOct In strdb_IPMaskArray
		If strOct >=256 Then
			oWsh.Popup "Invalid IP Network or Subnet Mask!",,"*Subnet-Creator.Vbs*",16
			WScript.Quit
		ElseIf strOct <0 Then
			oWsh.Popup "Invalid IP Network or Subnet Mask!",,"*Subnet-Creator.Vbs*",16
			WScript.Quit
		End If
	Next
	strBinMaskOct1=DecToBin(Int(strdb_IPMaskArray(0)))
	strBinMaskOct2=DecToBin(Int(strdb_IPMaskArray(1)))
	strBinMaskOct3=DecToBin(Int(strdb_IPMaskArray(2)))
	strBinMaskOct4=DecToBin(Int(strdb_IPMaskArray(3)))
		BinMask=strBinMaskOct1&strBinMaskOct2&strBinMaskOct3&strBinMaskOct4
	
End Function

Function DecToBin(intDec)

	Dim strResult
	Dim intValue
	Dim intExp
	
	strResult = ""
	intValue = intDec
	intExp = 65536
	While intExp >= 1
		If intValue >= intExp Then
			intValue = intValue - intExp
			strResult = strResult&"1"
		Else
			strResult = strResult&"0"
		End If
		intExp = intExp/2
	Wend
	DecToBin = Right(strResult,8)
	
End Function

Function BinToDec(strBin)

	Dim lngResult
	Dim intIdx
	
	lngResult = 0
	For intIdx = len(strBin) to 1 step -1
		strDigit = mid(strBin, intIdx, 1)
		Select Case strDigit
			Case "0"
				
			Case "1"
				lngResult = lngResult+(2^(len(strBin)-intIdx))
			Case Else
				lngResult = 0
				intIdx = 0
		End Select
	Next
	BinToDec = lngResult
	
End Function


Thanks, Danzig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top