【问题标题】:IP range calculator from IP Address/netmask using VBScript使用 VBScript 从 IP 地址/网络掩码计算 IP 范围
【发布时间】:2012-11-05 13:09:32
【问题描述】:

我不想问这个问题,但对脚本很陌生,需要帮助。

我想使用 VBS 创建一个计算器,它将接受我的输入并给我一个可接受的 IP 地址范围。

例如:

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

输出:IP Address Range = 214.13.104.129 - 214.13.104.190

我知道您可以使用许多在线工具,但我需要在无法访问互联网的系统上使用它。

【问题讨论】:

  • 为什么不下载一个便携式应用程序?
  • 好吧,下载和传输到其他电脑是不允许的。这需要我可以在其他系统上重新输入代码。谢谢
  • 所以我找到了这个,它看起来正是我需要的,但它在 C# 中,我需要它是一个 .vbs。谁能帮我在.vbs 中重做它? stackoverflow.com/questions/1470792/…谢谢

标签: vbscript ip netmask


【解决方案1】:

不确定您是否仍然需要这个,但为了信息科学和那些正在寻找解决方案的人,我还是发布了这个。我希望它至少可以帮助寻找解决方案的人。如果不是 OP...许多其他 Stack Overflow 用户。

CIDRIP = "192.168.0.1/24" 'must be in CIDR Format as no error checking added
wscript.echo "IP Address Range " & CIDR2IP(CIDRIP, false) & " - " & CIDR2IP(CIDRIP, True)

Function CIDR2IP(ip, high)
    highs = "11111111111111111111111111111111"
    lows = "00000000000000000000000000000000"
    byte0 = Dec2bin(Split(ip, ".")(0))
    byte1 = Dec2bin(Split(ip, ".")(1))
    byte2 = Dec2bin(Split(ip, ".")(2))
    byte3 = Dec2bin(Split(Split(ip, ".")(3), "/")(0))
    Mask = Split(Split(ip, ".")(3), "/")(1)
    bytes = byte0 & byte1 & byte2 & byte3
    rangelow = Left(bytes, Mask) & Right(lows, 32 - Mask)
    rangehigh = Left(bytes, Mask) & Right(highs, 32 - Mask)
    iplow = bin2ip(Left(bytes, Mask) & Right(lows, 32 - Mask))
    iphigh = bin2ip(Left(bytes, Mask) & Right(highs, 32 - Mask))
    If high Then
        CIDR2IP = iphigh
    Else
        CIDR2IP = iplow
    End If
End Function

将 32 位二进制字符串转换为 IP 地址的函数

'expecting input like 00000000000000000000000000000000
Function bin2ip(strbin)
    ip0 = C2dec(Mid(strbin, 1, 8))
    ip1 = C2dec(Mid(strbin, 9, 8))
    ip2 = C2dec(Mid(strbin, 17, 8))
    ip3 = C2dec(Mid(strbin, 25, 8))
    'combines all of the bytes into a single string
    bin2ip = ip0 & "." & ip1 & "." & ip2 & "." & ip3 
End Function

将二进制数转换为十进制数的函数

'expecting input like 00010101
Function C2dec(strbin)
    length = Len(strbin)
    dec = 0
    For x = 1 To length
        binval = 2 ^ (length - x)
        temp = Mid(strbin, x, 1)
        If temp = "1" Then dec = dec + binval
    Next
    C2dec = dec
End Function

十进制转二进制的函数

'Expecting input 0 thru 255
Function Dec2bin(dec)
    Const maxpower = 7
    Const length = 8
    bin = ""
    x = cLng(dec)
    For m = maxpower To 0 Step -1
        If x And (2 ^ m) Then
            bin = bin + "1"
        Else
            bin = bin + "0"
        End If
    Next
    Dec2bin = bin
End Function

额外奖励 - 对于任何了解 MYSQL 并在 MYSQL 中使用 IP 地址的人,他们将立即识别这些功能。

'expecting input like 10.120.44.1 and converts to 175647745
Function INET_NTOA(ip)
    ip0 = Split(ip, ".")(0)
    ip1 = Split(ip, ".")(1)
    ip2 = Split(ip, ".")(2)
    ip3 = Split(ip, ".")(3)
    urlobfs = 0
    urlobfs = ip0 * 256
    urlobfs = urlobfs + ip1
    urlobfs = urlobfs * 256
    urlobfs = urlobfs + ip2
    urlobfs = urlobfs * 256
    urlobfs = urlobfs + ip3
    INET_NTOA = urlobfs
End Function

'expecting input like 175647745 and converts to 10.120.44.1
'Bugs will occur for CLng ceiling numbers. +/- 2,147,483,647
Function INET_ATON(ip)
    Dim ipa()
    ReDim ipa(3)
    n2ip = ip
    For i = 3 To 1 Step -1
        ipa(i) = n2ip Mod 256
        n2ip = n2ip / 256
    Next
    ipa(0) = CInt(n2ip)
    INET_ATON = ipa(0) & "." & ipa(1) & "." & ipa(2) & "." & ipa(3)
End Function

2021 年 9 月更新 流行的请求要求从 CIDR 地址返回可用的 IP 范围,但我不知道...它一直在我的随机 IP 函数列表中。

Function CIDR2IPRANGE(cidr)
    ip = cidr
    highs = "11111111111111111111111111111111"
    lows = "00000000000000000000000000000000"
    byte0 = Dec2bin(Split(ip, ".")(0))
    byte1 = Dec2bin(Split(ip, ".")(1))
    byte2 = Dec2bin(Split(ip, ".")(2))
    byte3 = Dec2bin(Split(Split(ip, ".")(3), "/")(0))
    Mask = Split(Split(ip, ".")(3), "/")(1)
    bytes = byte0 & byte1 & byte2 & byte3
    lownetworkrange = Left(bytes, Mask)
    lowhostrange = Right(lows, 31 - Mask) & "1"
    rangelow = lownetworkrange + lowhostrange
    highnetworkrange = Left(bytes, Mask)
    highhostrange = Right(highs, 31 - Mask) & "0"
    rangehigh = highnetworkrange + highhostrange
    iplow = bin2ip(rangelow)
    iphigh = bin2ip(rangehigh)
    CIDR2IP = iplow & " - " & iphigh
End Function

干杯!

【讨论】:

  • 绝对出色@Steve Kline!
  • 获得此范围内所有 IP 的列表真是太棒了...实际上试图在 VBScript 中对此进行编码,但到目前为止我没有机会 ;)
【解决方案2】:

作为出色的@Steve kline 答案的补充,对于那些希望获取 IP 范围内所有 IP 地址列表的人,这里是从 http://www.intelliadmin.com/index.php/2012/04/use-vb-script-to-list-a-range-of-ip-addresses/ 抓取的脚本(带有Wayback Machine 的帮助)。

我认为iS4iS3iS2 变量应该改为0而不是1

'*********************************
'* IntelliAdmin, LLC 2012        *
'* http://www.intelliadmin.com   *
'*********************************

'The start and end ip range
StartIP = "10.10.29.129"
EndIP = "10.10.29.240"


'This function contains the code that will execute
'against the remote hosts

Function ExecuteAction(HostName)

 'Put your code here and use the variable hostname
 WScript.Echo "Hostname: " & HostName

End Function


'Parsing code below

Dim iS1,iS2,iS3,iS4
Dim iE1,iE2,iE3,iE4
Dim iC1,iC2,iC3,iC4


'Convert our starting ip to octets

IPList = Split(StartIP, ".")

iS1 = cint(IPList(0))
iS2 = cint(IPList(1))
iS3 = cint(IPList(2))
iS4 = cint(IPList(3))

'Convert our endingip to octets

IPList = Split(EndIP, ".")

iE1 = cint(IPList(0))
iE2 = cint(IPList(1))
iE3 = cint(IPList(2))
iE4 = cint(IPList(3))

'Loop through our list of IP addresses

for iC1=iS1 to 255
 for iC2=iS2 to 255
  for iC3=iS3 to 255
   for iC4=iS4 to 255
    ExecuteAction(iC1 & "." & iC2 & "." & iC3 & "." & iC4)
    if ((iC1>=iE1) and (iC2>=iE2) and (iC3>=iE3) and (iC4>=iE4)) then
     iC1=255
     iC2=255
     iC3=255
     iC4=255
    end if
   next
   iS4=1
  next
  iS3=1
 next
 iS2=1
next

【讨论】:

  • 我同意你的观点@SteveKline,但到目前为止它正在工作。感谢您添加到您最初的赞成答案^!^
猜你喜欢
  • 2010-12-01
  • 2016-01-15
  • 2012-10-06
  • 1970-01-01
  • 2017-10-08
  • 2012-12-28
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多