modified: c/main.rb
deleted: lib/gtk2.rb deleted: lib/gtk2/base.rb deleted: lib/net/external.rb deleted: lib/net/helper.rb deleted: lib/net/http.rb deleted: lib/net/icmp.rb deleted: lib/net/ping.rb deleted: lib/net/tcp.rb deleted: lib/net/udp.rb deleted: lib/net/wmi.rb
This commit is contained in:
parent
23faf3d9b6
commit
c656d7a918
@ -1,8 +1,8 @@
|
||||
#! /usr/bin/ruby
|
||||
|
||||
#lib
|
||||
require '../lib/gtk2' # lib graphique
|
||||
require '../lib/net/ping' #lib netwotk
|
||||
require 'gtk2' # lib graphique
|
||||
require 'net-ping' #lib netwotk
|
||||
|
||||
#include
|
||||
require '../m/ReadFile'
|
||||
|
13
lib/gtk2.rb
13
lib/gtk2.rb
@ -1,13 +0,0 @@
|
||||
#! /usr/bin/env ruby
|
||||
=begin
|
||||
gtk2.rb
|
||||
|
||||
Copyright (c) 2006 Ruby-GNOME2 Project Team
|
||||
This program is licenced under the same licence as Ruby-GNOME2.
|
||||
|
||||
$Id: gtk2.rb,v 1.9 2006/06/17 13:18:12 mutoh Exp $
|
||||
=end
|
||||
|
||||
require 'gtk2/base'
|
||||
|
||||
Gtk.init
|
105
lib/gtk2/base.rb
105
lib/gtk2/base.rb
@ -1,105 +0,0 @@
|
||||
#! /usr/bin/env ruby
|
||||
=begin
|
||||
gtk2/base.rb
|
||||
|
||||
Copyright (c) 2006 Ruby-GNOME2 Project Team
|
||||
This program is licenced under the same licence as Ruby-GNOME2.
|
||||
|
||||
$Id: base.rb,v 1.6 2007/08/13 11:09:22 ktou Exp $
|
||||
=end
|
||||
|
||||
|
||||
require 'glib2'
|
||||
require 'atk'
|
||||
require 'pango'
|
||||
require 'gdk_pixbuf2'
|
||||
|
||||
base_dir = Pathname.new(__FILE__).dirname.dirname.dirname.expand_path
|
||||
vendor_dir = base_dir + "vendor" + "local"
|
||||
vendor_bin_dir = vendor_dir + "bin"
|
||||
GLib.prepend_dll_path(vendor_bin_dir)
|
||||
begin
|
||||
major, minor, _ = RUBY_VERSION.split(/\./)
|
||||
require "#{major}.#{minor}/gtk2.so"
|
||||
rescue LoadError
|
||||
require "gtk2.so"
|
||||
end
|
||||
|
||||
module Gdk
|
||||
LOG_DOMAIN = "Gdk"
|
||||
end
|
||||
|
||||
if Gdk.cairo_available?
|
||||
module Cairo
|
||||
class Context
|
||||
if method_defined?(:set_source_color)
|
||||
alias_method :set_source_not_gdk_color, :set_source_color
|
||||
def set_source_color(color)
|
||||
if color.is_a?(Gdk::Color)
|
||||
set_source_gdk_color(color)
|
||||
else
|
||||
set_source_not_gdk_color(color)
|
||||
end
|
||||
end
|
||||
else
|
||||
alias_method :set_source_color, :set_source_gdk_color
|
||||
end
|
||||
|
||||
def source_color=(color)
|
||||
set_source_color(color)
|
||||
color
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
module Gtk
|
||||
LOG_DOMAIN = "Gtk"
|
||||
|
||||
class Printer
|
||||
def self.printers(wait = false)
|
||||
printers = []
|
||||
self.each(wait) {|v| printers << v}
|
||||
printers
|
||||
end
|
||||
end
|
||||
|
||||
if check_version?(2, 12, 0)
|
||||
class Builder
|
||||
private
|
||||
def canonical_handler_name(name)
|
||||
name.gsub(/[-\s]+/, "_")
|
||||
end
|
||||
|
||||
def __connect_signals__(connector, object, signal_name,
|
||||
handler_name, connect_object, flags)
|
||||
handler_name = canonical_handler_name(handler_name)
|
||||
if connect_object
|
||||
handler = connect_object.method(handler_name)
|
||||
else
|
||||
handler = connector.call(handler_name)
|
||||
end
|
||||
unless handler
|
||||
$stderr.puts("Undefined handler: #{handler_name}") if $DEBUG
|
||||
return
|
||||
end
|
||||
|
||||
if flags.after?
|
||||
signal_connect_method = :signal_connect_after
|
||||
else
|
||||
signal_connect_method = :signal_connect
|
||||
end
|
||||
|
||||
if handler.arity.zero?
|
||||
object.send(signal_connect_method, signal_name) {handler.call}
|
||||
else
|
||||
object.send(signal_connect_method, signal_name, &handler)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
GLib::Log.set_log_domain(Gdk::LOG_DOMAIN)
|
||||
GLib::Log.set_log_domain(Gtk::LOG_DOMAIN)
|
@ -1,117 +0,0 @@
|
||||
require 'ffi'
|
||||
require 'open3'
|
||||
require 'rbconfig'
|
||||
|
||||
require File.join(File.dirname(__FILE__), 'ping')
|
||||
|
||||
# The Net module serves as a namespace only.
|
||||
module Net
|
||||
|
||||
# The Ping::External class encapsulates methods for external (system) pings.
|
||||
class Ping::External < Ping
|
||||
|
||||
if File::ALT_SEPARATOR
|
||||
extend FFI::Library
|
||||
ffi_lib 'kernel32'
|
||||
|
||||
attach_function :SetConsoleCP, [:uint], :bool
|
||||
attach_function :GetConsoleCP, [], :uint
|
||||
end
|
||||
|
||||
# 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.
|
||||
#
|
||||
def ping(host = @host)
|
||||
super(host)
|
||||
|
||||
stdin = stdout = stderr = nil
|
||||
pcmd = ["ping"]
|
||||
bool = false
|
||||
orig_cp = nil
|
||||
|
||||
case RbConfig::CONFIG['host_os']
|
||||
when /linux|bsd|osx|mach|darwin/i
|
||||
pcmd += ['-c1', host]
|
||||
when /solaris|sunos/i
|
||||
pcmd += [host, '1']
|
||||
when /hpux/i
|
||||
pcmd += [host, '-n1']
|
||||
when /win32|windows|msdos|mswin|cygwin|mingw/i
|
||||
orig_cp = GetConsoleCP()
|
||||
SetConsoleCP(437) if orig_cp != 437 # United States
|
||||
pcmd += ['-n', '1', host]
|
||||
else
|
||||
pcmd += [host]
|
||||
end
|
||||
|
||||
start_time = Time.now
|
||||
|
||||
begin
|
||||
err = nil
|
||||
|
||||
Timeout.timeout(@timeout){
|
||||
stdin, stdout, stderr = Open3.popen3(*pcmd)
|
||||
err = stderr.gets # Can't chomp yet, might be nil
|
||||
}
|
||||
|
||||
stdin.close
|
||||
stderr.close
|
||||
|
||||
if File::ALT_SEPARATOR && GetConsoleCP() != orig_cp
|
||||
SetConsoleCP(orig_cp)
|
||||
end
|
||||
|
||||
unless err.nil?
|
||||
if err =~ /warning/i
|
||||
@warning = err.chomp
|
||||
bool = true
|
||||
else
|
||||
@exception = err.chomp
|
||||
end
|
||||
# The "no answer" response goes to stdout, not stderr, so check it
|
||||
else
|
||||
lines = stdout.readlines
|
||||
stdout.close
|
||||
if lines.nil? || lines.empty?
|
||||
bool = true
|
||||
else
|
||||
regexp = /
|
||||
no\ answer|
|
||||
host\ unreachable|
|
||||
could\ not\ find\ host|
|
||||
request\ timed\ out|
|
||||
100%\ packet\ loss
|
||||
/ix
|
||||
|
||||
lines.each{ |line|
|
||||
if regexp.match(line)
|
||||
@exception = line.chomp
|
||||
break
|
||||
end
|
||||
}
|
||||
|
||||
bool = true unless @exception
|
||||
end
|
||||
end
|
||||
rescue Exception => error
|
||||
@exception = error.message
|
||||
ensure
|
||||
stdin.close if stdin && !stdin.closed?
|
||||
stdout.close if stdout && !stdout.closed?
|
||||
stderr.close if stderr && !stderr.closed?
|
||||
end
|
||||
|
||||
# There is no duration if the ping failed
|
||||
@duration = Time.now - start_time if bool
|
||||
|
||||
bool
|
||||
end
|
||||
|
||||
alias ping? ping
|
||||
alias pingecho ping
|
||||
end
|
||||
end
|
@ -1,33 +0,0 @@
|
||||
require 'ffi'
|
||||
|
||||
module Windows
|
||||
extend FFI::Library
|
||||
ffi_lib :kernel32
|
||||
|
||||
attach_function :GetVersion, [], :ulong
|
||||
|
||||
def version
|
||||
version = GetVersion()
|
||||
major = LOBYTE(LOWORD(version))
|
||||
minor = HIBYTE(LOWORD(version))
|
||||
eval("Float(#{major}.#{minor})")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
class << self
|
||||
def LOWORD(l)
|
||||
l & 0xffff
|
||||
end
|
||||
|
||||
def LOBYTE(w)
|
||||
w & 0xff
|
||||
end
|
||||
|
||||
def HIBYTE(w)
|
||||
w >> 8
|
||||
end
|
||||
end
|
||||
|
||||
module_function :version
|
||||
end
|
163
lib/net/http.rb
163
lib/net/http.rb
@ -1,163 +0,0 @@
|
||||
require File.join(File.dirname(__FILE__), 'ping')
|
||||
require 'net/http'
|
||||
require 'net/https'
|
||||
require 'uri'
|
||||
require 'open-uri'
|
||||
|
||||
# Force non-blocking Socket.getaddrinfo on Unix systems. Do not use on
|
||||
# Windows because it (ironically) causes blocking problems.
|
||||
unless File::ALT_SEPARATOR or RUBY_VERSION >= "1.9.3"
|
||||
require 'resolv-replace'
|
||||
end
|
||||
|
||||
# The Net module serves as a namespace only.
|
||||
module Net
|
||||
|
||||
# The Ping::HTTP class encapsulates methods for HTTP pings.
|
||||
class Ping::HTTP < Ping
|
||||
|
||||
# By default an http ping will follow a redirect and give you the result
|
||||
# of the final URI. If this value is set to false, then it will not
|
||||
# follow a redirect and will return false immediately on a redirect.
|
||||
#
|
||||
attr_accessor :follow_redirect
|
||||
|
||||
# The maximum number of redirects allowed. The default is 5.
|
||||
attr_accessor :redirect_limit
|
||||
|
||||
# The user agent used for the HTTP request. The default is nil.
|
||||
attr_accessor :user_agent
|
||||
|
||||
# OpenSSL certificate verification mode. The default is VERIFY_NONE.
|
||||
attr_accessor :ssl_verify_mode
|
||||
|
||||
# Use GET request instead HEAD. The default is false.
|
||||
attr_accessor :get_request
|
||||
|
||||
# was this ping proxied?
|
||||
attr_accessor :proxied
|
||||
|
||||
# Creates and returns a new Ping::HTTP object. The default port is the
|
||||
# port associated with the URI. The default timeout is 5 seconds.
|
||||
#
|
||||
def initialize(uri=nil, port=nil, timeout=5)
|
||||
@follow_redirect = true
|
||||
@redirect_limit = 5
|
||||
@ssl_verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||
@get_request = false
|
||||
|
||||
port ||= URI.parse(uri).port if uri
|
||||
|
||||
super(uri, port, timeout)
|
||||
end
|
||||
|
||||
# Looks for an HTTP response from the URI passed to the constructor.
|
||||
# If the result is a kind of Net::HTTPSuccess then the ping was
|
||||
# successful and true is returned. Otherwise, false is returned
|
||||
# and the Ping::HTTP#exception method should contain a string
|
||||
# indicating what went wrong.
|
||||
#
|
||||
# If the HTTP#follow_redirect accessor is set to true (which it is
|
||||
# by default) and a redirect occurs during the ping, then the
|
||||
# HTTP#warning attribute is set to the redirect message, but the
|
||||
# return result is still true. If it's set to false then a redirect
|
||||
# response is considered a failed ping.
|
||||
#
|
||||
# If no file or path is specified in the URI, then '/' is assumed.
|
||||
# If no scheme is present in the URI, then 'http' is assumed.
|
||||
#
|
||||
def ping(host = @host)
|
||||
super(host)
|
||||
bool = false
|
||||
|
||||
# See https://bugs.ruby-lang.org/issues/8645
|
||||
host = "http://#{host}" unless host.include?("http")
|
||||
|
||||
uri = URI.parse(host)
|
||||
|
||||
# A port provided here overrides anything provided in constructor
|
||||
port = URI.split(host)[3] || @port
|
||||
port = port.to_i
|
||||
|
||||
start_time = Time.now
|
||||
|
||||
response = do_ping(uri, port)
|
||||
|
||||
if response.is_a?(Net::HTTPSuccess)
|
||||
bool = true
|
||||
elsif redirect?(response) # Check code, HTTPRedirection does not always work
|
||||
if @follow_redirect
|
||||
@warning = response.message
|
||||
rlimit = 0
|
||||
|
||||
while redirect?(response)
|
||||
if rlimit >= redirect_limit
|
||||
@exception = "Redirect limit exceeded"
|
||||
break
|
||||
end
|
||||
redirect = URI.parse(response['location'])
|
||||
redirect = uri + redirect if redirect.relative?
|
||||
response = do_ping(redirect, port)
|
||||
rlimit += 1
|
||||
end
|
||||
|
||||
if response.is_a?(Net::HTTPSuccess)
|
||||
bool = true
|
||||
else
|
||||
@warning = nil
|
||||
@exception ||= response.message
|
||||
end
|
||||
|
||||
else
|
||||
@exception = response.message
|
||||
end
|
||||
end
|
||||
|
||||
# There is no duration if the ping failed
|
||||
@duration = Time.now - start_time if bool
|
||||
|
||||
bool
|
||||
end
|
||||
|
||||
alias ping? ping
|
||||
alias pingecho ping
|
||||
alias follow_redirect? follow_redirect
|
||||
alias uri host
|
||||
alias uri= host=
|
||||
|
||||
private
|
||||
|
||||
def redirect?(response)
|
||||
response && response.code.to_i >= 300 && response.code.to_i < 400
|
||||
end
|
||||
|
||||
def do_ping(uri, port)
|
||||
response = nil
|
||||
proxy = uri.find_proxy || URI.parse("")
|
||||
begin
|
||||
uri_path = uri.path.empty? ? '/' : uri.path
|
||||
headers = { }
|
||||
headers["User-Agent"] = user_agent unless user_agent.nil?
|
||||
Timeout.timeout(@timeout) do
|
||||
http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new(uri.host, port)
|
||||
@proxied = http.proxy?
|
||||
if @get_request == true
|
||||
request = Net::HTTP::Get.new(uri_path)
|
||||
else
|
||||
request = Net::HTTP::Head.new(uri_path)
|
||||
end
|
||||
|
||||
if uri.scheme == 'https'
|
||||
http.use_ssl = true
|
||||
http.verify_mode = @ssl_verify_mode
|
||||
end
|
||||
|
||||
response = http.start { |h| h.request(request) }
|
||||
end
|
||||
rescue Exception => err
|
||||
@exception = err.message
|
||||
end
|
||||
response
|
||||
end
|
||||
end
|
||||
end
|
179
lib/net/icmp.rb
179
lib/net/icmp.rb
@ -1,179 +0,0 @@
|
||||
require File.join(File.dirname(__FILE__), 'ping')
|
||||
|
||||
if File::ALT_SEPARATOR
|
||||
require 'win32/security'
|
||||
require File.join(File.dirname(__FILE__), 'helper')
|
||||
end
|
||||
|
||||
# The Net module serves as a namespace only.
|
||||
module Net
|
||||
|
||||
# The Net::Ping::ICMP class encapsulates an icmp ping.
|
||||
class Ping::ICMP < Ping
|
||||
ICMP_ECHOREPLY = 0 # Echo reply
|
||||
ICMP_ECHO = 8 # Echo request
|
||||
ICMP_SUBCODE = 0
|
||||
|
||||
# You cannot set or change the port value. A value of 0 is always
|
||||
# used internally for ICMP pings.
|
||||
#
|
||||
undef_method :port=
|
||||
|
||||
# Returns the data size, i.e. number of bytes sent on the ping. The
|
||||
# default size is 56.
|
||||
#
|
||||
attr_reader :data_size
|
||||
|
||||
# Creates and returns a new Ping::ICMP object. This is similar to its
|
||||
# superclass constructor, but must be created with root privileges (on
|
||||
# UNIX systems), and the port value is ignored.
|
||||
#
|
||||
def initialize(host=nil, port=nil, timeout=5)
|
||||
raise 'requires root privileges' if Process.euid > 0
|
||||
|
||||
if File::ALT_SEPARATOR && Windows.version >= 6
|
||||
unless Win32::Security.elevated_security?
|
||||
raise 'requires elevated security'
|
||||
end
|
||||
end
|
||||
|
||||
@seq = 0
|
||||
@bind_port = 0
|
||||
@bind_host = nil
|
||||
@data_size = 56
|
||||
@data = ''
|
||||
|
||||
0.upto(@data_size){ |n| @data << (n % 256).chr }
|
||||
|
||||
@pid = Process.pid & 0xffff
|
||||
|
||||
super(host, port, timeout)
|
||||
@port = nil # This value is not used in ICMP pings.
|
||||
end
|
||||
|
||||
# Sets the number of bytes sent in the ping method.
|
||||
#
|
||||
def data_size=(size)
|
||||
@data_size = size
|
||||
@data = ''
|
||||
0.upto(size){ |n| @data << (n % 256).chr }
|
||||
end
|
||||
|
||||
# Associates the local end of the socket connection with the given
|
||||
# +host+ and +port+. The default port is 0.
|
||||
#
|
||||
def bind(host, port = 0)
|
||||
@bind_host = host
|
||||
@bind_port = port
|
||||
end
|
||||
|
||||
# Pings the +host+ specified in this method or in the constructor. If a
|
||||
# host was not specified either here or in the constructor, an
|
||||
# ArgumentError is raised.
|
||||
#
|
||||
def ping(host = @host)
|
||||
super(host)
|
||||
bool = false
|
||||
|
||||
socket = Socket.new(
|
||||
Socket::PF_INET,
|
||||
Socket::SOCK_RAW,
|
||||
Socket::IPPROTO_ICMP
|
||||
)
|
||||
|
||||
if @bind_host
|
||||
saddr = Socket.pack_sockaddr_in(@bind_port, @bind_host)
|
||||
socket.bind(saddr)
|
||||
end
|
||||
|
||||
@seq = (@seq + 1) % 65536
|
||||
pstring = 'C2 n3 A' << @data_size.to_s
|
||||
timeout = @timeout
|
||||
|
||||
checksum = 0
|
||||
msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, @seq, @data].pack(pstring)
|
||||
|
||||
checksum = checksum(msg)
|
||||
msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, @seq, @data].pack(pstring)
|
||||
|
||||
begin
|
||||
saddr = Socket.pack_sockaddr_in(0, host)
|
||||
rescue Exception
|
||||
socket.close unless socket.closed?
|
||||
return bool
|
||||
end
|
||||
|
||||
start_time = Time.now
|
||||
|
||||
socket.send(msg, 0, saddr) # Send the message
|
||||
|
||||
begin
|
||||
Timeout.timeout(@timeout){
|
||||
while true
|
||||
io_array = select([socket], nil, nil, timeout)
|
||||
|
||||
if io_array.nil? || io_array[0].empty?
|
||||
return false
|
||||
end
|
||||
|
||||
pid = nil
|
||||
seq = nil
|
||||
|
||||
data = socket.recvfrom(1500).first
|
||||
type = data[20, 2].unpack('C2').first
|
||||
|
||||
case type
|
||||
when ICMP_ECHOREPLY
|
||||
if data.length >= 28
|
||||
pid, seq = data[24, 4].unpack('n3')
|
||||
end
|
||||
else
|
||||
if data.length > 56
|
||||
pid, seq = data[52, 4].unpack('n3')
|
||||
end
|
||||
end
|
||||
|
||||
if pid == @pid && seq == @seq && type == ICMP_ECHOREPLY
|
||||
bool = true
|
||||
break
|
||||
end
|
||||
end
|
||||
}
|
||||
rescue Exception => err
|
||||
@exception = err
|
||||
ensure
|
||||
socket.close if socket
|
||||
end
|
||||
|
||||
# There is no duration if the ping failed
|
||||
@duration = Time.now - start_time if bool
|
||||
|
||||
return bool
|
||||
end
|
||||
|
||||
alias ping? ping
|
||||
alias pingecho ping
|
||||
|
||||
private
|
||||
|
||||
# Perform a checksum on the message. This is the sum of all the short
|
||||
# words and it folds the high order bits into the low order bits.
|
||||
#
|
||||
def checksum(msg)
|
||||
length = msg.length
|
||||
num_short = length / 2
|
||||
check = 0
|
||||
|
||||
msg.unpack("n#{num_short}").each do |short|
|
||||
check += short
|
||||
end
|
||||
|
||||
if length % 2 > 0
|
||||
check += msg[length-1, 1].unpack('C').first << 8
|
||||
end
|
||||
|
||||
check = (check >> 16) + (check & 0xffff)
|
||||
return (~((check >> 16) + check) & 0xffff)
|
||||
end
|
||||
end
|
||||
end
|
@ -1,89 +0,0 @@
|
||||
require 'socket'
|
||||
require 'timeout'
|
||||
|
||||
# The Net module serves as a namespace only.
|
||||
#
|
||||
module Net
|
||||
|
||||
# The Ping class serves as an abstract base class for all other Ping class
|
||||
# types. You should not instantiate this class directly.
|
||||
#
|
||||
class Ping
|
||||
# The version of the net-ping library.
|
||||
VERSION = '1.7.1'
|
||||
|
||||
# The host to ping. In the case of Ping::HTTP, this is the URI.
|
||||
attr_accessor :host
|
||||
|
||||
# The port to ping. This is set to the echo port (7) by default. The
|
||||
# Ping::HTTP class defaults to port 80.
|
||||
#
|
||||
attr_accessor :port
|
||||
|
||||
# The maximum time a ping attempt is made.
|
||||
attr_accessor :timeout
|
||||
|
||||
# If a ping fails, this value is set to the error that occurred which
|
||||
# caused it to fail.
|
||||
#
|
||||
attr_reader :exception
|
||||
|
||||
# This value is set if a ping succeeds, but some other condition arose
|
||||
# during the ping attempt which merits warning, e.g a redirect in the
|
||||
# case of Ping::HTTP#ping.
|
||||
#
|
||||
attr_reader :warning
|
||||
|
||||
# The number of seconds (returned as a Float) that it took to ping
|
||||
# the host. This is not a precise value, but rather a good estimate
|
||||
# since there is a small amount of internal calculation that is added
|
||||
# to the overall time.
|
||||
#
|
||||
attr_reader :duration
|
||||
|
||||
# The default constructor for the Net::Ping class. Accepts an optional
|
||||
# +host+, +port+ and +timeout+. The port defaults to your echo port, or
|
||||
# 7 if that happens to be undefined. The default timeout is 5 seconds.
|
||||
#
|
||||
# The host, although optional in the constructor, must be specified at
|
||||
# some point before the Net::Ping#ping method is called, or else an
|
||||
# ArgumentError will be raised.
|
||||
#
|
||||
# Yields +self+ in block context.
|
||||
#
|
||||
# This class is not meant to be instantiated directly. It is strictly
|
||||
# meant as an interface for subclasses.
|
||||
#
|
||||
def initialize(host=nil, port=nil, timeout=5)
|
||||
@host = host
|
||||
@port = port || Socket.getservbyname('echo') || 7
|
||||
@timeout = timeout
|
||||
@exception = nil
|
||||
@warning = nil
|
||||
@duration = nil
|
||||
|
||||
yield self if block_given?
|
||||
end
|
||||
|
||||
# The default interface for the Net::Ping#ping method. Each subclass
|
||||
# should call super() before continuing with their own implementation in
|
||||
# order to ensure that the @exception and @warning instance variables
|
||||
# are reset.
|
||||
#
|
||||
# If +host+ is nil here, then it will use the host specified in the
|
||||
# constructor. If the +host+ is nil and there was no host specified
|
||||
# in the constructor then an ArgumentError is raised.
|
||||
#--
|
||||
# The @duration should be set in the subclass' ping method.
|
||||
#
|
||||
def ping(host = @host)
|
||||
raise ArgumentError, 'no host specified' unless host
|
||||
@exception = nil
|
||||
@warning = nil
|
||||
@duration = nil
|
||||
end
|
||||
|
||||
alias ping? ping
|
||||
alias pingecho ping
|
||||
end
|
||||
end
|
102
lib/net/tcp.rb
102
lib/net/tcp.rb
@ -1,102 +0,0 @@
|
||||
require File.join(File.dirname(__FILE__), 'ping')
|
||||
|
||||
# The Net module serves as a namespace only.
|
||||
module Net
|
||||
|
||||
# With a TCP ping simply try to open a connection. If we are successful,
|
||||
# assume success. In either case close the connection to be polite.
|
||||
#
|
||||
class Ping::TCP < Ping
|
||||
@@service_check = false
|
||||
|
||||
# Returns whether or not Errno::ECONNREFUSED is considered a successful
|
||||
# ping. The default is false.
|
||||
#
|
||||
def self.service_check
|
||||
@@service_check
|
||||
end
|
||||
|
||||
# Sets whether or not an Errno::ECONNREFUSED should be considered a
|
||||
# successful ping.
|
||||
#
|
||||
def self.service_check=(bool)
|
||||
unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
|
||||
raise ArgumentError, 'argument must be true or false'
|
||||
end
|
||||
@@service_check = bool
|
||||
end
|
||||
|
||||
# This method attempts to ping a host and port using a TCPSocket with
|
||||
# the host, port and timeout values passed in the constructor. Returns
|
||||
# true if successful, or false otherwise.
|
||||
#
|
||||
# Note that, by default, an Errno::ECONNREFUSED return result will be
|
||||
# considered a failed ping. See the documentation for the
|
||||
# Ping::TCP.service_check= method if you wish to change this behavior.
|
||||
#
|
||||
def ping(host=@host)
|
||||
super(host)
|
||||
|
||||
bool = false
|
||||
start_time = Time.now
|
||||
|
||||
# Failure here most likely means bad host, so just bail.
|
||||
begin
|
||||
addr = Socket.getaddrinfo(host, port)
|
||||
rescue SocketError => err
|
||||
@exception = err
|
||||
return false
|
||||
end
|
||||
|
||||
begin
|
||||
# Where addr[0][0] is likely AF_INET.
|
||||
sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
|
||||
|
||||
# This may not be entirely necessary
|
||||
sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
||||
|
||||
begin
|
||||
# Where addr[0][3] is an IP address
|
||||
sock.connect_nonblock(Socket.pack_sockaddr_in(port, addr[0][3]))
|
||||
rescue Errno::EINPROGRESS
|
||||
# No-op, continue below
|
||||
rescue Exception => err
|
||||
# Something has gone horribly wrong
|
||||
@exception = err
|
||||
return false
|
||||
end
|
||||
|
||||
resp = IO.select(nil, [sock], nil, timeout)
|
||||
|
||||
# Assume ECONNREFUSED at this point
|
||||
if resp.nil?
|
||||
if @@service_check
|
||||
bool = true
|
||||
else
|
||||
@exception = Errno::ECONNREFUSED
|
||||
end
|
||||
else
|
||||
bool = true
|
||||
end
|
||||
ensure
|
||||
sock.close if sock
|
||||
end
|
||||
|
||||
# There is no duration if the ping failed
|
||||
@duration = Time.now - start_time if bool
|
||||
|
||||
bool
|
||||
end
|
||||
|
||||
alias ping? ping
|
||||
alias pingecho ping
|
||||
|
||||
# Class method aliases. DEPRECATED.
|
||||
class << self
|
||||
alias econnrefused service_check
|
||||
alias econnrefused= service_check=
|
||||
alias ecr service_check
|
||||
alias ecr= service_check=
|
||||
end
|
||||
end
|
||||
end
|
119
lib/net/udp.rb
119
lib/net/udp.rb
@ -1,119 +0,0 @@
|
||||
require File.join(File.dirname(__FILE__), 'ping')
|
||||
|
||||
# The Net module serves as a namespace only.
|
||||
module Net
|
||||
|
||||
# The Ping::UDP class encapsulates methods for UDP pings.
|
||||
class Ping::UDP < Ping
|
||||
@@service_check = true
|
||||
|
||||
# Returns whether or not the connect behavior should enforce remote
|
||||
# service availability as well as reachability. The default is true.
|
||||
#
|
||||
def self.service_check
|
||||
@@service_check
|
||||
end
|
||||
|
||||
# Set whether or not the connect behavior should enforce remote
|
||||
# service availability as well as reachability. If set to false
|
||||
# then Errno::ECONNREFUSED or Errno::ECONNRESET will be considered
|
||||
# a successful ping, meaning no actual data handshaking is required.
|
||||
# By default, if either of those errors occurs it is considered a failed
|
||||
# ping.
|
||||
#
|
||||
def self.service_check=(bool)
|
||||
unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
|
||||
raise ArgumentError, 'argument must be true or false'
|
||||
end
|
||||
@@service_check = bool
|
||||
end
|
||||
|
||||
# The maximum data size that can be sent in a UDP ping.
|
||||
MAX_DATA = 64
|
||||
|
||||
# The data to send to the remote host. By default this is 'ping'.
|
||||
# This should be MAX_DATA size characters or less.
|
||||
#
|
||||
attr_reader :data
|
||||
|
||||
# Creates and returns a new Ping::UDP object. This is effectively
|
||||
# identical to its superclass constructor.
|
||||
#
|
||||
def initialize(host=nil, port=nil, timeout=5)
|
||||
@data = 'ping'
|
||||
|
||||
super(host, port, timeout)
|
||||
|
||||
@bind_host = nil
|
||||
@bind_port = nil
|
||||
end
|
||||
|
||||
# Sets the data string sent to the remote host. This value cannot have
|
||||
# a size greater than MAX_DATA.
|
||||
#
|
||||
def data=(string)
|
||||
if string.size > MAX_DATA
|
||||
err = "cannot set data string larger than #{MAX_DATA} characters"
|
||||
raise ArgumentError, err
|
||||
end
|
||||
|
||||
@data = string
|
||||
end
|
||||
|
||||
# Associates the local end of the UDP connection with the given +host+
|
||||
# and +port+. This is essentially a wrapper for UDPSocket#bind.
|
||||
#
|
||||
def bind(host, port)
|
||||
@bind_host = host
|
||||
@bind_port = port
|
||||
end
|
||||
|
||||
# Sends a simple text string to the host and checks the return string. If
|
||||
# the string sent and the string returned are a match then the ping was
|
||||
# successful and true is returned. Otherwise, false is returned.
|
||||
#
|
||||
def ping(host = @host)
|
||||
super(host)
|
||||
|
||||
bool = false
|
||||
udp = UDPSocket.open
|
||||
array = []
|
||||
|
||||
if @bind_host
|
||||
udp.bind(@bind_host, @bind_port)
|
||||
end
|
||||
|
||||
start_time = Time.now
|
||||
|
||||
begin
|
||||
Timeout.timeout(@timeout){
|
||||
udp.connect(host, @port)
|
||||
udp.send(@data, 0)
|
||||
array = udp.recvfrom(MAX_DATA)
|
||||
}
|
||||
rescue Errno::ECONNREFUSED, Errno::ECONNRESET => err
|
||||
if @@service_check
|
||||
@exception = err
|
||||
else
|
||||
bool = true
|
||||
end
|
||||
rescue Exception => err
|
||||
@exception = err
|
||||
else
|
||||
if array[0] == @data
|
||||
bool = true
|
||||
end
|
||||
ensure
|
||||
udp.close if udp
|
||||
end
|
||||
|
||||
# There is no duration if the ping failed
|
||||
@duration = Time.now - start_time if bool
|
||||
|
||||
bool
|
||||
end
|
||||
|
||||
alias ping? ping
|
||||
alias pingecho ping
|
||||
end
|
||||
end
|
118
lib/net/wmi.rb
118
lib/net/wmi.rb
@ -1,118 +0,0 @@
|
||||
require File.join(File.dirname(__FILE__), 'ping')
|
||||
require 'win32ole'
|
||||
|
||||
# The Net module serves as a namespace only.
|
||||
#
|
||||
module Net
|
||||
|
||||
# The Ping::WMI class encapsulates the Win32_PingStatus WMI class for
|
||||
# MS Windows.
|
||||
#
|
||||
class Ping::WMI < Ping
|
||||
|
||||
PingStatus = Struct.new(
|
||||
'PingStatus',
|
||||
:address,
|
||||
:buffer_size,
|
||||
:no_fragmentation,
|
||||
:primary_address_resolution_status,
|
||||
:protocol_address,
|
||||
:protocol_address_resolved,
|
||||
:record_route,
|
||||
:reply_inconsistency,
|
||||
:reply_size,
|
||||
:resolve_address_names,
|
||||
:response_time,
|
||||
:response_time_to_live,
|
||||
:route_record,
|
||||
:route_record_resolved,
|
||||
:source_route,
|
||||
:source_route_type,
|
||||
:status_code,
|
||||
:timeout,
|
||||
:timestamp_record,
|
||||
:timestamp_record_address,
|
||||
:timestamp_record_address_resolved,
|
||||
:timestamp_route,
|
||||
:time_to_live,
|
||||
:type_of_service
|
||||
)
|
||||
|
||||
# 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.
|
||||
#
|
||||
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
|
||||
|
||||
# Unlike Net::Ping::WMI#ping, this method returns true or false to
|
||||
# indicate whether or not the ping was successful.
|
||||
#
|
||||
def ping?(host = @host, options = {})
|
||||
ping(host, options).status_code == 0
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user