Class: Net::Ping::External

### Home Classes Methods

### In Files

- lib/net/ping/external.rb

### Parent

Net::Ping

### Methods

- #ping
- #ping?
- #pingecho

### Files

- ping.txt

### Class/Module Index

Quicksearch

- Net
- Net::Ping
- Net::Ping::External
- Net::Ping::HTTP
- Net::Ping::ICMP
- Net::Ping::TCP
- Net::Ping::UDP
- Net::Ping::WMI
- Object

No matching classes.

# Net::Ping::External

The Ping::External class encapsulates methods for external (system) pings.

### Public Instance Methods

ping(host = @host, count = 1, interval = 1, timeout = @timeout) click to toggle source

Pings the host using your system's ping utility and checks for any errors or warnings. Returns true if successful, or false if not.

If the ping failed then the Ping::External#exception method should contain a string indicating what went wrong. If the ping succeeded then the Ping::External#warning method may or may not contain a value.

    # File lib/net/ping/external.rb, line 18
    def ping(host = @host, count = 1, interval = 1, timeout = @timeout)

      raise "Count must be an integer" unless count.is_a? Integer
      raise "Timeout must be a number" unless timeout.is_a? Numeric

      unless interval.is_a?(Numeric) && interval >= 0.2
        raise "Interval must be a decimal greater than or equal to 0.2"
      end

      super(host)

      pcmd = ['ping']
      bool = false

      case RbConfig::CONFIG['host_os']
        when /linux/
          pcmd += ['-c', count.to_s, '-W', timeout.to_s, host, '-i', interval.to_s]
        when /aix/
          pcmd += ['-c', count.to_s, '-w', timeout.to_s, host]
        when /bsd|osx|mach|darwin/
          pcmd += ['-c', count.to_s, '-t', timeout.to_s, host]
        when /solaris|sunos/
          pcmd += [host, timeout.to_s]
        when /hpux/
          pcmd += [host, "-n#{count.to_s}", '-m', timeout.to_s]
        when /win32|windows|msdos|mswin|cygwin|mingw/
          pcmd += ['-n', count.to_s, '-w', (timeout * 1000).to_s, host]
        else
          pcmd += [host]
      end

      start_time = Time.now

      begin
        err = nil

        Open3.popen3(*pcmd) do |stdin, stdout, stderr, thread|
          stdin.close
          err = stderr.gets # Can't chomp yet, might be nil

          case thread.value.exitstatus
            when 0
              info = stdout.read
              if info =~ /unreachable/x # Windows
                bool = false
                @exception = "host unreachable"
              else
                bool = true  # Success, at least one response.
              end

              if err & err =~ /warning/
                @warning = err.chomp
              end
            when 2
              bool = false # Transmission successful, no response.
              @exception = err.chomp if err
            else
              bool = false # An error occurred
              if err
                @exception = err.chomp
              else
                stdout.each_line do |line|
                  if line =~ /(timed out|could not find host|packet loss)/
                    @exception = line.chomp
                    break
                  end
                end
              end
          end
        end
      rescue Exception => error
        @exception = error.message
      end

      # There is no duration if the ping failed
      @duration = Time.now - start_time if bool

      bool
    end

Also aliased as: ping?, pingecho
ping?(host = @host, count = 1, interval = 1, timeout = @timeout) click to toggle source
Alias for: ping
pingecho(host = @host, count = 1, interval = 1, timeout = @timeout) click to toggle source
Alias for: ping

[Validate]

Generated with the Darkfish Rdoc Generator 2.