使用VBScript从IP地址/网络掩码获得IP范围计算器 [英] IP range caclulator from IP Address/netmask using VBScript

查看:139
本文介绍了使用VBScript从IP地址/网络掩码获得IP范围计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我想使用VBS创建一个计算器,该计算器将接受我的输入并提供可接受的IP地址范围.

例如:

VBS要求用户输入用户输入IP地址/网络掩码:214.13.104.128/28

输出:IP地址范围= 214.13.104.129-214.13.104.190

我知道您可以使用许多在线工具,但是我将需要在无法访问Internet的系统上使用它,而我不能只是下载工具并将其安装在这些脱机系统上.

我发现一些代码可以满足我的大部分需求,但是输入框要求输入IP,然后第二个输入框要求完整的子网.在一些帮助下,我修改了代码,使其仅具有一个输入框,并且输入格式为例如214.13.104.128/28.在一些帮助下,我还修改了代码,以便将输入拆分为IP和网络掩码...

---拆分代码---
a = Split(strInput,"/")
strIP = a(0)
strSN = a(1)
------

现在我需要帮助的是拆分后从a(1)计算网络掩码并通过计算运行它,以下列格式吐出结果...

IP 172.16.98.53
SNM 255.255.255.0
SN 172.16.98.0
1st 172.16.98.1
最后172.16.98.254
主机253

---- i应该已经包括了将/24计算到实际子网中的方法-现在添加----

/后面的数字表示子网的二进制形式(从左到右)-每个八位位组8位数字和4个八位位组,其余为"0",因此对于此示例,/24 in binary = 11111111.11111111.11111111.00000000 .完成此操作后,您便可以将二进制文件计算为常规的十进制格式,其中从左到右,每个"1"代表依次从以下范围开始的数字,然后将它们加起来即可得到子网的十进制数. -范围128,64,32,16,8,4,2,1

在此示例中,由于二进制八进制数中的所有八个位置均为1,因此我们将范围中的所有数字加在一起得出255.由于二进制数的前3个八进制数均为1,最后一个为零,所以最终子网的十进制形式为255.255.255.0

