Click here to Skip to main content
15,896,490 members

IP range caclulator from IP Address/netmask using VBScript

nuckinfutz asked:

Open original thread
All,

I would like to create a calculator using VBS that will accept my input and give me a range of IP address acceptable.

For example:

VBS asks for user input User inputs the IP address/netmask : 214.13.104.128/28

Output: IP Address Range = 214.13.104.129 - 214.13.104.190

I know that there are many online tools you can use, but I will need to use this on systems that cant access the internet and i can not just download a tool and install it on these offline systems.

I have found some code that does most of what i need, but the input boxes ask for Ip then a second input box asks for full subnet. with some help, i have modified the code so that it has only one input box and the format for the input is for example 214.13.104.128/28. also with some help, i have also modified the code so that it splits the input to seperate the Ip and netmask...

---code for split---
a=Split(strInput,"/")
strIP = a(0)
strSN = a(1)
------

now what i need help with is after the split to calculate the netmask from a(1) and run it through the calculations to spit out the results in the following format...

IP 172.16.98.53
SNM 255.255.255.0
SN 172.16.98.0
1st 172.16.98.1
Last 172.16.98.254
Hosts 253

----i should have included the method of calculating the /24 into an actual subnet - adding now----

for the number after the / = a "1" in a binary form of the subnet from left to right - 8 digits per octet and 4 octets and the rest "0"s, so for this example /24 in binary = 11111111.11111111.11111111.00000000. once this has been done, you then calculate the binary into a regular decimal format where from left to right, every "1" represents a number from the following range in sequence then you add them up to get the decimal for of the subnet. - range 128,64,32,16,8,4,2,1

for this example, since all eight places in the binary octects are ones, we would add all numbers in the range together to get 255. since the first 3 octets of the binary number are all ones and the last are zeros, we would end up with the decimal form of the subnet as 255.255.255.0

i hope this didnt complicate the question, but realized i didnt include it in the original question and it may be of help when fixing the code.----

------Entire code----(it works if you enter the Ip and entire subnet - 172.16.98.53/255.255.255.0 but will error if you enter in the format of choice - 172.16.98.53/24 because i havent figured out how to tell it to calculate the subnet from the /24 and then run it through the calculations)

