net-ping 1.4.1-x86-mingw32 → 1.5.3-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +258 -232
- data/MANIFEST +25 -23
- data/README +52 -46
- data/Rakefile +100 -89
- data/doc/ping.txt +274 -249
- data/examples/example_pingexternal.rb +16 -16
- data/examples/example_pinghttp.rb +22 -22
- data/examples/example_pingldap.rb +22 -0
- data/examples/example_pingtcp.rb +16 -16
- data/examples/example_pingudp.rb +12 -12
- data/lib/net/ping.rb +18 -15
- data/lib/net/ping/external.rb +122 -126
- data/lib/net/ping/http.rb +149 -120
- data/lib/net/ping/icmp.rb +180 -168
- data/lib/net/ping/ldap.rb +107 -0
- data/lib/net/ping/ping.rb +89 -88
- data/lib/net/ping/tcp.rb +83 -83
- data/lib/net/ping/udp.rb +119 -119
- data/lib/net/ping/wmi.rb +118 -118
- data/net-ping.gemspec +43 -38
- data/test/test_net_ping.rb +37 -37
- data/test/test_net_ping_external.rb +128 -121
- data/test/test_net_ping_http.rb +182 -176
- data/test/test_net_ping_icmp.rb +120 -116
- data/test/test_net_ping_ldap.rb +200 -0
- data/test/test_net_ping_tcp.rb +108 -108
- data/test/test_net_ping_udp.rb +122 -122
- data/test/test_net_ping_wmi.rb +84 -84
- metadata +59 -26
data/lib/net/ping/icmp.rb
CHANGED
@@ -1,168 +1,180 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'ping')
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
98
|
-
|
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
|
-
|
1
|
+
require File.join(File.dirname(__FILE__), 'ping')
|
2
|
+
|
3
|
+
if File::ALT_SEPARATOR
|
4
|
+
require 'win32/security'
|
5
|
+
require 'windows/system_info'
|
6
|
+
include Windows::SystemInfo
|
7
|
+
end
|
8
|
+
|
9
|
+
# The Net module serves as a namespace only.
|
10
|
+
module Net
|
11
|
+
|
12
|
+
# The Net::Ping::ICMP class encapsulates an icmp ping.
|
13
|
+
class Ping::ICMP < Ping
|
14
|
+
ICMP_ECHOREPLY = 0 # Echo reply
|
15
|
+
ICMP_ECHO = 8 # Echo request
|
16
|
+
ICMP_SUBCODE = 0
|
17
|
+
|
18
|
+
# You cannot set or change the port value. A value of 0 is always
|
19
|
+
# used internally for ICMP pings.
|
20
|
+
#
|
21
|
+
undef_method :port=
|
22
|
+
|
23
|
+
# Returns the data size, i.e. number of bytes sent on the ping. The
|
24
|
+
# default size is 56.
|
25
|
+
#
|
26
|
+
attr_reader :data_size
|
27
|
+
|
28
|
+
# Creates and returns a new Ping::ICMP object. This is similar to its
|
29
|
+
# superclass constructor, but must be created with root privileges (on
|
30
|
+
# UNIX systems), and the port value is ignored.
|
31
|
+
#
|
32
|
+
def initialize(host=nil, port=nil, timeout=5)
|
33
|
+
raise 'requires root privileges' if Process.euid > 0
|
34
|
+
|
35
|
+
if File::ALT_SEPARATOR && windows_version >= 6
|
36
|
+
unless Win32::Security.elevated_security?
|
37
|
+
raise 'requires elevated security'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
@seq = 0
|
42
|
+
@bind_port = 0
|
43
|
+
@bind_host = nil
|
44
|
+
@data_size = 56
|
45
|
+
@data = ''
|
46
|
+
|
47
|
+
0.upto(@data_size){ |n| @data << (n % 256).chr }
|
48
|
+
|
49
|
+
@pid = Process.pid & 0xffff
|
50
|
+
|
51
|
+
super(host, port, timeout)
|
52
|
+
@port = nil # This value is not used in ICMP pings.
|
53
|
+
end
|
54
|
+
|
55
|
+
# Sets the number of bytes sent in the ping method.
|
56
|
+
#
|
57
|
+
def data_size=(size)
|
58
|
+
@data_size = size
|
59
|
+
@data = ''
|
60
|
+
0.upto(size){ |n| @data << (n % 256).chr }
|
61
|
+
end
|
62
|
+
|
63
|
+
# Associates the local end of the socket connection with the given
|
64
|
+
# +host+ and +port+. The default port is 0.
|
65
|
+
#
|
66
|
+
def bind(host, port = 0)
|
67
|
+
@bind_host = host
|
68
|
+
@bind_port = port
|
69
|
+
end
|
70
|
+
|
71
|
+
# Pings the +host+ specified in this method or in the constructor. If a
|
72
|
+
# host was not specified either here or in the constructor, an
|
73
|
+
# ArgumentError is raised.
|
74
|
+
#
|
75
|
+
def ping(host = @host)
|
76
|
+
super(host)
|
77
|
+
bool = false
|
78
|
+
|
79
|
+
socket = Socket.new(
|
80
|
+
Socket::PF_INET,
|
81
|
+
Socket::SOCK_RAW,
|
82
|
+
Socket::IPPROTO_ICMP
|
83
|
+
)
|
84
|
+
|
85
|
+
if @bind_host
|
86
|
+
saddr = Socket.pack_sockaddr_in(@bind_port, @bind_host)
|
87
|
+
socket.bind(saddr)
|
88
|
+
end
|
89
|
+
|
90
|
+
@seq = (@seq + 1) % 65536
|
91
|
+
pstring = 'C2 n3 A' << @data_size.to_s
|
92
|
+
timeout = @timeout
|
93
|
+
|
94
|
+
checksum = 0
|
95
|
+
msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, @seq, @data].pack(pstring)
|
96
|
+
|
97
|
+
checksum = checksum(msg)
|
98
|
+
msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, @seq, @data].pack(pstring)
|
99
|
+
|
100
|
+
begin
|
101
|
+
saddr = Socket.pack_sockaddr_in(0, host)
|
102
|
+
rescue Exception
|
103
|
+
socket.close unless socket.closed?
|
104
|
+
return bool
|
105
|
+
end
|
106
|
+
|
107
|
+
start_time = Time.now
|
108
|
+
|
109
|
+
socket.send(msg, 0, saddr) # Send the message
|
110
|
+
|
111
|
+
begin
|
112
|
+
Timeout.timeout(@timeout){
|
113
|
+
while true
|
114
|
+
io_array = select([socket], nil, nil, timeout)
|
115
|
+
|
116
|
+
if io_array.nil? || io_array[0].empty?
|
117
|
+
return false
|
118
|
+
end
|
119
|
+
|
120
|
+
pid = nil
|
121
|
+
seq = nil
|
122
|
+
|
123
|
+
data = socket.recvfrom(1500).first
|
124
|
+
type = data[20, 2].unpack('C2').first
|
125
|
+
|
126
|
+
case type
|
127
|
+
when ICMP_ECHOREPLY
|
128
|
+
if data.length >= 28
|
129
|
+
pid, seq = data[24, 4].unpack('n3')
|
130
|
+
end
|
131
|
+
else
|
132
|
+
if data.length > 56
|
133
|
+
pid, seq = data[52, 4].unpack('n3')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
if pid == @pid && seq == @seq && type == ICMP_ECHOREPLY
|
138
|
+
bool = true
|
139
|
+
break
|
140
|
+
end
|
141
|
+
end
|
142
|
+
}
|
143
|
+
rescue Exception => err
|
144
|
+
@exception = err
|
145
|
+
ensure
|
146
|
+
socket.close if socket
|
147
|
+
end
|
148
|
+
|
149
|
+
# There is no duration if the ping failed
|
150
|
+
@duration = Time.now - start_time if bool
|
151
|
+
|
152
|
+
return bool
|
153
|
+
end
|
154
|
+
|
155
|
+
alias ping? ping
|
156
|
+
alias pingecho ping
|
157
|
+
|
158
|
+
private
|
159
|
+
|
160
|
+
# Perform a checksum on the message. This is the sum of all the short
|
161
|
+
# words and it folds the high order bits into the low order bits.
|
162
|
+
#
|
163
|
+
def checksum(msg)
|
164
|
+
length = msg.length
|
165
|
+
num_short = length / 2
|
166
|
+
check = 0
|
167
|
+
|
168
|
+
msg.unpack("n#{num_short}").each do |short|
|
169
|
+
check += short
|
170
|
+
end
|
171
|
+
|
172
|
+
if length % 2 > 0
|
173
|
+
check += msg[length-1, 1].unpack('C').first << 8
|
174
|
+
end
|
175
|
+
|
176
|
+
check = (check >> 16) + (check & 0xffff)
|
177
|
+
return (~((check >> 16) + check) & 0xffff)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'ping')
|
2
|
+
require 'net/ldap'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
|
6
|
+
# The Net module serves as a namespace only.
|
7
|
+
module Net
|
8
|
+
|
9
|
+
# The Ping::LDAP class encapsulates methods for LDAP pings.
|
10
|
+
class Ping::LDAP < Ping
|
11
|
+
|
12
|
+
# uri contains the URI object for the request
|
13
|
+
#
|
14
|
+
attr_accessor :uri
|
15
|
+
|
16
|
+
# username and password may be set for ping using
|
17
|
+
# an authenticated LDAP bind
|
18
|
+
#
|
19
|
+
attr_accessor :username
|
20
|
+
attr_accessor :password
|
21
|
+
|
22
|
+
# set/get the encryption method. By default nil,
|
23
|
+
# but may be set to :simple_tls
|
24
|
+
#
|
25
|
+
attr_accessor :encryption
|
26
|
+
def encryption=(value)
|
27
|
+
@encryption = (value.is_a? Symbol) ? value : value.to_sym
|
28
|
+
end
|
29
|
+
|
30
|
+
# Creates and returns a new Ping::LDAP object.
|
31
|
+
# The default +timeout+ is 5 seconds.
|
32
|
+
#
|
33
|
+
# +uri+ string is expected to be a full URI with scheme (ldap/ldaps)
|
34
|
+
# and optionally the port (else default port is assumed) e.g.
|
35
|
+
# ldap://my.ldap.host.com
|
36
|
+
# ldap://my.ldap.host.com:1389
|
37
|
+
# ldaps://my.ldap.host.com
|
38
|
+
# ldaps://my.ldap.host.com:6636
|
39
|
+
#
|
40
|
+
# If a plain hostname is provided as the +uri+, a default port of 389 is assumed
|
41
|
+
#
|
42
|
+
def initialize(uri=nil, timeout=5)
|
43
|
+
host, port = decode_uri(uri)
|
44
|
+
super(host, port, timeout)
|
45
|
+
end
|
46
|
+
|
47
|
+
# method used to decode uri string
|
48
|
+
#
|
49
|
+
def decode_uri(value)
|
50
|
+
@uri = URI.parse(value)
|
51
|
+
if uri.scheme =~ /ldap/
|
52
|
+
p = @port = uri.port
|
53
|
+
h = @host = uri.host
|
54
|
+
@encryption = uri.scheme=='ldaps' ? :simple_tls : nil
|
55
|
+
else
|
56
|
+
h = value
|
57
|
+
p = 389
|
58
|
+
end
|
59
|
+
[h, p]
|
60
|
+
end
|
61
|
+
|
62
|
+
# constructs the LDAP configuration structure
|
63
|
+
#
|
64
|
+
def config
|
65
|
+
{
|
66
|
+
:host => uri.host,
|
67
|
+
:port => uri.port,
|
68
|
+
:encryption => encryption
|
69
|
+
}.merge(
|
70
|
+
(username && password) ?
|
71
|
+
{ :auth => {:method => :simple, :username => username, :password => password} } :
|
72
|
+
{ :auth => {:method => :anonymous} }
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
# perform ping, optionally providing the ping destination uri
|
77
|
+
#
|
78
|
+
def ping(host = nil)
|
79
|
+
decode_uri(host) if host
|
80
|
+
super(@host)
|
81
|
+
|
82
|
+
bool = false
|
83
|
+
|
84
|
+
start_time = Time.now
|
85
|
+
|
86
|
+
begin
|
87
|
+
Timeout.timeout(@timeout) do
|
88
|
+
Net::LDAP.new( config ).bind
|
89
|
+
end
|
90
|
+
rescue Net::LDAP::LdapError => e
|
91
|
+
@exception = e.message
|
92
|
+
rescue Exception => e
|
93
|
+
@exception = e.message
|
94
|
+
else
|
95
|
+
bool = true
|
96
|
+
end
|
97
|
+
|
98
|
+
# There is no duration if the ping failed
|
99
|
+
@duration = Time.now - start_time if bool
|
100
|
+
|
101
|
+
bool
|
102
|
+
end
|
103
|
+
|
104
|
+
alias ping? ping
|
105
|
+
alias pingecho ping
|
106
|
+
end
|
107
|
+
end
|