ronin-support 0.1.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +4 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/COPYING.txt +504 -0
- data/ChangeLog.md +4 -0
- data/Gemfile +23 -0
- data/README.md +98 -0
- data/Rakefile +29 -0
- data/gemspec.yml +20 -0
- data/lib/ronin/extensions.rb +28 -0
- data/lib/ronin/extensions/file.rb +63 -0
- data/lib/ronin/extensions/ip_addr.rb +220 -0
- data/lib/ronin/extensions/kernel.rb +45 -0
- data/lib/ronin/extensions/meta.rb +22 -0
- data/lib/ronin/extensions/meta/object.rb +24 -0
- data/lib/ronin/extensions/string.rb +200 -0
- data/lib/ronin/formatting.rb +26 -0
- data/lib/ronin/formatting/binary.rb +22 -0
- data/lib/ronin/formatting/digest.rb +22 -0
- data/lib/ronin/formatting/extensions.rb +25 -0
- data/lib/ronin/formatting/extensions/binary.rb +24 -0
- data/lib/ronin/formatting/extensions/binary/file.rb +35 -0
- data/lib/ronin/formatting/extensions/binary/integer.rb +147 -0
- data/lib/ronin/formatting/extensions/binary/string.rb +363 -0
- data/lib/ronin/formatting/extensions/digest.rb +23 -0
- data/lib/ronin/formatting/extensions/digest/file.rb +117 -0
- data/lib/ronin/formatting/extensions/digest/string.rb +80 -0
- data/lib/ronin/formatting/extensions/http.rb +23 -0
- data/lib/ronin/formatting/extensions/http/integer.rb +57 -0
- data/lib/ronin/formatting/extensions/http/string.rb +102 -0
- data/lib/ronin/formatting/extensions/text.rb +23 -0
- data/lib/ronin/formatting/extensions/text/array.rb +125 -0
- data/lib/ronin/formatting/extensions/text/string.rb +206 -0
- data/lib/ronin/formatting/http.rb +22 -0
- data/lib/ronin/formatting/text.rb +22 -0
- data/lib/ronin/network.rb +29 -0
- data/lib/ronin/network/esmtp.rb +22 -0
- data/lib/ronin/network/extensions.rb +29 -0
- data/lib/ronin/network/extensions/esmtp.rb +22 -0
- data/lib/ronin/network/extensions/esmtp/net.rb +100 -0
- data/lib/ronin/network/extensions/http.rb +22 -0
- data/lib/ronin/network/extensions/http/net.rb +661 -0
- data/lib/ronin/network/extensions/imap.rb +22 -0
- data/lib/ronin/network/extensions/imap/net.rb +124 -0
- data/lib/ronin/network/extensions/pop3.rb +22 -0
- data/lib/ronin/network/extensions/pop3/net.rb +92 -0
- data/lib/ronin/network/extensions/smtp.rb +22 -0
- data/lib/ronin/network/extensions/smtp/net.rb +110 -0
- data/lib/ronin/network/extensions/ssl.rb +22 -0
- data/lib/ronin/network/extensions/ssl/net.rb +147 -0
- data/lib/ronin/network/extensions/tcp.rb +22 -0
- data/lib/ronin/network/extensions/tcp/net.rb +304 -0
- data/lib/ronin/network/extensions/telnet.rb +22 -0
- data/lib/ronin/network/extensions/telnet/net.rb +156 -0
- data/lib/ronin/network/extensions/udp.rb +22 -0
- data/lib/ronin/network/extensions/udp/net.rb +226 -0
- data/lib/ronin/network/http.rb +24 -0
- data/lib/ronin/network/http/exceptions.rb +22 -0
- data/lib/ronin/network/http/exceptions/unknown_request.rb +29 -0
- data/lib/ronin/network/http/http.rb +290 -0
- data/lib/ronin/network/http/proxy.rb +307 -0
- data/lib/ronin/network/imap.rb +49 -0
- data/lib/ronin/network/network.rb +41 -0
- data/lib/ronin/network/pop3.rb +49 -0
- data/lib/ronin/network/smtp.rb +24 -0
- data/lib/ronin/network/smtp/email.rb +143 -0
- data/lib/ronin/network/smtp/smtp.rb +68 -0
- data/lib/ronin/network/ssl.rb +47 -0
- data/lib/ronin/network/tcp.rb +22 -0
- data/lib/ronin/network/telnet.rb +109 -0
- data/lib/ronin/network/udp.rb +22 -0
- data/lib/ronin/path.rb +132 -0
- data/lib/ronin/support.rb +28 -0
- data/lib/ronin/support/inflector.rb +40 -0
- data/lib/ronin/support/version.rb +27 -0
- data/lib/ronin/templates.rb +23 -0
- data/lib/ronin/templates/erb.rb +75 -0
- data/lib/ronin/templates/template.rb +161 -0
- data/ronin-support.gemspec +10 -0
- data/spec/extensions/file_spec.rb +24 -0
- data/spec/extensions/ip_addr_spec.rb +171 -0
- data/spec/extensions/kernel_spec.rb +30 -0
- data/spec/extensions/string_spec.rb +177 -0
- data/spec/formatting/binary/helpers/hexdumps.rb +16 -0
- data/spec/formatting/binary/helpers/hexdumps/ascii.bin +0 -0
- data/spec/formatting/binary/helpers/hexdumps/hexdump_decimal_shorts.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_bytes.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_shorts.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_bytes.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_shorts.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/hexdump_repeated.txt +6 -0
- data/spec/formatting/binary/helpers/hexdumps/od_decimal_bytes.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_decimal_ints.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_decimal_quads.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_decimal_shorts.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_hex_bytes.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_hex_ints.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_hex_quads.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_hex_shorts.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_octal_bytes.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_octal_ints.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_octal_quads.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_octal_shorts.txt +17 -0
- data/spec/formatting/binary/helpers/hexdumps/od_repeated.txt +6 -0
- data/spec/formatting/binary/helpers/hexdumps/repeated.bin +1 -0
- data/spec/formatting/binary/integer_spec.rb +140 -0
- data/spec/formatting/binary/string_spec.rb +306 -0
- data/spec/formatting/digest/string_spec.rb +82 -0
- data/spec/formatting/http/integer_spec.rb +42 -0
- data/spec/formatting/http/string_spec.rb +76 -0
- data/spec/formatting/text/array_spec.rb +105 -0
- data/spec/formatting/text/string_spec.rb +180 -0
- data/spec/network/http/http_spec.rb +280 -0
- data/spec/network/http/proxy_spec.rb +150 -0
- data/spec/network/network_spec.rb +8 -0
- data/spec/network/ssl_spec.rb +14 -0
- data/spec/path_spec.rb +84 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/support_spec.rb +8 -0
- data/spec/templates/classes/example_erb.rb +11 -0
- data/spec/templates/classes/example_template.rb +35 -0
- data/spec/templates/erb_spec.rb +21 -0
- data/spec/templates/helpers/data.rb +9 -0
- data/spec/templates/helpers/data/includes/_relative.erb +1 -0
- data/spec/templates/helpers/data/templates/example.erb +1 -0
- data/spec/templates/template_spec.rb +54 -0
- metadata +286 -0
data/ChangeLog.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
platforms :jruby do
|
6
|
+
gem 'jruby-openssl', '~> 0.7.0'
|
7
|
+
end
|
8
|
+
|
9
|
+
group :development do
|
10
|
+
gem 'rake', '~> 0.8.7'
|
11
|
+
|
12
|
+
platforms :jruby do
|
13
|
+
gem 'bluecloth', '>= 2.0.0'
|
14
|
+
end
|
15
|
+
|
16
|
+
platforms :ruby do
|
17
|
+
gem 'rdiscount', '~> 1.6.3'
|
18
|
+
end
|
19
|
+
|
20
|
+
gem 'ore-core', '~> 0.1.0'
|
21
|
+
gem 'ore-tasks', '~> 0.2.0'
|
22
|
+
gem 'rspec', '~> 2.0.0'
|
23
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Ronin Support
|
2
|
+
|
3
|
+
* [Source](http://github.com/ronin-ruby/ronin-support)
|
4
|
+
* [Issues](http://github.com/ronin-ruby/ronin-support/issues)
|
5
|
+
* [Documentation](http://rubydoc.info/github/ronin-ruby/ronin-support/frames)
|
6
|
+
* [Mailing List](http://groups.google.com/group/ronin-ruby)
|
7
|
+
* irc.freenode.net #ronin
|
8
|
+
|
9
|
+
## Description
|
10
|
+
|
11
|
+
Ronin Support is a support library for Ronin. Ronin Support contains many of
|
12
|
+
the convenience methods used by Ronin and additional libraries.
|
13
|
+
|
14
|
+
Ronin is a Ruby platform for exploit development and security research.
|
15
|
+
Ronin allows for the rapid development and distribution of code, exploits
|
16
|
+
or payloads over many common Source-Code-Management (SCM) systems.
|
17
|
+
|
18
|
+
### Ruby
|
19
|
+
|
20
|
+
Ronin's Ruby environment allows security researchers to leverage Ruby with
|
21
|
+
ease. The Ruby environment contains a multitude of convenience methods
|
22
|
+
for working with data in Ruby, a Ruby Object Database, a customized Ruby
|
23
|
+
Console and an extendable command-line interface.
|
24
|
+
|
25
|
+
### Extend
|
26
|
+
|
27
|
+
Ronin's more specialized features are provided by additional Ronin
|
28
|
+
libraries, which users can choose to install. These libraries can allow
|
29
|
+
one to write and run Exploits and Payloads, scan for PHP vulnerabilities,
|
30
|
+
perform Google Dorks or run 3rd party scanners.
|
31
|
+
|
32
|
+
### Publish
|
33
|
+
|
34
|
+
Ronin allows users to publish and share code, exploits, payloads or other
|
35
|
+
data via Overlays. Overlays are directories of code and data that can be
|
36
|
+
hosted on any SVN, Hg, Git or Rsync server. Ronin makes it easy to create,
|
37
|
+
install or update Overlays.
|
38
|
+
|
39
|
+
## Features
|
40
|
+
|
41
|
+
* Provides convenience methods for:
|
42
|
+
* Formatting data:
|
43
|
+
* Binary
|
44
|
+
* Text
|
45
|
+
* HTTP
|
46
|
+
* URIs
|
47
|
+
* Generating random text.
|
48
|
+
* Networking:
|
49
|
+
* TCP
|
50
|
+
* UDP
|
51
|
+
* SMTP / ESMTP
|
52
|
+
* POP3
|
53
|
+
* Imap
|
54
|
+
* Telnet
|
55
|
+
* HTTP / HTTPS
|
56
|
+
* Enumerating IP ranges:
|
57
|
+
* IPv4 / IPv6 addresses.
|
58
|
+
* CIDR / globbed ranges.
|
59
|
+
* (Un-)Hexdumping data.
|
60
|
+
* Handling exceptions.
|
61
|
+
|
62
|
+
## Examples
|
63
|
+
|
64
|
+
For examples of the convenience methods provided by ronin-support,
|
65
|
+
please see [Everyday Ronin](http://ronin-ruby.github.com/resources/everyday_ronin.html).
|
66
|
+
|
67
|
+
## Requirements
|
68
|
+
|
69
|
+
* [Ruby](http://www.ruby-lang.org/) >= 1.8.7
|
70
|
+
* [chars](http://github.com/postmodern/chars) ~> 0.1.2
|
71
|
+
* [data_paths](http://github.com/postmodern/data_paths) ~> 0.2.0
|
72
|
+
* [uri-query_params](http://github.com/postmodern/uri-query_params) ~> 0.5.0
|
73
|
+
|
74
|
+
## Install
|
75
|
+
|
76
|
+
$ sudo gem install ronin-support
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
Ronin Support - A support library for Ronin.
|
81
|
+
|
82
|
+
Copyright (c) 2010 Hal Brodigan (postmodern.mod3 at gmail.com)
|
83
|
+
|
84
|
+
This library is free software; you can redistribute it and/or
|
85
|
+
modify it under the terms of the GNU Lesser General Public
|
86
|
+
License as published by the Free Software Foundation; either
|
87
|
+
version 2.1 of the License, or (at your option) any later version.
|
88
|
+
|
89
|
+
This library is distributed in the hope that it will be useful,
|
90
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
91
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
92
|
+
Lesser General Public License for more details.
|
93
|
+
|
94
|
+
You should have received a copy of the GNU Lesser General Public
|
95
|
+
License along with this library; if not, write to the Free Software
|
96
|
+
Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
97
|
+
Boston, MA 02110-1301 USA
|
98
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler'
|
5
|
+
rescue LoadError => e
|
6
|
+
STDERR.puts e.message
|
7
|
+
STDERR.puts "Run `gem install bundler` to install Bundler."
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
Bundler.setup(:development)
|
13
|
+
rescue Bundler::BundlerError => e
|
14
|
+
STDERR.puts e.message
|
15
|
+
STDERR.puts "Run `bundle install` to install missing gems"
|
16
|
+
exit e.status_code
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake'
|
20
|
+
|
21
|
+
require 'ore/tasks'
|
22
|
+
Ore::Tasks.new
|
23
|
+
|
24
|
+
require 'rspec/core/rake_task'
|
25
|
+
RSpec::Core::RakeTask.new
|
26
|
+
task :default => :spec
|
27
|
+
|
28
|
+
require 'yard'
|
29
|
+
YARD::Rake::YardocTask.new
|
data/gemspec.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
name: ronin-support
|
2
|
+
summary: A support library for Ronin.
|
3
|
+
description:
|
4
|
+
Ronin Support is a support library for Ronin. Ronin Support contains many
|
5
|
+
of the convenience methods used by Ronin and additional libraries.
|
6
|
+
|
7
|
+
license: LGPL-2.1
|
8
|
+
authors: Postmodern
|
9
|
+
email: postmodern.mod3@gmail.com
|
10
|
+
homepage: http://github.com/ronin-ruby/ronin-support
|
11
|
+
has_yard: true
|
12
|
+
|
13
|
+
dependencies:
|
14
|
+
chars: ~> 0.2.0
|
15
|
+
uri-query_params: ~> 0.5.2
|
16
|
+
data_paths: ~> 0.2.1
|
17
|
+
|
18
|
+
development_dependencies:
|
19
|
+
bundler: ~> 1.0.0
|
20
|
+
yard: ~> 0.6.1
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#
|
2
|
+
# Ronin - A Ruby platform for exploit development and security research.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
|
5
|
+
#
|
6
|
+
# This library is free software; you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation; either
|
9
|
+
# version 2.1 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# This library is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
# Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with this library; if not, write to the Free Software
|
18
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
19
|
+
# Boston, MA 02110-1301 USA
|
20
|
+
#
|
21
|
+
|
22
|
+
require 'ronin/extensions/meta'
|
23
|
+
require 'ronin/extensions/string'
|
24
|
+
require 'ronin/extensions/file'
|
25
|
+
require 'ronin/extensions/ip_addr'
|
26
|
+
require 'ronin/extensions/kernel'
|
27
|
+
|
28
|
+
require 'uri/query_params'
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#
|
2
|
+
# Ronin - A Ruby platform for exploit development and security research.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
|
5
|
+
#
|
6
|
+
# This library is free software; you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation; either
|
9
|
+
# version 2.1 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# This library is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
# Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with this library; if not, write to the Free Software
|
18
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
19
|
+
# Boston, MA 02110-1301 USA
|
20
|
+
#
|
21
|
+
|
22
|
+
class File
|
23
|
+
|
24
|
+
#
|
25
|
+
# Writes the given data to a specified path.
|
26
|
+
#
|
27
|
+
# @param [String] path
|
28
|
+
# The path of the file to write to.
|
29
|
+
#
|
30
|
+
# @param [String] data
|
31
|
+
# The data to write to the file.
|
32
|
+
#
|
33
|
+
# @return [nil]
|
34
|
+
#
|
35
|
+
def File.write(path,data)
|
36
|
+
File.open(path,'w') { |file| file.write(data) }
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Escapes a path.
|
41
|
+
#
|
42
|
+
# @param [String] path
|
43
|
+
# Unescaped path.
|
44
|
+
#
|
45
|
+
# @return [String]
|
46
|
+
# The escaped path.
|
47
|
+
#
|
48
|
+
def File.escape_path(path)
|
49
|
+
path = path.to_s
|
50
|
+
|
51
|
+
# remove any \0 characters first
|
52
|
+
path.gsub!("\0",'')
|
53
|
+
|
54
|
+
# remove any home-dir expansions
|
55
|
+
path.gsub!("~","\\~")
|
56
|
+
|
57
|
+
path = File.expand_path(File.join('/',path))
|
58
|
+
|
59
|
+
# remove the leading slash
|
60
|
+
return path[1..-1]
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
#
|
2
|
+
# Ronin - A Ruby platform for exploit development and security research.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
|
5
|
+
#
|
6
|
+
# This library is free software; you can redistribute it and/or
|
7
|
+
# modify it under the terms of the GNU Lesser General Public
|
8
|
+
# License as published by the Free Software Foundation; either
|
9
|
+
# version 2.1 of the License, or (at your option) any later version.
|
10
|
+
#
|
11
|
+
# This library is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14
|
+
# Lesser General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Lesser General Public
|
17
|
+
# License along with this library; if not, write to the Free Software
|
18
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
19
|
+
# Boston, MA 02110-1301 USA
|
20
|
+
#
|
21
|
+
|
22
|
+
require 'ipaddr'
|
23
|
+
require 'resolv'
|
24
|
+
require 'strscan'
|
25
|
+
|
26
|
+
class IPAddr
|
27
|
+
|
28
|
+
include Enumerable
|
29
|
+
|
30
|
+
# A regular expression for matching IPv4 Addresses.
|
31
|
+
IPV4_REGEXP = /[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}/
|
32
|
+
|
33
|
+
# A regular expression for matching IPv6 Addresses.
|
34
|
+
IPV6_REGEXP = /:(:[0-9a-f]{1,4}){1,7}|([0-9a-f]{1,4}::?){1,7}[0-9a-f]{1,4}(:#{IPV4_REGEXP})?/
|
35
|
+
|
36
|
+
# A regular expression for matching IP Addresses.
|
37
|
+
REGEXP = /#{IPV4_REGEXP}|#{IPV6_REGEXP}/
|
38
|
+
|
39
|
+
#
|
40
|
+
# Extracts IP Addresses from text.
|
41
|
+
#
|
42
|
+
# @param [String] text
|
43
|
+
# The text to scan for IP Addresses.
|
44
|
+
#
|
45
|
+
# @param [Symbol] version
|
46
|
+
# The version of IP Address to scan for (`:ipv4` or `:ipv6`).
|
47
|
+
#
|
48
|
+
# @yield [ip]
|
49
|
+
# The given block will be passed each extracted IP Address.
|
50
|
+
#
|
51
|
+
# @yieldparam [String] ip
|
52
|
+
# An IP Address from the text.
|
53
|
+
#
|
54
|
+
# @return [Array<String>]
|
55
|
+
# The IP Addresses found in the text.
|
56
|
+
#
|
57
|
+
def IPAddr.extract(text,version=nil,&block)
|
58
|
+
regexp = case version
|
59
|
+
when :ipv4
|
60
|
+
IPV4_REGEXP
|
61
|
+
when :ipv6
|
62
|
+
IPV6_REGEXP
|
63
|
+
else
|
64
|
+
REGEXP
|
65
|
+
end
|
66
|
+
|
67
|
+
parser = StringScanner.new(text)
|
68
|
+
|
69
|
+
if block_given?
|
70
|
+
yield parser.matched while parser.skip_until(regexp)
|
71
|
+
return nil
|
72
|
+
else
|
73
|
+
ips = []
|
74
|
+
|
75
|
+
ips << parser.matched while parser.skip_until(regexp)
|
76
|
+
return ips
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
#
|
81
|
+
# Iterates over each IP address within the IP Address range. Supports
|
82
|
+
# both IPv4 and IPv6 address ranges.
|
83
|
+
#
|
84
|
+
# @param [String] cidr_or_glob
|
85
|
+
# The IP address range to iterate over.
|
86
|
+
# May be in standard CIDR notation or globbed format.
|
87
|
+
#
|
88
|
+
# @yield [ip]
|
89
|
+
# The block which will be passed each IP address contained within the
|
90
|
+
# IP address range.
|
91
|
+
#
|
92
|
+
# @yieldparam [String] ip
|
93
|
+
# An IP address within the IP address range.
|
94
|
+
#
|
95
|
+
# @return [nil]
|
96
|
+
#
|
97
|
+
# @example Enumerate through a CIDR range
|
98
|
+
# IPAddr.each('10.1.1.1/24') do |ip|
|
99
|
+
# puts ip
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# @example Enumerate through a globbed IP range
|
103
|
+
# IPAddr.each('10.1.1-5.*') do |ip|
|
104
|
+
# puts ip
|
105
|
+
# end
|
106
|
+
#
|
107
|
+
# @example Enumerate through a globbed IPv6 range
|
108
|
+
# IPAddr.each('::ff::02-0a::c3') do |ip|
|
109
|
+
# puts ip
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
def IPAddr.each(cidr_or_glob,&block)
|
113
|
+
unless (cidr_or_glob.include?('*') || cidr_or_glob.include?('-'))
|
114
|
+
return IPAddr.new(cidr_or_glob).each(&block)
|
115
|
+
end
|
116
|
+
|
117
|
+
return enum_for(:each,cidr_or_glob) unless block
|
118
|
+
|
119
|
+
if cidr_or_glob.include?('::')
|
120
|
+
prefix = if cidr_or_glob[0,2] == '::'
|
121
|
+
'::'
|
122
|
+
else
|
123
|
+
''
|
124
|
+
end
|
125
|
+
|
126
|
+
separator = '::'
|
127
|
+
base = 16
|
128
|
+
|
129
|
+
format = lambda { |address|
|
130
|
+
prefix + address.map { |i| '%.2x' % i }.join('::')
|
131
|
+
}
|
132
|
+
else
|
133
|
+
separator = '.'
|
134
|
+
base = 10
|
135
|
+
|
136
|
+
format = lambda { |address| address.join('.') }
|
137
|
+
end
|
138
|
+
|
139
|
+
# split the address
|
140
|
+
ranges = cidr_or_glob.split(separator)
|
141
|
+
|
142
|
+
# map the components of the address to numeric ranges
|
143
|
+
ranges.map! do |segment|
|
144
|
+
if segment == '*'
|
145
|
+
(1..254)
|
146
|
+
elsif segment.include?('-')
|
147
|
+
start, stop = segment.split('-',2).map { |i| i.to_i(base) }
|
148
|
+
|
149
|
+
(start..stop)
|
150
|
+
elsif !(segment.empty?)
|
151
|
+
segment.to_i(base)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
ranges.compact!
|
155
|
+
|
156
|
+
expand_range = lambda { |address,remaining|
|
157
|
+
if remaining.empty?
|
158
|
+
block.call(format.call(address))
|
159
|
+
else
|
160
|
+
n = remaining.first
|
161
|
+
remaining = remaining[1..-1]
|
162
|
+
|
163
|
+
if n.kind_of?(Range)
|
164
|
+
n.each { |i| expand_range.call(address + [i], remaining) }
|
165
|
+
else
|
166
|
+
expand_range.call(address + [n], remaining)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
}
|
170
|
+
|
171
|
+
expand_range.call([], ranges)
|
172
|
+
return nil
|
173
|
+
end
|
174
|
+
|
175
|
+
#
|
176
|
+
# Resolves the host-names for the IP address.
|
177
|
+
#
|
178
|
+
# @return [Array<String>]
|
179
|
+
# The host-names for the IP address.
|
180
|
+
#
|
181
|
+
def lookup
|
182
|
+
Resolv.getnames(self.to_s)
|
183
|
+
end
|
184
|
+
|
185
|
+
#
|
186
|
+
# Iterates over each IP address that is included in the addresses
|
187
|
+
# netmask. Supports both IPv4 and IPv6 addresses.
|
188
|
+
#
|
189
|
+
# @yield [ip]
|
190
|
+
# The block which will be passed every IP address covered be the
|
191
|
+
# netmask of the IPAddr object.
|
192
|
+
#
|
193
|
+
# @yieldparam [String] ip
|
194
|
+
# An IP address.
|
195
|
+
#
|
196
|
+
# @example
|
197
|
+
# netblock = IPAddr.new('10.1.1.1/24')
|
198
|
+
#
|
199
|
+
# netblock.each do |ip|
|
200
|
+
# puts ip
|
201
|
+
# end
|
202
|
+
#
|
203
|
+
def each
|
204
|
+
return enum_for(:each) unless block_given?
|
205
|
+
|
206
|
+
case @family
|
207
|
+
when Socket::AF_INET
|
208
|
+
family_mask = IN4MASK
|
209
|
+
when Socket::AF_INET6
|
210
|
+
family_mask = IN6MASK
|
211
|
+
end
|
212
|
+
|
213
|
+
(0..((~@mask_addr) & family_mask)).each do |i|
|
214
|
+
yield _to_string(@addr | i)
|
215
|
+
end
|
216
|
+
|
217
|
+
return self
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|