--------Code----
'force script to run in cscript mode
Set objShell = CreateObject("WScript.Shell")
If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
objShell.Run "%comspec% /k cscript //nologo """ & WScript.ScriptFullName & """", 1, False
WScript.Quit
End If

strInput=inputbox("Enter IP / Netmask", "Subnet Calc", "172.16.98.53/24")


a=Split(strInput,"/")
strIP = a(0)
strSN = a(1)

CalcSubnet strIP, strSN
wscript.quit

'-----------------------------------------------------------
function CalcSubnet(strIP,strSNM)
binSNM=str2bin(strSNM)
binIP=str2bin(strIP)

'IP <and> SN to find Network addresses
for c=32 to 1 step -1
temp=(cint(mid(binIP,c, 1)) and cint(mid(binSNM,c, 1))) & temp
next
netwAdd=temp : temp=""

'IP <or> SN to find blocks of all "ones" - these addresss are broadcast addresses
for c=32 to 1 step -1
temp=(cint(mid(binIP,c, 1)) or cint(mid(binSNM,c, 1))) & temp
next
bcCheck=temp : temp=""


'Calc 1st. host address in range (Network Address + 1)
ist=binAdd(netwAdd,string(31, "0")&"1")
'Calc Last host address in range (111111...1 - bcCheck + IP - 0...000001)
lst=binSub(string(32,"1"),bcCheck)
lst=binadd(lst,binIP)
lst=binsub(lst,string(31, "0")&"1" )

wscript.echo "IP "&bin2str(binIP)&vbcrlf&_
"SNM "&bin2str(binSNM)&vbcrlf&_
"SN "&bin2str(netwAdd)&vbcrlf&_
"1st "& bin2str(ist) &vbcrlf&_
"Last "& bin2str(lst) &vbcrlf&_
"Hosts "& Bin2Dec(binsub(lst,ist)) &vbcrlf

end function

'-----------------------------------------------------------

Function Bin2Dec(strBin)
'Plain old binary to decimal function
result = 0
for intIndex = len(strBin) to 1 step -1
strDigit = mid(strBin, intIndex, 1)
if strDigit = "0" then
'do nothing
elseif strDigit = "1" then
result = result + (2 ^ (len(strBin)-intIndex))
else
Bin2Dec = 0
exit for
end if
next
Bin2Dec = result
End Function

'-----------------------------------------------------------

Function bin2str(strBinary)
'special binary to decimal function
'input 32bit binary number
'output 4 octet ip address
For iPosOct = 1 To 4
strOctBin = Right(Left(strBinary, iPosOct * 8), 8)
intOctet = 0
intValue = 1
For iPosBin = 1 To Len(strOctBin)
If Left(Right(strOctBin, iPosBin), 1) = "1" Then
intOctet = intOctet + intValue
end if
intValue = intValue * 2
Next
If bin2str = Empty Then
bin2str = CStr(intOctet)
Else
bin2str = bin2str & "." & CStr(intOctet)
end if
Next
End Function

'-----------------------------------------------------------

Function str2bin(strAddress)
'special decimal to binary function
'input 4 octet ip address
'output 32bit binary number

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

'-----------------------------------------------------------

function binSub(binA,binB)
'subtract one 32bit binary number from another
'binA must be biggest
c=0
for i=32 to 1 step-1
a=cint(mid(binA,i,1))
b=cint(mid(binB,i,1))
if a=0 and b=0 and c=0 then
subt=0 : c=0
elseif a=1 and b=0 and c=0 then
subt=1 : c=0
elseif a=0 and b=1 and c=0 then
subt=1 : c=1
elseif a=1 and b=1 and c=0 then
subt=0 : c=0
elseif a=1 and b=1 and c=1 then
subt=1 : c=1
elseif a=1 and b=0 and c=1 then
subt=0 : c=0
elseif a=0 and b=1 and c=1 then
subt=0 : c=0
elseif a=0 and b=0 and c=1 then
subt=1 : c=1
else
msgbox "This function is only for subtracting 2 32bit binary numbers"
binSub=0 : exit function
end if

total=subt&total
'wscript.echo "a-"&BinA&" "&a&vbcrlf&"b-"&BinB&" "&b&vbcrlf&"subtraction "&subt&vbcrlf&"carry "& c&vbcrlf&"x-"&total&vbcrlf&cvcrlf
next
if c=1 then
msgbox "Error you are subtracting a larger number from a smaller number"&vbcrlf&binA&vbcrlf&binB
end if

binsub=total
end function

'-----------------------------------------------------------

function binAdd(binA,binB)
'add two 32bit binary numbers together
c=0
for i=32 to 1 step-1
a=cint(mid(binA,i,1))
b=cint(mid(binB,i,1))
if a=0 and b=0 and c=0 then
add=0 : c=0
elseif a=1 and b=0 and c=0 then
add=1 : c=0
elseif a=0 and b=1 and c=0 then
add=1 : c=0
elseif a=1 and b=1 and c=0 then
add=0 : c=1
elseif a=1 and b=1 and c=1 then
add=1 : c=1
elseif a=1 and b=0 and c=1 then
add=0 : c=1
elseif a=0 and b=1 and c=1 then
add=0 : c=1
elseif a=0 and b=0 and c=1 then
add=1 : c=0
else
msgbox "Error this function is only for adding 2 32bit binary numbers together"
binAdd=0 : exit function
end if

total=add&total
'wscript.echo "a-"&BinA&" "&a&vbcrlf&"b-"&BinB&" "&b&vbcrlf&"addition "&add&vbcrlf&"carry "& c&vbcrlf&"x-"&total
next
binAdd=total
end function


Thank you.
Tags: VBScript

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900