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.



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

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.



180
181
182
# File 'lib/nmap/host.rb', line 180

def address
  ip || mac
end

#addressesArray<Host>

Parses the addresses of the host.

Returns:

  • (Array<Host>)

    The addresses of the host.



110
111
112
# File 'lib/nmap/host.rb', line 110

def addresses
  each_address.to_a
end

#each(&block) ⇒ Object

Parses the open ports of the host.

See Also:



502
503
504
# File 'lib/nmap/host.rb', line 502

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.



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

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']
    )

    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.



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

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.



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

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.



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

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.



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

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.



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

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



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

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

#hostnameHostname?

The primary hostname of the host.

Returns:

Since:

  • 0.8.0



224
225
226
# File 'lib/nmap/host.rb', line 224

def hostname
  each_hostname.first
end

#hostnamesArray<Hostname>

Parses the hostnames of the host.

Returns:

  • (Array<Hostname>)

    The hostnames of the host.



213
214
215
# File 'lib/nmap/host.rb', line 213

def hostnames
  each_hostname.to_a
end

#inspectString

Inspects the host.

Returns:

  • (String)

    The inspected host.



567
568
569
# File 'lib/nmap/host.rb', line 567

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

#ipString

The IP address of the host.

Returns:

  • (String)

    The IPv4 or IPv6 address of the host.



170
171
172
# File 'lib/nmap/host.rb', line 170

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:



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

def ip_id_sequence
  @ip_id_sequence ||= if (seq = @node.at('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.



329
330
331
332
333
# File 'lib/nmap/host.rb', line 329

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.



146
147
148
149
150
# File 'lib/nmap/host.rb', line 146

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

#ipv6String

Parses the IPv6 address of the host.

Returns:

  • (String)

    The IPv6 address of the host.



158
159
160
161
162
# File 'lib/nmap/host.rb', line 158

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

#macString

Parses the MAC address of the host.

Returns:

  • (String)

    The MAC address of the host.



120
121
122
123
124
# File 'lib/nmap/host.rb', line 120

def mac
  @mac ||= if (addr = @node.at("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.



427
428
429
# File 'lib/nmap/host.rb', line 427

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.



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

def os
  @os ||= if (os = @node.at('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.



394
395
396
# File 'lib/nmap/host.rb', line 394

def ports
  each_port.to_a
end

#scriptsHash{String => String}

The output from the NSE scripts ran against the host.

Returns:

  • (Hash{String => String})

    The NSE script names and output.

Since:

  • 0.3.0



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

def scripts
  unless @scripts
    @scripts = {}

    @node.xpath('hostscript/script').each do |script|
      @scripts[script['id']] = script['output']
    end
  end

  return @scripts
end

#start_timeTime

The time the host was first scanned.

Returns:

  • (Time)

    The time the host was first scanned.

Since:

  • 0.1.2



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

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.



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

def status
  unless @status
    status = @node.at('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.



460
461
462
# File 'lib/nmap/host.rb', line 460

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:



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

def tcp_sequence
  @tcp_sequence ||= if (seq = @node.at('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:



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

def tcp_ts_sequence
  @tcp_ts_sequence ||= if (seq = @node.at('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.



299
300
301
302
303
# File 'lib/nmap/host.rb', line 299

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

  tcp_sequence(&block)
end

#tcptssequence(&block) ⇒ Object

Deprecated.

Use #tcp_ts_sequence instead.



359
360
361
362
363
# File 'lib/nmap/host.rb', line 359

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:



557
558
559
# File 'lib/nmap/host.rb', line 557

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



540
541
542
543
544
545
546
547
# File 'lib/nmap/host.rb', line 540

def traceroute
  @traceroute ||= if (trace = @node.at('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.



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

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



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

def uptime
  @uptime ||= if (uptime = @node.at('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



134
135
136
137
138
# File 'lib/nmap/host.rb', line 134

def vendor
  @vendor ||= if (vendor = @node.at("address[@vendor]"))
             vendor['vendor']
           end
end