Vous êtes sur la page 1sur 7

' MultiPing.vbs v2.

0
'
' Script to ping a range of IP Addresses and
' write results to an Excel Workbook
'
' Noel McGran (23/07/04)
'User Input of IP range
Message = "Please enter start IP:"
Title = "Start Range"
StartIP = InputBox(Message, Title)
xx = StartIP
Good = ValidIP(xx)
Do while Good = "invalid"
Message = StartIP & " is not a valid IP." & _
vbCrLf & "Please re-enter start IP:"
Title = "Start Range"
StartIP = InputBox(Message, Title)
xx = StartIP
Good = ValidIP(xx)
Loop
Message = "Please enter end IP:"
Title = "End Range"
EndIP = InputBox(Message, Title)
yy = EndIP
Good = ValidIP(yy)
Do while Good = "invalid"
Message = EndIP & " is not a valid IP." & _
vbCrLf & "Please re-enter end IP:"
Title = "End Range"
EndIP = InputBox(Message, Title)
yy = EndIP
Good = ValidIP(yy)
Loop
xx = StartIP
yy = EndIP
Good = GreaterIP(xx,yy)
Do while Good = "before"
Message = EndIP & " is " & Good & " " & StartIP _
& vbCrLf & "Please re-enter End IP:"
Title = "End Range"
EndIP = InputBox(Message, Title)
xx = StartIP
yy = EndIP
Good = GreaterIP(xx,yy)
Loop
msgText = "IP range is " & StartIP & " to " & EndIP _
& vbCrLF & "Continue?"
Select Case MsgBox(msgText, VBOKCancel)
Case 1
TempFilename = "D:\Romeu\Scripts\tmp\IPtemp.txt"
notfound = "No Response"
' Spreadsheet file to be created.
strExcelPath = "D:\Romeu\Scripts\tmp\MultiPing.xls"
' Bind to Excel object.
Set objExcel = CreateObject("Excel.Application")
' Create a new workbook.
objExcel.Workbooks.Add
' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
objSheet.Name = "Active IPs"
' Populate spreadsheet cells with user attributes.
objSheet.Cells(1, 1).Value = "IP Address"
objSheet.Cells(1, 2).Value = "Computer Name"
' Format the spreadsheet.
objSheet.Range("A1:B1").Font.Bold = True
objSheet.Select
objSheet.Range("A2:B2").Select
objExcel.ActiveWindow.FreezePanes = True
objExcel.Columns(1).ColumnWidth = 15
objExcel.Columns(2).ColumnWidth = 30

LF = chr(10)
const ForReading = 1, ForWriting = 2, ForAppending = 3
currentIP = StartIP
set shell = CreateObject ("wscript.shell")
Set FS = CreateObject ("scripting.FileSystemObject")
j = 2
Do
command = "cmd /C ping -a " & currentIP & " > " _
& TempFilename
x = shell.run(command, 0, true)
set f = fs.OpenTextFile(tempfilename, ForReading, true)
fline = f.readline
fline = f.readline
l1 = instr (fline, " ")
l2 = instr (fline, "[")
if l2 = 0 then
mname = notfound
else
mname = mid (fline, l1, l2-l1)
end if
objSheet.Cells(j, 1).Value = currentIP
objSheet.Cells(j, 2).Value = mname
f.close
xx = currentIP
currentIP = newIP(xx)
j = j+1
Loop Until currentIP = EndIP
' process last IP
command = "cmd /C ping -a " & currentIP & " > " _
& TempFilename
x = shell.run(command, 0, true)
set f = fs.OpenTextFile(tempfilename, ForReading, true)
fline = f.readline
fline = f.readline
l1 = instr (fline, " ")
l2 = instr (fline, "[")
if l2 = 0 then
mname = notfound
else
mname = mid (fline, l1, l2-l1)
end if
objSheet.Cells(j, 1).Value = currentIP
objSheet.Cells(j, 2).Value = mname
f.close

' Save the spreadsheet and close the workbook.


objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
' Quit Excel.
objExcel.Application.Quit
' Clean Up
Set objSheet = Nothing
Set objExcel = Nothing
wscript.echo "Done!"
Case 2
wscript.echo "Goodbye."
Set objSheet = Nothing
Set objExcel = Nothing
End Select

