Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A Script to Ping a Range of IP Addresses 4

Status
Not open for further replies.

nullig

Technical User
Jan 20, 2004
10
0
0
CA
Hope somebody finds this to be useful...


'º 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 = "c:\Temp\MultiPing\IPtemp.txt"
notfound = "No Response"

' Spreadsheet file to be created.
strExcelPath = "C:\Temp\MultiPing\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
 
Hello!

this script was most helpfull to me
i only have one question. I'm also trying to retrieve te Ping respond times of the ip.
I'm actually writing a monitoringstool to retrieve the service level of our routers. instead of saving to an xls, i write it in a SQL table.
 
You could play with the following section to parse for the times and save that info as well....
----------------------------------------------
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
----------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top