Class: Nmap::Host

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

Instance Method Summary collapse

Constructor Details

#initialize(node) {|host| ... } ⇒ Host

Creates a new Host object.

Parameters:

  • node (Nokogiri::XML::Node)

    The XML node that contains the host information.

Yields:

  • (host)

    If a block is given, it will be passed the newly created Host object.

Yield Parameters:

  • host (Host)

    The newly created Host object.



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

def initialize(node,&block)
  @node = node

  block.call(self) if block
end

Instance Method Details

#addressString

The address of the host.

Returns:

  • (String)

    The IP or MAC address of the host.



174
175
176
# File 'lib/nmap/host.rb', line 174

def address
  ip || mac
end

#addressesArray<Host>

Parses the addresses of the host.

Returns:

  • (Array<Host>)

    The addresses of the host.



106
107
108
# File 'lib/nmap/host.rb', line 106

def addresses
  each_address.to_a
end

#each(&block) ⇒ Object

Parses the open ports of the host.

See Also:



366
367
368
# File 'lib/nmap/host.rb', line 366

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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nmap/host.rb', line 85

def each_address
  return enum_for(:each_address) 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 (String)

    A hostname of the host.

Returns:

  • (Host, Enumerator)

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



191
192
193
194
195
196
197
198
199
# File 'lib/nmap/host.rb', line 191

def each_hostname
  return enum_for(:each_hostname) unless block_given?

  @node.xpath("hostnames/hostname[@name]").each do |host|
    yield 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.



275
276
277
278
279
280
281
282
283
# File 'lib/nmap/host.rb', line 275

def each_open_port(&block)
  return enum_for(:each_open_port) 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.



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

def each_port
  return enum_for(:each_port) 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.



308
309
310
311
312
313
314
315
316
# File 'lib/nmap/host.rb', line 308

def each_tcp_port
  return enum_for(:each_tcp_port) 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.



341
342
343
344
345
346
347
348
349
# File 'lib/nmap/host.rb', line 341

def each_udp_port
  return enum_for(:each_udp_port) 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
  Time.at(@node['endtime'].to_i)
end

#hostnamesArray<String>

Parses the hostnames of the host.

Returns:

  • (Array<String>)

    The hostnames of the host.



207
208
209
# File 'lib/nmap/host.rb', line 207

def hostnames
  each_hostname.to_a
end

#ipString

The IP address of the host.

Returns:

  • (String)

    The IPv4 or IPv6 address of the host.



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

def ip
  ipv6 || ipv4
end

#ipv4String

Parses the IPv4 address of the host.

Returns:

  • (String)

    The IPv4 address of the host.



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

def ipv4
  unless @ipv4
    addr = @node.xpath("address[@addr][@addrtype='ipv4']").first

    @ipv4 = addr['addr'] if addr
  end

  return @ipv4
end

#ipv6String

Parses the IPv6 address of the host.

Returns:

  • (String)

    The IPv6 address of the host.



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

def ipv6
  unless @ipv6
    addr = @node.xpath("address[@addr][@addrtype='ipv6']").first

    @ipv6 = addr['addr'] if addr
  end

  return @ipv6
end

#macString

Parses the MAC address of the host.

Returns:

  • (String)

    The MAC address of the host.



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

def mac
  unless @mac
    addr = @node.xpath("address[@addr][@addrtype='mac']").first

    @mac = addr['addr'] if addr
  end

  return @mac
end

#open_portsArray<Port>

Parses the open ports of the host.

Returns:

  • (Array<Port>)

    The open ports of the host.



291
292
293
# File 'lib/nmap/host.rb', line 291

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.



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

def os(&block)
  os = @node.at('os')

  return OS.new(os,&block) if os
end

#portsArray<Port>

Parses the scanned ports of the host.

Returns:

  • (Array<Port>)

    The scanned ports of the host.



258
259
260
# File 'lib/nmap/host.rb', line 258

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



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

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
  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
# File 'lib/nmap/host.rb', line 63

def status
  status = @node.at('status')

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

#tcp_portsArray<Port>

Parses the TCP ports of the host.

Returns:

  • (Array<Port>)

    The TCP ports of the host.



324
325
326
# File 'lib/nmap/host.rb', line 324

def tcp_ports
  each_tcp_port.to_a
end

#to_sString

Converts the host to a String.

Returns:

  • (String)

    The address of the host.

See Also:



398
399
400
# File 'lib/nmap/host.rb', line 398

def to_s
  address.to_s
end

#udp_portsArray<Port>

Parses the UDP ports of the host.

Returns:

  • (Array<Port>)

    The UDP ports of the host.



357
358
359
# File 'lib/nmap/host.rb', line 357

def udp_ports
  each_udp_port.to_a
end