Class: Nmap::XML::Host

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nmap/xml/host.rb

Overview

Wraps a host XML element.

Since:

  • 1.0.0

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Host

Creates a new Host object.

Parameters:

  • node (Nokogiri::XML::Node)

    The XML node that contains the host information.

Since:

  • 1.0.0



33
34
35
# File 'lib/nmap/xml/host.rb', line 33

def initialize(node)
  @node = node
end

Instance Method Details

#addressString

The address of the host.

Returns:

  • (String)

    The IP or MAC address of the host.

Since:

  • 1.0.0



186
187
188
# File 'lib/nmap/xml/host.rb', line 186

def address
  ip || mac
end

#addressesArray<Host>

Parses the addresses of the host.

Returns:

  • (Array<Host>)

    The addresses of the host.

Since:

  • 1.0.0



116
117
118
# File 'lib/nmap/xml/host.rb', line 116

def addresses
  each_address.to_a
end

#each(&block) ⇒ Object

Parses the open ports of the host.

See Also:

Since:

  • 1.0.0



481
482
483
# File 'lib/nmap/xml/host.rb', line 481

def each(&block)
  each_open_port(&block)
end

#each_address {|addr| ... } ⇒ Host, Enumerator

Parses each address of the host.

Yields:

  • (addr)

    Each parsed address will be pass to a given block.

Yield Parameters:

  • addr (Address)

    A address of the host.

Returns:

  • (Host, Enumerator)

    The host. If no block was given, an enumerator will be returned.

Since:

  • 1.0.0



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nmap/xml/host.rb', line 94

def each_address
  return enum_for(__method__) unless block_given?

  @node.xpath("address[@addr]").each do |addr|
    address = Address.new(
      addr['addrtype'].to_sym,
      addr['addr'],
      addr['vendor']
    )

    yield address
  end

  return self
end

#each_hostname {|host| ... } ⇒ Host, Enumerator

Parses the hostnames of the host.

Yields:

  • (host)

    Each parsed hostname will be passed to the given block.

Yield Parameters:

  • host (Hostname)

    A hostname of the host.

Returns:

  • (Host, Enumerator)

    The host. If no block was given, an enumerator will be returned.

Since:

  • 1.0.0



203
204
205
206
207
208
209
210
211
# File 'lib/nmap/xml/host.rb', line 203

def each_hostname
  return enum_for(__method__) unless block_given?

  @node.xpath("hostnames/hostname[@name]").each do |host|
    yield Hostname.new(host['type'],host['name'])
  end

  return self
end

#each_open_port {|port| ... } ⇒ Host, Enumerator

Parses the open ports of the host.

Yields:

  • (port)

    Each open port of the host.

Yield Parameters:

  • port (Port)

    An open scanned port of the host.

Returns:

  • (Host, Enumerator)

    The host. If no block was given, an enumerator will be returned.

Since:

  • 1.0.0



390
391
392
393
394
395
396
397
398
# File 'lib/nmap/xml/host.rb', line 390

def each_open_port
  return enum_for(__method__) unless block_given?

  @node.xpath("ports/port[state/@state='open']").each do |port|
    yield Port.new(port)
  end

  return self
end

#each_port {|port| ... } ⇒ Host, Enumerator

Parses the scanned ports of the host.

Yields:

  • (port)

    Each scanned port of the host.

Yield Parameters:

  • port (Port)

    A scanned port of the host.

Returns:

  • (Host, Enumerator)

    The host. If no block was given, an enumerator will be returned.

Since:

  • 1.0.0



357
358
359
360
361
362
363
364
365
# File 'lib/nmap/xml/host.rb', line 357

def each_port
  return enum_for(__method__) unless block_given?

  @node.xpath("ports/port").each do |port|
    yield Port.new(port)
  end

  return self
end

#each_tcp_port {|port| ... } ⇒ Host, Enumerator

Parses the TCP ports of the host.

Yields:

  • (port)

    Each TCP port of the host.

Yield Parameters:

  • port (Port)

    An TCP scanned port of the host.

Returns:

  • (Host, Enumerator)

    The host. If no block was given, an enumerator will be returned.

Since:

  • 1.0.0



423
424
425
426
427
428
429
430
431
# File 'lib/nmap/xml/host.rb', line 423

def each_tcp_port
  return enum_for(__method__) unless block_given?

  @node.xpath("ports/port[@protocol='tcp']").each do |port|
    yield Port.new(port)
  end

  return self
end

#each_udp_port {|port| ... } ⇒ Host, Enumerator

Parses the UDP ports of the host.

Yields:

  • (port)

    Each UDP port of the host.

Yield Parameters:

  • port (Port)

    An UDP scanned port of the host.

Returns:

  • (Host, Enumerator)

    The host. If no block was given, an enumerator will be returned.

Since:

  • 1.0.0