'------------
'function for increasing the IP number
'------------
function newip(xx)
dim n,n1,n2,n3,n4,v1,v2,v3,v4
n = 1
n0 = n
while mid (xx,n,1) <> "."
n = n+1
wend
n1 = n
n = n+1
while mid (xx,n,1) <> "."
n = n+1
wend
n2 = n
n = n+1
while mid (xx,n,1) <> "."
n = n+1
wend
n3 = n
n4 = len(xx)
v1 = mid (xx,n0,n1-1)
v2 = mid (xx,n1+1,n2-n1-1)
v3 = mid (xx,n2+1,n3-n2-1)
v4 = mid (xx,n3+1,n4-n3)
v4 = v4+1
if v4 > 255 then
v3 = v3+1
v4 = 0
end if
if v3 > 255 then
v2 = v2+1
v3 = 0
v4 = 0
end if
if v2 > 255 then
v1 = v1+1
v2 = 0
v3 = 0
v4 = 0
end if

return = (v1 & "." & v2 & "." & v3 & "." & v4)
newIP = return
end function
'------------
'function for validating the IP address
'------------
function ValidIP(xx)
dim n,n0,n1,n2,n3,n4,v1,v2,v3,v4,s,s1,s2,s3,s4
n = 1
n0 = n
s = 1
return = "valid"
s1 = InStr(s, xx, ".", 1)
s2 = InStr(s1+1, xx, ".", 1)
s3 = InStr(s2+1, xx, ".", 1)
s4 = len(xx)+1
if s1-s < 1 then
return = "invalid"
elseif s1-s > 3 then
return = "invalid"
elseif s2-s1 < 1 then
return = "invalid"
elseif s2-s1 > 4 then
return = "invalid"
elseif s3-s2 < 1 then
return = "invalid"
elseif s3-s2 > 4 then
return = "invalid"
elseif s4-s3 < 1 then
return = "invalid"
elseif s4-s3 > 4 then
return = "invalid"
else
while mid (xx,n,1) <> "."
n = n+1
wend
n1 = n
n = n+1
while mid (xx,n,1) <> "."
n = n+1
wend
n2 = n
n = n+1
while mid (xx,n,1) <> "."
n = n+1
wend
n3 = n
n4 = len(xx)
v1 = mid (xx,n0,n1-1)
v2 = mid (xx,n1+1,n2-n1-1)
v3 = mid (xx,n2+1,n3-n2-1)
v4 = mid (xx,n3+1,n4-n3)
if v4 > 255 then
return = "invalid"
end if
if v3 > 255 then
return = "invalid"
end if
if v2 > 255 then
return = "invalid"
end if
if v1 > 255 then
return = "invalid"
end if
end if
ValidIP = return
end function
'------------
'function for validating the End IP address
'------------
function GreaterIP(aa,zz)
return = "after"
dim n,n0,n1,n2,n3,n4,v1,v2,v3,v4
dim m,m0,m1,m2,m3,m4,w1,w2,w3,w4
n = 1
n0 = n
while mid (xx,n,1) <> "."
n = n+1
wend
n1 = n
n = n+1
while mid (xx,n,1) <> "."
n = n+1
wend
n2 = n
n = n+1
while mid (xx,n,1) <> "."
n = n+1
wend
n3 = n
n4 = len(xx)
v1 = mid (xx,n0,n1-1)
v2 = mid (xx,n1+1,n2-n1-1)
v3 = mid (xx,n2+1,n3-n2-1)
v4 = mid (xx,n3+1,n4-n3)

m = 1
m0 = m
while mid (yy,m,1) <> "."
m = m+1
wend
m1 = m
m = m+1
while mid (yy,m,1) <> "."
m = m+1
wend
m2 = m
m = m+1
while mid (yy,m,1) <> "."
m = m+1
wend
m3 = m
m4 = len(yy)
w1 = mid (yy,m0,m1-1)
w2 = mid (yy,m1+1,m2-m1-1)
w3 = mid (yy,m2+1,m3-m2-1)
w4 = mid (yy,m3+1,m4-m3)

if cint(v1) > cint(w1) then


return = "before"
else
if cint(v2) > cint(w2) then
return = "before"
else
if cint(v3) > cint(w3) then
return = "before"
else
if cint(v4) > cint(w4) then
return = "before"
end if
end if
end if
end if
GreaterIP = return
end function

Vous aimerez peut-être aussi