ruby-nmap 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog.md CHANGED
@@ -1,3 +1,12 @@
1
+ ### 0.4.1 / 2010-11-23
2
+
3
+ * Fixed a bug in {Nmap::XML#each} where it was calling `each_up_hosts`,
4
+ and not {Nmap::XML#each_up_host}.
5
+ * {Nmap::OS#each_class}, {Nmap::OS#each_match}, {Nmap::XML#each_host} and
6
+ {Nmap::XML#each_up_host} now return an Enumerator object if no block
7
+ is given.
8
+ * Use `yield` instead of `block.call` for a slight performance improvement.
9
+
1
10
  ### 0.4.0 / 2010-11-17
2
11
 
3
12
  * Added new options to {Nmap::Task} based on nmap 5.21:
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ rescue LoadError => e
12
12
  end
13
13
 
14
14
  begin
15
- gem 'rspec', '~> 2.0.0'
15
+ gem 'rspec', '~> 2.1.0'
16
16
  require 'rspec/core/rake_task'
17
17
 
18
18
  RSpec::Core::RakeTask.new
data/gemspec.yml CHANGED
@@ -18,5 +18,5 @@ dependencies:
18
18
 
19
19
  development_dependencies:
20
20
  ore-tasks: ~> 0.3.0
21
- rspec: ~> 2.0.0
21
+ rspec: ~> 2.1.0
22
22
  yard: ~> 0.6.0
data/lib/nmap/host.rb CHANGED
@@ -4,7 +4,6 @@ require 'nmap/os'
4
4
  require 'nmap/port'
5
5
 
6
6
  require 'nokogiri'
7
- require 'enumerator'
8
7
 
9
8
  module Nmap
10
9
  class Host
@@ -24,10 +23,10 @@ module Nmap
24
23
  # @yieldparam [Host] host
25
24
  # The newly created Host object.
26
25
  #
27
- def initialize(node,&block)
26
+ def initialize(node)
28
27
  @node = node
29
28
 
30
- block.call(self) if block
29
+ yield self if block_given?
31
30
  end
32
31
 
33
32
  #
@@ -39,7 +38,7 @@ module Nmap
39
38
  # @since 0.1.2
40
39
  #
41
40
  def start_time
42
- Time.at(@node['starttime'].to_i)
41
+ @start_time ||= Time.at(@node['starttime'].to_i)
43
42
  end
44
43
 
45
44
  #
@@ -51,7 +50,7 @@ module Nmap
51
50
  # @since 0.1.2
52
51
  #
53
52
  def end_time
54
- Time.at(@node['endtime'].to_i)
53
+ @end_time ||= Time.at(@node['endtime'].to_i)
55
54
  end
56
55
 
57
56
  #
@@ -61,12 +60,16 @@ module Nmap
61
60
  # The status of the host.
62
61
  #
63
62
  def status
64
- status = @node.at('status')
63
+ unless @status
64
+ status = @node.at('status')
65
65
 
66
- return Status.new(
67
- status['state'].to_sym,
68
- status['reason']
69
- )
66
+ @status = Status.new(
67
+ status['state'].to_sym,
68
+ status['reason']
69
+ )
70
+ end
71
+
72
+ return @status
70
73
  end
71
74
 
72
75
  #
@@ -221,9 +224,9 @@ module Nmap
221
224
  # The OS guessing information.
222
225
  #
223
226
  def os(&block)
224
- os = @node.at('os')
225
-
226
- return OS.new(os,&block) if os
227
+ if (os = @node.at('os'))
228
+ @os = OS.new(os,&block)
229
+ end
227
230
  end
228
231
 
229
232
  #
data/lib/nmap/os.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require 'nmap/os_class'
2
2
  require 'nmap/os_match'
3
3
 
4
- require 'enumerator'
5
-
6
4
  module Nmap
7
5
  class OS
8
6
 
@@ -20,10 +18,10 @@ module Nmap
20
18
  # @yieldparam [OS] os
21
19
  # The newly created OS object.
22
20
  #
23
- def initialize(node,&block)
21
+ def initialize(node)
24
22
  @node = node
25
23
 
26
- block.call(self) if block
24
+ yield self if block_given?
27
25
  end
28
26
 
29
27
  #
@@ -35,10 +33,13 @@ module Nmap
35
33
  # @yieldparam [OSClass] class
36
34
  # The OS class information.
37
35
  #
38
- # @return [OS]
39
- # The OS information.
36
+ # @return [OS, Enumerator]
37
+ # The OS information. If no block was given, an enumerator object
38
+ # will be returned.
40
39
  #
41
- def each_class(&block)
40
+ def each_class
41
+ return enum_for(:each_class) unless block_given?
42
+
42
43
  @node.xpath("osclass").map do |osclass|
43
44
  os_class = OSClass.new(
44
45
  osclass['type'].to_sym,
@@ -47,7 +48,7 @@ module Nmap
47
48
  osclass['accuracy'].to_i
48
49
  )
49
50
 
50
- block.call(os_class) if block
51
+ yield os_class
51
52
  end
52
53
 
53
54
  return self
@@ -60,7 +61,7 @@ module Nmap
60
61
  # The OS class information.
61
62
  #
62
63
  def classes
63
- Enumerator.new(self,:each_class).to_a
64
+ enum_for(:each_class).to_a
64
65
  end
65
66
 
66
67
  #
@@ -72,17 +73,20 @@ module Nmap
72
73
  # @yieldparam [OSMatch] class
73
74
  # The OS match information.
74
75
  #
75
- # @return [OS]
76
- # The OS information.
76
+ # @return [OS, Enumerator]
77
+ # The OS information. If no block was given, an enumerator object
78
+ # will be returned.
77
79
  #
78
- def each_match(&block)
80
+ def each_match
81
+ return enum_for(:each_match) unless block_given?
82
+
79
83
  @node.xpath("osmatch").map do |osclass|
80
84
  os_match = OSMatch.new(
81
85
  osclass['name'],
82
86
  osclass['accuracy'].to_i
83
87
  )
84
88
 
85
- block.call(os_match) if block
89
+ yield os_match
86
90
  end
87
91
 
88
92
  return self
@@ -95,7 +99,7 @@ module Nmap
95
99
  # The OS match information.
96
100
  #
97
101
  def matches
98
- Enumerator.new(self,:each_match).to_a
102
+ enum_for(:each_match).to_a
99
103
  end
100
104
 
101
105
  #
@@ -105,7 +109,9 @@ module Nmap
105
109
  # The ports used.
106
110
  #
107
111
  def ports_used
108
- @node.xpath("portused/@portid").map { |port| port.inner_text.to_i }
112
+ @ports_used ||= @node.xpath("portused/@portid").map do |port|
113
+ port.inner_text.to_i
114
+ end
109
115
  end
110
116
 
111
117
  #
@@ -115,7 +121,7 @@ module Nmap
115
121
  # The OS fingerprint.
116
122
  #
117
123
  def fingerprint
118
- @node.at("osfingerprint/@fingerprint").inner_text
124
+ @fingerprint ||= @node.at("osfingerprint/@fingerprint").inner_text
119
125
  end
120
126
 
121
127
  #
data/lib/nmap/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Nmap
2
2
  # ruby-nmap version
3
- VERSION = '0.4.0'
3
+ VERSION = '0.4.1'
4
4
  end
data/lib/nmap/xml.rb CHANGED
@@ -4,7 +4,6 @@ require 'nmap/scan'
4
4
  require 'nmap/host'
5
5
 
6
6
  require 'nokogiri'
7
- require 'enumerator'
8
7
 
9
8
  module Nmap
10
9
  class XML
@@ -26,11 +25,11 @@ module Nmap
26
25
  # @yieldparam [XML] xml
27
26
  # The newly created XML object.
28
27
  #
29
- def initialize(path,&block)
28
+ def initialize(path)
30
29
  @path = File.expand_path(path)
31
30
  @doc = Nokogiri::XML(File.new(@path))
32
31
 
33
- block.call(self) if block
32
+ yield self if block_given?
34
33
  end
35
34
 
36
35
  #
@@ -130,12 +129,15 @@ module Nmap
130
129
  # @yieldparam [Host] host
131
130
  # A host in the scan.
132
131
  #
133
- # @return [XML]
134
- # The XML object.
132
+ # @return [XML, Enumerator]
133
+ # The XML object. If no block was given, an enumerator object will
134
+ # be returned.
135
135
  #
136
136
  def each_host(&block)
137
+ return enum_for(:each_host) unless block
138
+
137
139
  @doc.xpath('/nmaprun/host').each do |host|
138
- block.call(Host.new(host)) if block
140
+ Host.new(host,&block)
139
141
  end
140
142
 
141
143
  return self
@@ -148,7 +150,7 @@ module Nmap
148
150
  # The hosts in the scan.
149
151
  #
150
152
  def hosts
151
- Enumerator.new(self,:each_host).to_a
153
+ enum_for(:each_host).to_a
152
154
  end
153
155
 
154
156
  #
@@ -160,10 +162,13 @@ module Nmap
160
162
  # @yieldparam [Host] host
161
163
  # A host in the scan.
162
164
  #
163
- # @return [XML]
164
- # The XML parser.
165
+ # @return [XML, Enumerator]
166
+ # The XML parser. If no block was given, an enumerator object will
167
+ # be returned.
165
168
  #
166
169
  def each_up_host(&block)
170
+ return enum_for(:each_up_host) unless block
171
+
167
172
  @doc.xpath("/nmaprun/host[status[@state='up']]").each do |host|
168
173
  Host.new(host,&block)
169
174
  end
@@ -178,16 +183,16 @@ module Nmap
178
183
  # The hosts in the scan.
179
184
  #
180
185
  def up_hosts
181
- Enumerator.new(self,:each_up_host).to_a
186
+ enum_for(:each_up_host).to_a
182
187
  end
183
188
 
184
189
  #
185
190
  # Parses the hosts that were found to be up during the scan.
186
191
  #
187
- # @see each_up_hosts
192
+ # @see each_up_host
188
193
  #
189
194
  def each(&block)
190
- each_up_hosts(&block)
195
+ each_up_host(&block)
191
196
  end
192
197
 
193
198
  #
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ gem 'rspec', '~> 2.1.0'
1
2
  require 'rspec'
2
3
  require 'nmap/version'
3
4
 
data/spec/xml_spec.rb CHANGED
@@ -66,6 +66,10 @@ describe XML do
66
66
  @xml.hosts.length.should == 10
67
67
  end
68
68
 
69
+ it "should iterate over each up host" do
70
+ @xml.each.all? { |host| host.status.state == :up }.should == true
71
+ end
72
+
69
73
  it "should convert to a String" do
70
74
  @xml.to_s.should == Helpers::SCAN_FILE
71
75
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 0
9
- version: 0.4.0
8
+ - 1
9
+ version: 0.4.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Postmodern
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-17 00:00:00 -08:00
17
+ date: 2010-11-23 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -72,9 +72,9 @@ dependencies:
72
72
  - !ruby/object:Gem::Version
73
73
  segments:
74
74
  - 2
75
+ - 1
75
76
  - 0
76
- - 0
77
- version: 2.0.0
77
+ version: 2.1.0
78
78
  type: :development
79
79
  version_requirements: *id004
80
80
  - !ruby/object:Gem::Dependency