456
457
458
459
460
461
462
463
464
# File 'lib/nmap/xml/host.rb', line 456

def each_udp_port
  return enum_for(__method__) unless block_given?

  @node.xpath("ports/port[@protocol='udp']").each do |port|
    yield Port.new(port)
  end

  return self
end

#end_timeTime

The time the host was last scanned.

Returns:

  • (Time)

    The time the host was last scanned.

Since:

  • 0.1.2



57
58
59
# File 'lib/nmap/xml/host.rb', line 57

def end_time
  @end_time ||= Time.at(@node['endtime'].to_i)
end

#host_scriptHostScript?

The NSE scripts ran against the host.

Returns:

  • (HostScript, nil)

    Contains the host script output and data.

Since:

  • 0.9.0



493
494
495
496
497
# File 'lib/nmap/xml/host.rb', line 493

def host_script
  @host_script ||= if (hostscript = @node.at_xpath('hostscript'))
                     HostScript.new(hostscript)
                   end
end

#hostnameHostname?

The primary hostname of the host.

Returns:

Since:

  • 0.8.0



230
231
232
# File 'lib/nmap/xml/host.rb', line 230

def hostname
  each_hostname.first
end

#hostnamesArray<Hostname>

Parses the hostnames of the host.

Returns:

  • (Array<Hostname>)

    The hostnames of the host.

Since:

  • 1.0.0



219
220
221
# File 'lib/nmap/xml/host.rb', line 219

def hostnames
  each_hostname.to_a
end

#inspectString

Inspects the host.

Returns:

  • (String)

    The inspected host.

Since:

  • 1.0.0



540
541
542
# File 'lib/nmap/xml/host.rb', line 540

def inspect
  "#<#{self.class}: #{self}>"
end

#ipString

The IP address of the host.

Returns:

  • (String)

    The IPv4 or IPv6 address of the host.

Since:

  • 1.0.0



176
177
178
# File 'lib/nmap/xml/host.rb', line 176

def ip
  ipv6 || ipv4
end

#ip_id_sequence {|ipidsequence| ... } ⇒ IpIdSequence

Parses the IPID sequence number analysis of the host.

Yields:

  • (ipidsequence)

    If a block is given, it will be passed the resulting object

Yield Parameters:

  • ipidsequence (IpIdSequence)

    IPID Sequence number analysis.

Returns:

Since:

  • 1.0.0



314
315
316
317
318
319
320
321
# File 'lib/nmap/xml/host.rb', line 314

def ip_id_sequence
  @ip_id_sequence ||= if (seq = @node.at_xpath('ipidsequence'))
                        IpIdSequence.new(seq)
                      end

  yield @ip_id_sequence if (@ip_id_sequence && block_given?)
  return @ip_id_sequence
end

#ipv4String

Parses the IPv4 address of the host.

Returns:

  • (String)

    The IPv4 address of the host.

Since:

  • 1.0.0



152
153
154
155
156
# File 'lib/nmap/xml/host.rb', line 152

def ipv4
  @ipv4 ||= if (addr = @node.at_xpath("address[@addrtype='ipv4']"))
              addr['addr']
            end
end

#ipv6String

Parses the IPv6 address of the host.

Returns:

  • (String)

    The IPv6 address of the host.

Since:

  • 1.0.0



164
165
166
167
168
# File 'lib/nmap/xml/host.rb', line 164

def ipv6
  @ipv6 ||= if (addr = @node.at_xpath("address[@addrtype='ipv6']"))
              addr['addr']
            end
end

#macString

Parses the MAC address of the host.

Returns:

  • (String)

    The MAC address of the host.

Since:

  • 1.0.0



126
127
128
129
130
# File 'lib/nmap/xml/host.rb', line 126

def mac
  @mac ||= if (addr = @node.at_xpath("address[@addrtype='mac']"))
             addr['addr']
           end
end

#open_portsArray<Port>

Parses the open ports of the host.

Returns:

  • (Array<Port>)

    The open ports of the host.

Since:

  • 1.0.0



406
407
408
# File 'lib/nmap/xml/host.rb', line 406

def open_ports
  each_open_port.to_a
end

#os {|os| ... } ⇒ OS

Parses the OS guessing information of the host.

Yields:

  • (os)

    If a block is given, it will be passed the OS guessing information.

Yield Parameters:

  • os (OS)

    The OS guessing information.

Returns:

  • (OS)

    The OS guessing information.

Since:

  • 1.0.0



246
247
248
249
250
251
252
253
# File 'lib/nmap/xml/host.rb', line 246

def os
  @os ||= if (os = @node.at_xpath('os'))
            OS.new(os)
          end

  yield @os if (@os && block_given?)
  return @os
end

#portsArray<Port>

Parses the scanned ports of the host.

Returns:

  • (Array<Port>)

    The scanned ports of the host.

