'Subnet Calculator V1 'Script by MJP 2010 'usage cscript VBScriptSubnetCalcV2 > output.txt '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 'a bit of test code to feed a range of addresses into script for testing ' snm="255.255.192.0" ' for iCount=0 to 255 ' ip="172.16.17."&iCount ' CalcSubnet ip, snm ' next strIP=inputbox("Enter IP", "MJP Subnet Calc", "172.16.98.53") strSN=inputbox("Enter Subnet Mask", "MJP Subnet Calc", "255.255.224.0") CalcSubnet strIP, strSN wscript.quit '----------------------------------------------------------- function CalcSubnet(strIP,strSNM) binSNM=str2bin(strSNM) binIP=str2bin(strIP) 'IP 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 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 "&binIP&" "&bin2str(binIP)&vbcrlf&_ "SNM "&binSNM&" "&bin2str(binSNM)&vbcrlf&_ "SN "&netwAdd&" "&bin2str(netwAdd)&vbcrlf&_ "1st "&ist &" "& bin2str(ist) &vbcrlf&_ "Last "&lst &" "& bin2str(lst) &vbcrlf&_ "Hosts "&binsub(lst,ist) &" "& 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 '-----------------------------------------------------------