Class: Ping::WMI

Inherits:
Ping
  • Object
show all
Defined in:
lib/net/ping/wmi.rb

Overview

The Ping::WMI class encapsulates the Win32_PingStatus WMI class for MS Windows.

Defined Under Namespace

Classes: PingStatus

Instance Method Summary collapse

Instance Method Details

#ping(host = @host, options = {}) ⇒ Object

Unlike the ping method for other Ping subclasses, this version returns a PingStatus struct which contains various bits of information about the results of the ping itself, such as response time.

In addition, this version allows you to pass certain options that are then passed on to the underlying WQL query. See the MSDN documentation on Win32_PingStatus for details.

Examples:

Ping with no options

Ping::WMI.ping('www.perl.com')

Ping with options

Ping::WMI.ping('www.perl.com', :BufferSize => 64, :NoFragmentation => true)

The PingStatus struct is a wrapper for the Win32_PingStatus WMI class.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/net/ping/wmi.rb', line 59

def ping(host = @host, options = {})
   super(host)

   lhost = Socket.gethostname

   cs = "winmgmts:{impersonationLevel=impersonate}!//#{lhost}/root/cimv2"
   wmi = WIN32OLE.connect(cs)

   query = "select * from win32_pingstatus where address = '#{host}'"

   unless options.empty?
      options.each{ |key, value|
         if value.is_a?(String)
            query << " and #{key} = '#{value}'"
         else
            query << " and #{key} = #{value}"
         end
      }
   end

   status = Struct::PingStatus.new

   wmi.execquery(query).each{ |obj|
      status.address                           = obj.Address
      status.buffer_size                       = obj.BufferSize
      status.no_fragmentation                  = obj.NoFragmentation
      status.primary_address_resolution_status = obj.PrimaryAddressResolutionStatus
      status.protocol_address                  = obj.ProtocolAddress
      status.protocol_address_resolved         = obj.ProtocolAddressResolved
      status.record_route                      = obj.RecordRoute 
      status.reply_inconsistency               = obj.ReplyInconsistency
      status.reply_size                        = obj.ReplySize
      status.resolve_address_names             = obj.ResolveAddressNames
      status.response_time                     = obj.ResponseTime
      status.response_time_to_live             = obj.ResponseTimeToLive
      status.route_record                      = obj.RouteRecord
      status.route_record_resolved             = obj.RouteRecordResolved
      status.source_route                      = obj.SourceRoute
      status.source_route_type                 = obj.SourceRouteType
      status.status_code                       = obj.StatusCode
      status.timeout                           = obj.Timeout
      status.timestamp_record                  = obj.TimeStampRecord
      status.timestamp_record_address          = obj.TimeStampRecordAddress
      status.timestamp_record_address_resolved = obj.TimeStampRecordAddressResolved
      status.timestamp_route                   = obj.TimeStampRoute
      status.time_to_live                      = obj.TimeToLive
      status.type_of_service                   = obj.TypeOfService
   }

   status.freeze # Read-only data
end

#ping?(host = @host, options = {}) ⇒ Boolean

Unlike Net::Ping::WMI#ping, this method returns true or false to indicate whether or not the ping was successful.

Returns:

  • (Boolean)


114
115
116
# File 'lib/net/ping/wmi.rb', line 114

def ping?(host = @host, options = {})
   ping(host, options).status_code == 0
end