Since:

  • 1.0.0



373
374
375
# File 'lib/nmap/xml/host.rb', line 373

def ports
  each_port.to_a
end

#start_timeTime

The time the host was first scanned.

Returns:

  • (Time)

    The time the host was first scanned.

Since:

  • 0.1.2



45
46
47
# File 'lib/nmap/xml/host.rb', line 45

def start_time
  @start_time ||= Time.at(@node['starttime'].to_i)
end

#statusStatus

Parses the status of the host.

Returns:

  • (Status)

    The status of the host.

Since:

  • 1.0.0



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nmap/xml/host.rb', line 67

def status
  unless @status
    status = @node.at_xpath('status')

    @status = Status.new(
      status['state'].to_sym,
      status['reason'],
      status['reason_ttl'].to_i
    )
  end

  return @status
end

#tcp_portsArray<Port>

Parses the TCP ports of the host.

Returns:

  • (Array<Port>)

    The TCP ports of the host.

Since:

  • 1.0.0



439
440
441
# File 'lib/nmap/xml/host.rb', line 439

def tcp_ports
  each_tcp_port.to_a
end

#tcp_sequence {|sequence| ... } ⇒ TcpSequence

Parses the TCP Sequence number analysis of the host.

Yields:

  • (sequence)

    If a block is given, it will be passed the resulting object

Yield Parameters:

  • sequence (TcpSequence)

    TCP Sequence number analysis.

Returns:

Since:

  • 1.0.0



293
294
295
296
297
298
299
300
# File 'lib/nmap/xml/host.rb', line 293

def tcp_sequence
  @tcp_sequence ||= if (seq = @node.at_xpath('tcpsequence'))
                      TcpSequence.new(seq)
                    end

  yield @tcp_sequence if (@tcp_sequence && block_given?)
  return @tcp_sequence
end

#tcp_ts_sequence {|tcptssequence| ... } ⇒ TcpTsSequence

Parses the TCP Timestamp sequence number analysis of the host.

Yields:

  • (tcptssequence)

    If a block is given, it will be passed the resulting object

Yield Parameters:

  • tcptssequence (TcpTsSequence)

    TCP Timestamp Sequence number analysis.

Returns:

Since:

  • 1.0.0



335
336
337
338
339
340
341
342
# File 'lib/nmap/xml/host.rb', line 335

def tcp_ts_sequence
  @tcp_ts_sequence ||= if (seq = @node.at_xpath('tcptssequence'))
                         TcpTsSequence.new(seq)
                       end

  yield @tcp_ts_sequence if (@tcp_ts_sequence && block_given?)
  return @tcp_ts_sequence
end

#to_sString

Converts the host to a String.

Returns:

  • (String)

    The hostname or address of the host.

See Also:

Since:

  • 1.0.0



530
531
532
# File 'lib/nmap/xml/host.rb', line 530

def to_s
  (hostname || address).to_s
end

#traceroute {|traceroute| ... } ⇒ Traceroute

Parses the traceroute information, if present.

Yields:

  • (traceroute)

    If a block is given, it will be passed the traceroute information.

Yield Parameters:

  • traceroute (Traceroute)

    The traceroute information.

Returns:

Since:

  • 0.7.0



513
514
515
516
517
518
519
520
# File 'lib/nmap/xml/host.rb', line 513

def traceroute
  @traceroute ||= if (trace = @node.at_xpath('trace'))
                    Traceroute.new(trace)
                  end

  yield @traceroute if (@traceroute && block_given?)
  return @traceroute
end

#udp_portsArray<Port>

Parses the UDP ports of the host.

Returns:

  • (Array<Port>)

    The UDP ports of the host.

Since:

  • 1.0.0



472
473
474
# File 'lib/nmap/xml/host.rb', line 472

def udp_ports
  each_udp_port.to_a
end

#uptime {|uptime| ... } ⇒ Uptime

Parses the Uptime analysis of the host.

Yields:

  • (uptime)

    If a block is given, it will be passed the resulting object

Yield Parameters:

Returns:

  • (Uptime)

    The parsed object.

Since:

  • 0.7.0



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/nmap/xml/host.rb', line 269

def uptime
  @uptime ||= if (uptime = @node.at_xpath('uptime'))
                Uptime.new(
                  uptime['seconds'].to_i,
                  Time.parse(uptime['lastboot'])
                )
              end

  yield @uptime if (@uptime && block_given?)
  return @uptime
end

#vendorString

Parses the MAC vendor of the host.

Returns:

  • (String)

    The Mac Vendor of the host.

Since:

  • 0.8.0



140
141
142
143
144
# File 'lib/nmap/xml/host.rb', line 140

def vendor
  @vendor ||= if (vendor = @node.at_xpath("address/@vendor"))
                vendor.inner_text
              end
end