viewpoint-particle

Author Topic: Ping Status Script  (Read 3084 times)

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Ping Status Script
« on: October 03, 2013, 08:58:34 am »
Ping Status script to get values returned by the standard ping command.
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt%2Ftsleft.web+WinBatch%2FWMI+Ping%7EStatus.txt

Code: Winbatch
;***************************************************************************
;**       Ping Status
;**
;**
;** Purpose: Get values returned by the standard ping command. More information about ping can be found in RFC 791.
;** Inputs: Value of the address requested. The form of the value can be either the computer name ("wxyz1234"), IPv4 address ("192.168.177.124"), or IPv6 address ("2010:836B:4179::836B:4179").
;** Outputs: HTML table containing results.
;** System Requirements:
;**    Relies on WMI
;**    Starting with Windows Vista, Win32_PingStatus can return data for computers that have both IPv4 addresses and IPv6 addresses.
;**    Windows Server 2003 and Windows XP:  Win32_PingStatus only returns data for computers running IPv4. For more information, see IPv6 and IPv4 Support in WMI.
;**
;** Revisions: 2013.10.2 Deana Falk
;***************************************************************************
If  WinVersion(5)<="2-5-1"
   Pause('Notice', 'Need XP or Newer Version of Windows')
   Exit
EndIf

#DefineFunction udfArrayToList(arrData,strDelim)
   strData = ''
   count = ArrInfo ( arrData, 1 ) -1
   For ctr = 0 To count
      If ctr < count
         strData=StrCat(strData,arrData[ctr],strDelim)
      Else
         strData=StrCat(strData,arrData[ctr])
      EndIf
   Next
   Return (strData)
#EndFunction

#DefineFunction GetSourceRouteType(intSourceRouteType)
  Select intSourceRouteType
     Case 1
       strType = "Loose Source Routing"
       Break
     Case 2
       strType = "Strict Source Routing"
       Break
     Case intSourceRouteType
       ; Default - 0 - or any other value.
       strType = intSourceRouteType : " - None"
       Break
  EndSelect
  Return (strType)
#EndFunction

#DefineFunction GetTypeOfService(intServiceType)
  Select intServiceType
     Case 2
       strType = "Minimize Monetary Cost"
       Break
     Case 4
       strType = "Maximize Reliability"
       Break
     Case 8
       strType = "Maximize Throughput"
       Break
     Case 16
       strType = "Minimize Delay"
       Break
     Case intServiceType
       ; Default - 0 - or any other value.
       strType = intServiceType : " - Normal"
       Break
  EndSelect
  Return (strType)
#EndFunction

#DefineFunction GetStatusCode (intCode)
  Select  intCode
  Case  0
    strStatus = "Success"
    Break
  Case  11001
    strStatus = "Buffer Too Small"
    Break
  Case  11002
    strStatus = "Destination Net Unreachable"
    Break
  Case  11003
    strStatus = "Destination Host Unreachable"
    Break
  Case  11004
    strStatus = "Destination Protocol Unreachable"
    Break
  Case  11005
    strStatus = "Destination Port Unreachable"
    Break
  Case  11006
    strStatus = "No Resources"
    Break
  Case  11007
    strStatus = "Bad Option"
    Break
  Case  11008
    strStatus = "Hardware Error"
    Break
  Case  11009
    strStatus = "Packet Too Big"
    Break
  Case  11010
    strStatus = "Request Timed Out"
    Break
  Case  11011
    strStatus = "Bad Request"
    Break
  Case  11012
    strStatus = "Bad Route"
    Break
  Case  11013
    strStatus = "TimeToLive Expired Transit"
    Break
  Case  11014
    strStatus = "TimeToLive Expired Reassembly"
    Break
  Case  11015
    strStatus = "Parameter Problem"
    Break
  Case  11016
    strStatus = "Source Quench"
    Break
  Case  11017
    strStatus = "Option Too Big"
    Break
  Case  11018
    strStatus = "Bad Destination"
    Break
  Case  11032
    strStatus = "Negotiating IPSEC"
    Break
  Case  11050
    strStatus = "General Failure"\break
  Case intCode
    strStatus = intCode : " - Unknown"
  EndSelect
  Return(strStatus)
