まだこれが必要かどうかはわかりませんが、情報科学と解決策を探している人のために、とにかくこれを投稿しています。少なくとも解決策を探している人に役立つことを願っています。OPでない場合...他の多くのStackOverflowユーザー。
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
2進数を10進数に変換する関数
'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
10進数を2進数に変換する関数
'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
乾杯!