1 (изменено: MikhailSM, 2011-03-22 18:18:33)

Тема: VBS: Работа с маской подсети

Подскажите, пожалуйста, есть ли возможность в VBS указать маску подсети к примеру /24 и сделать перебор всех айпишников циклом. С циклом понятно, а вот как сделать так чтобы скрипт менял айпишник не знаю. Алгоритм работы скрипта:
1. Задать маску
2. Посчитать какой диапозон айпишников будет и поместить их все в массив
3. Для каждого айпишника выполнить функцию.

2

Re: VBS: Работа с маской подсети

Нашел сам.

'this scipt will do some very fast IP/subnet-calculations
'instead of using any string-operations here like other known algorithms I am using optimized BIT-operations
'carsten.giese@googlemail.com, 03.10.2010

option explicit

'prepare an array for BIT-operations:
dim v,i,bit(32)
v=1
for i=0 to 31
bit(i)=NumberToLong(v)
v=v+v
next

'here is a small demo how to work with the functions:

dim ip,mask,size,sn,ip1,ipN
ip="192.168.100.45"
mask="255.255.255.240"
sn=subnet(ip,mask)
wscript.echo "subnet=" & IpString(sn) & "\" & maskBit(mask)
size=subnetSize(mask)
wscript.echo "size =" & size
ip1=sn or 1
ipN=sn or size
wscript.echo "first IP in subnet=" & IpString(ip1)
wscript.echo "last IP in subnet=" & IpString(ipN)

wscript.echo vbCrLf & "sample 1: listing all IPs in subnet..."
for i=ip1 to ipN
wscript.echo IpString(i)
next

wscript.echo vbCrLf & "sample 2:checking 65535 IPs for the same subnet..."
dim a,b,o1,o2,ipVal
a=timer
for o1=0 to 255
for o2=0 to 255
ip="192.168." & o1 & "." & o2
ipVal=IpValue(ip)
if ipVal>=ip1 then
if ipVal<=ipN then wscript.echo ip
end if
next
next
b=timer
wscript.echo "duration in seconds: " & b-a

'---------------------------------- functions ----------------------------------
function subnetSize(mask)
'returns the number of IPs for a given subnet-mask
'will only work with MS/CISCO-compliant masks with leading 1-s and trailing 0-s, but not for masks like 255.0.255.0
subnetSize=-IpValue(mask)-2
end function

function subnet(IP,mask)
'converts a given IP-string and subnetmask-string into a number from type LONG representing the subnet
subnet=IpValue(IP) and IpValue(mask)
end function

function IpValue(IP)
'converts an IP-string into a number from type LONG (for bit-operators like AND/OR)
IpValue=NumberToLong(IpToNumber(IP))
end function

function IpToNumber(IP)
'converts an IP-string into a large positive number between &H00000000 and &HFFFFFFFF 
dim octet
for each octet in split(IP,".")
IpToNumber=IpToNumber * 256 + CByte(octet)
next
end function

function NumberToLong(n)
'converts a large number between &H00000000 and &HFFFFFFFF into type LONG
if n>&H7FFFFFFF then 
numberToLong=n-4294967296
else
numberToLong=n
end if
end function

function IpString(n)
'converts a n IP-value from type LONG into a readable IP-string
dim octet,octetValue,bOffset
for octet=0 to 3
octetValue=0
for b=0 to 7
if n and bit(bOffset+b) then octetValue=octetValue + bit(b)
next
bOffset=bOffset+8
if octet=0 then
IpString=cstr(octetValue)
else
IpString= cstr(octetValue) & "." & IpString
end if
next
end function

function maskbit(mask)
dim m,b
m = IpValue(mask)
for b=0 to 31
if (m and bit(b)) then exit for
next
maskBit=32-b
end function

3

Re: VBS: Работа с маской подсети

MikhailSM, посмотрите на это. Опрос производится без указания маски — маска и диапазон адресов подсети берётся по IP адресу первого подключенного сетевого адаптера.