#EndFunction

#DefineFunction udfConvertToHTMLTableEx( arrData )
   ; Beautified version of the table
   rowcount = ArrInfo( arrData, 1 )
   colcount = ArrInfo( arrData, 2 )
   htmlData = '<table width="auto" border="1" cellpadding="3" cellspacing="0">'
   ;Pause( rowcount, colcount)
   For row = 0 To ArrInfo( arrData, 1 ) - 1
       ; Alternate Row Color
       If row == 0 ;Heading only
          htmlData = htmlData : '<tr bgcolor="CornflowerBlue">' ; #6495ED
       ElseIf (row mod 2) == 0      ;even numbered row
          htmlData = htmlData : '<tr bgcolor="LightGray">' ;   #D3D3D3
       Else
          htmlData = htmlData : '<tr>'
       EndIf
       For col = 0 To ArrInfo( arrData, 2 ) - 1
                    htmlData = htmlData : '<td align="left">'
          If ObjectTypeGet( arrData[row,col] ) == 'ARRAY|VARIANT'
              data = udfArrayToList(arrData[row,col],'|')
          Else
              data = arrData[row,col]
          EndIf
          htmlData = htmlData : data
          htmlData = htmlData : '</td>'
       Next
       htmlData = htmlData : '</tr>'
   Next
   htmlData = htmlData : '</table>'
   Return htmlData
#EndFunction

outfile = DirScript():'PingStatus.htm'

title = 'Ping Status'
strAddress = AskLine ('Ping Status','Specify an address to ping. Enter computer name ("wxyz1234"), IPv4 address ("192.168.177.124"), or IPv6 address ("2010:836B:4179::836B:4179").', 'google.com')
query = "Win32_PingStatus.Address='" : strAddress : "'"
objWMI = GetObject("winmgmts:")
objPing = objWMI.Get(query)

propertylist = 'Address,BufferSize,NoFragmentation,PrimaryAddressResolutionStatus,ProtocolAddress,ProtocolAddressResolved,RecordRoute,ReplyInconsistency,ReplySize,ResolveAddressNames,ResponseTime,ResponseTimeToLive,RouteRecord,RouteRecordResolved,SourceRoute,SourceRouteType,StatusCode,Timeout,TimeStampRecord,TimeStampRecordAddress,TimeStampRecordAddressResolved,TimeStampRoute,TimeToLive,TypeofService'
count = ItemCount( propertylist, ',' )
arrData = ArrDimension( 2, count )
For i = 0 To count-1
    prop = ItemExtract( i+1, propertylist, ',' )
    arrData[0,i] = prop
    If prop == "SourceRouteType"
       GetSourceRouteType(objPing.SourceRouteType)
    ElseIf prop == "StatusCode"
       GetStatusCode(objPing.StatusCode)
    Else
       value = objPing.%prop%
       If value == "" Then value = "NA"
       If ObjectTypeGet (value) == "ARRAY|VARIANT" Then value = udfArrayToList(value,";")
    EndIf
    arrData[1,i] = value
Next

; HTML Output
fh = FileOpen( outfile, "Write")
FileWrite( fh, '<!DOCTYPE html><html><head><title>Ping Status</title><head><body><h1>':strAddress:'</h1>' )
FileWrite( fh,  udfConvertToHTMLTableEx( arrData ) )
FileWrite( fh, '<p>For more information about these values please see: <a target="_blank" href="http://msdn.microsoft.com/en-us/library/aa394350(v=vs.85).aspx">http://msdn.microsoft.com/en-us/library/aa394350(v=vs.85).aspx</a></p>' )
FileWrite( fh, '</body></html>' )
FileClose( fh )

ret = AskYesNo( title, 'Would you like to display the results?' )
If ret
   ; Display Results
   Run( outfile, '' )
EndIf
Exit
Deana F.
Technical Support
Wilson WindowWare Inc.