Click here to Skip to main content
12,503,676 members (29,502 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VBScript
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
Posted 15-Nov-12 1:15am
Updated 15-Nov-12 2:33am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Looks like you're getting somewhere. How I'd do it:

0. work in 32bit (unsigned) integers
1. get starting ip to int32
2. get mask to int32 eg /26 => FFFFFFC0
3. get COMPLEMENT of mask (xor FFFFFFFF) => 000003F
4. AND 1 and 2 to get first ip
5. OR 1 and 3 to get last ip
6. weed out broadcase/null addresses if required
7. translate back into dotted decimal
8. done!

Cheers,
Peter
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100