我希望这不会使问题复杂化,但是意识到我没有将它包含在原始问题中,并且在修复代码时可能会有所帮助.
------整个代码----((如果您输入IP和整个子网-172.16.98.53/255.255.255.0是有效的,但是如果您选择以下格式-172.16.98.53/24则将出错,因为还没有弄清楚如何告诉它从/24计算子网,然后通过计算运行它)

--------代码----
''强制脚本以cscript模式运行
设置objShell = CreateObject("WScript.Shell")
如果Instr(1,WScript.FullName,"CScript",vbTextCompare)= 0然后
objShell.Run%comspec%/k cscript//nologo""& WScript.ScriptFullName& "",1,False
WScript.Quit
如果结束

strInput = inputbox(输入IP/网络掩码",子网计算","172.16.98.53/24")


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

CalcSubnet strIP,strSN
wscript.quit

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

``IP∧ SN查找网络地址
对于c = 32到1步-1
temp =(cint(mid(binIP,c,1))和cint(mid(binSNM,c,1)))& temp
下一个
netwAdd = temp:temp ="

``IP<或> SN查找所有"1"的块-这些地址是广播地址
对于c = 32到1步-1
temp =(cint(mid(binIP,c,1))或cint(mid(binSNM,c,1)))& temp
下一个
bcCheck = temp:temp ="


''Calc 1st.范围内的主机地址(网络地址+ 1)
ist = binAdd(netwAdd,string(31,"0")&"1")
''计算范围内的最后一个主机地址(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& __
第一" bin2str(ist)& vbcrlf& __
最后"& bin2str(lst)& vbcrlf& __
主机"& Bin2Dec(binsub(lst,ist))& vbcrlf

最终功能

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

函数Bin2Dec(strBin)
''将旧的二进制转换为十进制函数
结果= 0
对于intIndex = len(strBin)到1步-1
strDigit = mid(strBin,intIndex,1)
如果strDigit ="0",则
''什么都不做
elseif strDigit ="1"然后
结果=结果+(2 ^(len(strBin)-intIndex))
其他
Bin2Dec = 0
退出
如果
结束 下一个
Bin2Dec =结果
最终功能

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

函数bin2str(strBinary)
''特殊的二进制到十进制函数
''输入32位二进制数
''输出4个八位位组ip地址
对于iPosOct = 1到4
strOctBin =右(左(strBinary,iPosOct * 8),8)
intOctet = 0
intValue = 1
对于iPosBin = 1到Len(strOctBin)
如果Left(Right(strOctBin,iPosBin),1)="1",则
intOctet = intOctet + intValue
如果
结束 intValue = intValue * 2
下一个
如果bin2str =空,则
bin2str = CStr(intOctet)
其他
bin2str = bin2str& ." & CStr(intOctet)
如果
结束 下一个
最终功能

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

函数str2bin(strAddress)
''特殊的十进制转换为二进制函数
''输入4个八位位组IP地址
''输出32位二进制数

objAddress = Split(strAddress,.")
对于objAddress中的每个strOctet
intOctet = CInt(strOctet)
strOctBin ="
对于x = 1至8
如果intOctet Mod 2> 0然后
strOctBin ="1"& strOctBin
其他
strOctBin ="0"& strOctBin
如果
结束 intOctet = Int(intOctet/2)
下一个
str2bin = str2bin& strOctBin
下一个
最终功能

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

函数binSub(binA,binB)
''从另一个减去一个32位二进制数
``binA必须是最大的
c = 0
对于i = 32到1 step-1
a = cint(mid(binA,i,1))
b = cint(mid(binB,i,1))
如果a = 0和b = 0且c = 0,则
subt = 0:c = 0
else如果a = 1且b = 0且c = 0则
subt = 1:c = 0
else如果a = 0且b = 1且c = 0则
subt = 1:c = 1
else如果a = 1且b = 1且c = 0则
subt = 0:c = 0
else如果a = 1且b = 1且c = 1则
subt = 1:c = 1
else如果a = 1且b = 0且c = 1则
subt = 0:c = 0
else如果a = 0且b = 1且c = 1则
subt = 0:c = 0
else如果a = 0且b = 0且c = 1则
subt = 1:c = 1
其他
msgbox此功能仅用于减去2个32位二进制数"
binSub = 0:退出函数
如果
结束
total = subt& total
''wscript.echo"a-"& BinA&"& a& vbcrlf&"b-"& BinB&"减法& subt& vbcrlf&" carry& ; c& vbcrlf&"x-"&& vbcrlf& cvcrlf
下一个
如果c = 1则
msgbox错误是您从较小的数字中减去了较大的数字"& vbcrlf& binA& vbcrlf& binB
如果
结束
binsub = total
最终功能

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

函数binAdd(binA,binB)
''将两个32位二进制数字加在一起
c = 0
对于i = 32到1 step-1
a = cint(mid(binA,i,1))
b = cint(mid(binB,i,1))
如果a = 0和b = 0且c = 0,则
add = 0:c = 0
else如果a = 1且b = 0且c = 0则
add = 1:c = 0
else如果a = 0且b = 1且c = 0则
add = 1:c = 0
else如果a = 1且b = 1且c = 0则
add = 0:c = 1
else如果a = 1且b = 1且c = 1则
加= 1:c = 1
else如果a = 1且b = 0且c = 1则
add = 0:c = 1
else如果a = 0且b = 1且c = 1则
add = 0:c = 1
else如果a = 0且b = 0且c = 1则
add = 1:c = 0
其他
msgbox错误,此功能仅适用于将2个32位二进制数字加在一起"
binAdd = 0:退出功能
如果
结束
total =添加和总计
''wscript.echo"a-"& BinA&"& a& vbcrlf&"b-"& BinB&"& b& vbcrlf&附加"& add& vbcrlf&携带"& ; c& vbcrlf&"x-"&total
下一个
binAdd =总计
最终功能


谢谢.

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.

推荐答案

看起来像你在找东西.我该怎么做:

0.使用32位(无符号)整数
1.开始使用ip到int32
2.获取mask到int32,例如/26 => FFFFFFC0
3.得到掩码的补全(xor FFFFFFFF)=>. 000003F
4. AND 1和2获得第一个ip
5.或1和3获得最后一个ip
6.如果需要的话,清除大写/空地址
7.转换回点分十进制
8.完成!

干杯,
彼得
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


这篇关于使用VBScript从IP地址/网络掩码获得IP范围计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