Class: Net::Ping::External
- Defined in:
- lib/net/ping/external.rb
Overview
The Ping::External class encapsulates methods for external (system) pings.
Constant Summary
Constants inherited from Net::Ping
Instance Attribute Summary
Attributes inherited from Net::Ping
#duration, #exception, #host, #port, #timeout, #warning
Instance Method Summary collapse
-
#ping(host = @host, count = 1, interval = 1, timeout = @timeout) ⇒ Object
(also: #ping?, #pingecho)
Pings the host using your system’s ping utility and checks for any errors or warnings.
- #ping6(host = @host, count = 1, interval = 1, timeout = @timeout) ⇒ Object
Methods inherited from Net::Ping
Constructor Details
This class inherits a constructor from Net::Ping
Instance Method Details
#ping(host = @host, count = 1, interval = 1, timeout = @timeout) ⇒ Object Also known as: ping?, pingecho
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.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 |
# 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/i pcmd += ['-c', count.to_s, '-W', timeout.to_s, host, '-i', interval.to_s] when /aix/i pcmd += ['-c', count.to_s, '-w', timeout.to_s, host] when /bsd|osx|mach|darwin/i pcmd += ['-c', count.to_s, '-t', timeout.to_s, host] when /solaris|sunos/i pcmd += [host, timeout.to_s] when /hpux/i pcmd += [host, "-n#{count.to_s}", '-m', timeout.to_s] when /win32|windows|msdos|mswin|cygwin|mingw/i 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 @end_time = Time.now err = stderr.gets # Can't chomp yet, might be nil case thread.value.exitstatus when 0 info = stdout.read if info =~ /unreachable/ix # Windows bool = false @exception = "host unreachable" else bool = true # Success, at least one response. end if err & err =~ /warning/i @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)/i @exception = line.chomp break end end end end end rescue Exception => error @exception = error. end # There is no duration if the ping failed @duration = @end_time - start_time if bool bool end |
#ping6(host = @host, count = 1, interval = 1, timeout = @timeout) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 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 |
# File 'lib/net/ping/external.rb', line 99 def ping6(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 = ['ping6'] bool = false case RbConfig::CONFIG['host_os'] when /linux/i pcmd += ['-c', count.to_s, '-W', timeout.to_s, host, '-i', interval.to_s] when /aix/i pcmd += ['-c', count.to_s, '-w', timeout.to_s, host] when /bsd|osx|mach|darwin/i pcmd += ['-c', count.to_s, '-t', timeout.to_s, host] when /solaris|sunos/i pcmd += [host, timeout.to_s] when /hpux/i pcmd += [host, "-n#{count.to_s}", '-m', timeout.to_s] when /win32|windows|msdos|mswin|cygwin|mingw/i 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 @end_time = Time.now err = stderr.gets # Can't chomp yet, might be nil case thread.value.exitstatus when 0 info = stdout.read if info =~ /unreachable/ix # Windows bool = false @exception = "host unreachable" else bool = true # Success, at least one response. end if err & err =~ /warning/i @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)/i @exception = line.chomp break end end end end end rescue Exception => error @exception = error. end # There is no duration if the ping failed @duration = @end_time - start_time if bool bool end |