Class: Inspec::Resources::UnixHostProvider
Instance Attribute Summary
Attributes inherited from HostProvider
#inspec
Instance Method Summary
collapse
Constructor Details
Returns a new instance of UnixHostProvider.
150
151
152
153
154
155
156
|
# File 'lib/resources/host.rb', line 150
def initialize(inspec)
super
@has_nc = inspec.command('nc').exist?
@has_ncat = inspec.command('ncat').exist?
@has_net_redirections = inspec.command("strings `which bash` | grep -qE '/dev/(tcp|udp)/'").exit_status == 0
end
|
Instance Method Details
#missing_requirements(protocol) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/resources/host.rb', line 158
def missing_requirements(protocol)
missing = []
if %w{tcp udp}.include?(protocol) && !@has_nc && !@has_ncat
if @has_net_redirections
missing << "#{timeout} (part of coreutils) or netcat must be installed" unless inspec.command(timeout).exist?
else
missing << 'netcat must be installed'
end
end
missing
end
|
#netcat_check_command(hostname, port, protocol) ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/resources/host.rb', line 191
def netcat_check_command(hostname, port, protocol)
if @has_nc
base_cmd = 'nc'
elsif @has_ncat
base_cmd = 'ncat'
else
return
end
if protocol == 'udp'
= '-u'
else
= ''
end
"echo | #{base_cmd} -v -w 1 #{} #{hostname} #{port}"
end
|
#ping(hostname, port, protocol) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/resources/host.rb', line 172
def ping(hostname, port, protocol)
if %w{tcp udp}.include?(protocol)
if @has_nc || @has_ncat
resp = inspec.command(netcat_check_command(hostname, port, protocol))
else
resp = inspec.command("#{timeout} 1 bash -c \"< /dev/#{protocol}/#{hostname}/#{port}\"")
end
else
resp = inspec.command("ping -w 1 -c 1 #{hostname}")
end
{
success: resp.exit_status.to_i.zero?,
connection: resp.stderr,
socket: resp.stdout,
}
end
|
#resolve_with_dig(hostname) ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
# File 'lib/resources/host.rb', line 213
def resolve_with_dig(hostname)
addresses = []
cmd = inspec.command("dig +short A #{hostname}")
cmd.stdout.lines.each do |line|
matched = line.chomp.match(Resolv::IPv4::Regex)
addresses << matched.to_s unless matched.nil?
end
cmd = inspec.command("dig +short AAAA #{hostname}")
cmd.stdout.lines.each do |line|
matched = line.chomp.match(Resolv::IPv6::Regex)
addresses << matched.to_s unless matched.nil?
end
addresses.empty? ? nil : addresses
end
|
#resolve_with_getent(hostname) ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/resources/host.rb', line 233
def resolve_with_getent(hostname)
cmd = inspec.command("getent ahosts #{hostname}")
return nil unless cmd.exit_status.to_i.zero?
addresses = []
cmd.stdout.lines.each do |line|
ip, = line.split(/\s+/, 2)
next unless ip.match(Resolv::IPv4::Regex) || ip.match(Resolv::IPv6::Regex)
addresses << ip unless addresses.include?(ip)
end
addresses
end
|
#timeout ⇒ Object
209
210
211
|
# File 'lib/resources/host.rb', line 209
def timeout
'timeout'
end
|