Class: Net::PingExternal

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

Overview

Use your system’s ping command

Constant Summary

Constants inherited from Ping

Net::Ping::VERSION

Instance Attribute Summary

Attributes inherited from Ping

#exception, #host, #port, #timeout, #warning

Instance Method Summary collapse

Methods inherited from Ping

#initialize

Constructor Details

This class inherits a constructor from Net::Ping

Instance Method Details

#pingObject Also known as: ping?

Pings the host using your system’s ping utility and checks for any errors or warnings.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/net/ping.rb', line 133

def ping
   super
   input, output, error = ""
   pstring = "ping "
   success = false
   
   case PLATFORM
      when /linux|bsd/i
         pstring += "-c 1 #{@host}"
      when /solaris|sunos/i
         pstring += "#{@host} 1"
      when /hpux/i
         pstring += "#{@host} -n 1"
      when /win32|windows/i
         pstring += "-n 1 #{@host}"
      else
         pstring += "#{@host}"
   end
   
   if File::ALT_SEPARATOR
      require "win32/open3"           
   else
      require "open3"            
   end
   
   Timeout.timeout(@timeout){
      input, output, error = Open3.popen3(pstring)
   }

   e = error.gets # Can't chomp yet, might be nil

   input.close
   error.close
  
   unless e.nil?
      if e =~ /warning/i
         @warning = e.chomp
         success = true
      else
         @exception = e.chomp
      end
   # The "no answer" response goes to stdout, not stderr, so check it
   else
      lines = output.readlines
      output.close
      if lines.nil? || lines.empty?
         success = true
      else
         regexp = /no answer|host unreachable|could not find host/i
         lines.each{ |e|
            if regexp.match(e)
               @exception = e.chomp
            end
         }
         success = true
      end
   end
   success
end