PING enable your APPS and SCRIPTS

usetheforce2
06-16-2003, 09:18 PM
hello all,

For all you scripters out there, heres a activex dll you can use to enable you scripts to easilly perform single, multiple, and subnet pinging. download the following ActiveX dll project and compile it.

NOTE: if for some reason you haven't the ability to compile the project, download the "already-compiled" .dll and register it using the regsvr32 utility.
regsvr32 <dllpath>\Netw.dll
dllpath being the path to the location of the dll

Let me describe some of the key functions and methods provided by the activex dll that are important to know of:

First and most importantly is the Ping method which returns True for a successful ping and False for a failed ping attempt. This method only requires one parameter - a dotted decimal ip address. This is all that is required to ping a remote host. However, it would be nice to know some more tidbits of info, such as the time it took, and the status of the ping. So, you can use the RoundTripTime and PingStatusMsg properties to establish further information of the ping. The following basic script will ping a remote host, and depending on the success or failiure of the ping attempt, display some info.
Sample VBScript (.vbs)
' First instatiate an ICMP_Ping object
Set ipPing = CreateObject("NetW.ICMP_Ping")

' host to ping
IPAddress = "24.100.3.40"

' how many times to ping each host
numTimes = 2

' of times to ping each host
For y = 1 To numTimes

' if ping is successful then display some simple properties
If ipPing.Ping(cstr(IPAddress)) Then
PingStatus = PingStatus & _
IPAddress & " " & _
ipPing.RoundTripTime & "ms" & vbNewLine
Else
' if fail, display the message
PingStatus = PingStatus & _
IPAddress & " " & _
ipPing.PingStatusMsg & vbNewLine
End If
wscript.echo PingStatus
Next

run this script by either double clicking or command line
cscript <scriptpath>\simpleping.vbs

usetheforce2
06-16-2003, 09:39 PM
Of course, If we want sweep a subnet or ip range, then we'll have to turn our attention to couple very important functions. The IPToLong and LongToIP functions are what provide us with the magic to ping ip ranges.

IPToLong --> converts a dotted decimal ip address to a long representation of the ip address.
LongToIP --> converts the long value back to a dotted ip address

the basis of both functions works by considering the number or string as a base 256 number. Just as our basic base 10 number system refers to each column to the left of the decimal point represents a multiple of ten. Using a base 256 isnt any different.
Here's a quick example of how we can ping an ip range.
Sample VBScript (.vbs)
Dim ipPing
Dim numTimes
Dim PingStatus
Dim StartIP
Dim EndIP


' establish our ip range
StartIP = "24.100.3.1"
EndIP = "24.100.4.1"

' First instatiate an ICMP_Ping object
Set ipPing = CreateObject("NetW.ICMP_Ping")

' how many times to ping each host
numTimes = 1

' set the timeout to something smaller
' it defaults to 2 seconds otherwise
' just speeds things up, but we do loose accuracy
ipPing.TimeOut = 100

' IPToLong Will convert a dotted decimal ip address into a long representation

For x = ipPing.IPToLong(cstr(StartIP)) To ipPing.IPToLong(cstr(EndIP))

' of times to ping each host
For y = 1 To numTimes

' if ping is successful then display some simple properties
If ipPing.Ping(ipPing.LongToIP(x)) Then
PingStatus = PingStatus & _
ipPing.LongToIP(x) & " " & _
ipPing.RoundTripTime & "ms" & vbNewLine
Else
' if fail, display the message
PingStatus = PingStatus & _
ipPing.LongToIP(x) & " " & _
ipPing.PingStatusMsg & vbNewLine
End If

Next
wscript.echo PingStatus & "-----------" & vbNewLine
PingStatus = ""
Next

of course, i've also put a script together that accepts command line arguments that enables further flexiblity when running this script. download the samlple script PingSweepArgs.vbs and try it out.

usetheforce2
06-16-2003, 10:06 PM
yet another example of a ping sweep, but this time we use an excel file to provide the IP list Dim objWorkBook
Dim objExcelApp
Dim objWorkSheet
Dim objPing
Dim WorkSheetName

' should set these up as command-line arguments
WorkSheetName = "IPADDRESSES"
ColumnNumber = 1
FilePath = "c:\IP.xls" '<--- MAKE SURE TO CHANGE THIS

' get a reference to a netw ping object
Set objPing = CreateObject("NetW.ICMP_Ping")

' get the workbook
Set objExcelApp = CreateObject("Excel.Application")
' specify the path to the excel file
Set objWorkBook = objExcelApp.Workbooks.Open(FilePath)

' itterate the worksheets present
For x = 1 To objWorkBook.Sheets.Count
' locate the desired worksheet
If Trim(UCase(objWorkBook.Sheets(x).Name)) = Trim(UCase(WorkSheetName)) Then
' obtain a reference to the desired worksheet object
Set objWorkSheet = objWorkBook.Sheets(x)
End If
Next

objPing.TimeOut = 100

For x = 1 To objWorkSheet.UsedRange.Rows.Count
Set objRange = objWorkSheet.Cells.Item(x, ColumnNumber)
' now we can ping our host
If objPing.Ping(objRange.Value) Then
wscript.echo objRange.Value & " " & objPing.PingStatusMsg & " TIME: " & objPing.RoundTripTime & "ms"
Else
wscript.echo objRange.Value & " " & objPing.PingStatusMsg
End If
Next

' dont forget to close the workbook
objWorkBook.Close

'[clean up]

Set objWorkSheet = Nothing
Set objExcelApp = Nothing
Set objWorkBook = Nothing
of course, this would be much more handy if we could pass is the first fields in from a command line:
' should set these up as command-line arguments
WorkSheetName = "IPADDRESSES"
ColumnNumber = 1
FilePath = "c:\IP.xls" '<--- MAKE SURE TO CHANGE THIS
but i'll leave that up to you.

use the following example file for a temp ping list to try.

usetheforce2
06-29-2003, 10:28 PM
update

usetheforce2
03-09-2004, 06:37 PM
sorry, the previous version had an error in the ip conversion routine.

i was using a long datatype that could no support class A ip address over 48.0.0.0. however, this issue has been corrected and i apologize for any inconvenience this may have caused...

refer to these functions for error corrections...

Public Function IPToLong(strIp As String) As Double
...........
End Function
Public Function LongToIP(ByVal lngNum As Double) As String
..........
End Function


once again enjoy
Regan

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum