Class: Nmap::Host

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

Overview

Wraps a host XML element.

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.



30
31
32
# File 'lib/nmap/host.rb', line 30

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.



182
183
184
# File 'lib/nmap/host.rb', line 182

def address
  ip || mac
end

#addressesArray<Host>

Parses the addresses of the host.

Returns:

  • (Array<Host>)

    The addresses of the host.



112
113
114
# File 'lib/nmap/host.rb', line 112

def addresses
  each_address.to_a
end

#each(&block) ⇒ Object

Parses the open ports of the host.

See Also:



504
505
506
# File 'lib/nmap/host.rb', line 504

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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/nmap/host.rb', line 90

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.



199
200
201
202
203
204
205
206
207
# File 'lib/nmap/host.rb', line 199

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.



413
414
415
416
417
418
419
420
421
# File 'lib/nmap/host.rb', line 413

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.



380
381
382
383
384
385
386
387
388
# File 'lib/nmap/host.rb', line 380

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.



446
447
448
449
450
451
452
453
454
# File 'lib/nmap/host.rb', line 446

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.



479
480
481
482
483
484
485
486
487
# File 'lib/nmap/host.rb', line 479

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



54
55
56
# File 'lib/nmap/host.rb', line 54

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



534
535
536
537
538
# File 'lib/nmap/host.rb', line 534

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



226
227
228
# File 'lib/nmap/host.rb', line 226

def hostname
  each_hostname.first
end

#hostnamesArray<Hostname>

Parses the hostnames of the host.

Returns:

  • (Array<Hostname>)

    The hostnames of the host.



215
216
217
# File 'lib/nmap/host.rb', line 215

def hostnames
  each_hostname.to_a
end

#inspectString

Inspects the host.

Returns:

  • (String)

    The inspected host.



581
582
583
# File 'lib/nmap/host.rb', line 581

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

#ipString

The IP address of the host.

Returns:

  • (String)

    The IPv4 or IPv6 address of the host.



172
173
174
# File 'lib/nmap/host.rb', line 172

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:



319
320
321
322
323
324
325
326
# File 'lib/nmap/host.rb', line 319

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

#ipidsequence(&block) ⇒ Object

Deprecated.

Use #ip_id_sequence instead.



331
332
333
334
335
# File 'lib/nmap/host.rb', line 331

def ipidsequence(&block)
  warn "DEPRECATION: use #{self.class}#ip_id_sequence instead"

  ip_id_sequence(&block)
end

#ipv4String

Parses the IPv4 address of the host.

Returns:

  • (String)

    The IPv4 address of the host.



148
149
150
151
152
# File 'lib/nmap/host.rb', line 148

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.



160
161
162
163
164
# File 'lib/nmap/host.rb', line 160

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.



122
123
124
125
126
# File 'lib/nmap/host.rb', line 122

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.



429
430
431
# File 'lib/nmap/host.rb', line 429

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.



242
243
244
245
246
247
248
249
# File 'lib/nmap/host.rb', line 242

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.



396
397
398
# File 'lib/nmap/host.rb', line 396

def ports
  each_port.to_a
end

#scriptsHash{String => String}

Deprecated.

Use #host_script instead.

The output from the NSE scripts ran against the host.

Returns:

  • (Hash{String => String})

    The NSE script names and output.

Since:

  • 0.3.0



518
519
520
521
522
523
524
# File 'lib/nmap/host.rb', line 518

def scripts
  if host_script
    host_script.scripts
  else
    {}
  end
end

#start_timeTime

The time the host was first scanned.

Returns:

  • (Time)

    The time the host was first scanned.

Since:

  • 0.1.2



42
43
44
# File 'lib/nmap/host.rb', line 42

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.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nmap/host.rb', line 64

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

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

  return @status
end

#tcp_portsArray<Port>

Parses the TCP ports of the host.

Returns:

  • (Array<Port>)

    The TCP ports of the host.



462
463
464
# File 'lib/nmap/host.rb', line 462

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:



289
290
291
292
293
294
295
296
# File 'lib/nmap/host.rb', line 289

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:



349
350
351
352
353
354
355
356
# File 'lib/nmap/host.rb', line 349

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

#tcpsequence(&block) ⇒ Object

Deprecated.

Use #tcp_sequence instead.



301
302
303
304
305
# File 'lib/nmap/host.rb', line 301

def tcpsequence(&block)
  warn "DEPRECATION: use #{self.class}#tcp_sequence instead"

  tcp_sequence(&block)
end

#tcptssequence(&block) ⇒ Object

Deprecated.

Use #tcp_ts_sequence instead.



361
362
363
364
365
# File 'lib/nmap/host.rb', line 361

def tcptssequence(&block)
  warn "DEPRECATION: use #{self.class}#tcp_ts_sequence instead"

  tcp_ts_sequence(&block)
end

#to_sString

Converts the host to a String.

Returns:

  • (String)

    The hostname or address of the host.

See Also:



571
572
573
# File 'lib/nmap/host.rb', line 571

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



554
555
556
557
558
559
560
561
# File 'lib/nmap/host.rb', line 554

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.



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

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



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/nmap/host.rb', line 265

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



136
137
138
139
140
# File 'lib/nmap/host.rb', line 136

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