ip_in_range 0.9 → 1.02

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ip_in_range.gemspec CHANGED
@@ -7,13 +7,11 @@ Gem::Specification.new do |s|
7
7
  s.date = Date.today.strftime('%F')
8
8
  s.summary = SUMMARY
9
9
  s.description = "Verify that an IP is in a range"
10
- s.authors = AUTHORS
11
- s.email = EMAIL
12
- s.files = %w~ip_in_range~.collect{|f| 'bin/' << f} + %w~log.conf ip_range.rb version.rb file_checking.rb logging.rb email.rb~.collect{|f| 'lib/' << f} + ['doc/license.txt','ip_in_range.gemspec']
10
+ s.authors = AUTHORS.join ','
11
+ s.email = AUTHORS_MAIL.join ','
12
+ s.files = %w~ip_in_range~.collect{|f| 'bin/' << f} + %w~ip_range.rb version.rb file_checking.rb basic_logging.rb email.rb~.collect{|f| 'lib/' << f} + ['doc/license.txt','ip_in_range.gemspec'] + ["README.md"]
13
13
  s.homepage = ''
14
- #s.requirements = ''
15
- #s.add_runtime_dependency ''
16
14
  s.executables = 'ip_in_range'
17
- s.license = 'GPL-3.0'
18
- s.required_ruby_version = '>= 2.7'
15
+ s.license = 'Nonstandard'
16
+ s.required_ruby_version = '>= 3.0'
19
17
  end
@@ -0,0 +1,170 @@
1
+ #!/bin/env ruby
2
+ #encoding: UTF-8
3
+ =begin
4
+ /***************************************************************************
5
+ * ©2013-2023, Michael Uplawski <michael.uplawski@uplawski.eu> *
6
+ * *
7
+ * This program is free software; you can redistribute it and/or modify *
8
+ * it under the terms of the WTFPL 2.0 or later, see *
9
+ * http://www.wtfpl.net/about/ *
10
+ * *
11
+ * This program 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. *
14
+ * *
15
+ ***************************************************************************/
16
+ =end
17
+
18
+ #
19
+ # Simplified logging.
20
+ # See example code at the bottom of this file.
21
+ # Execute this file to see the output.
22
+ #
23
+
24
+ #require 'time'
25
+
26
+ module BasicLogging
27
+
28
+ DEBUG = 0
29
+ INFO = 1
30
+ WARN = 2
31
+ ERROR = 3
32
+ FATAL = 4
33
+ UNKNOWN = nil
34
+
35
+ # this is mainly for the translation of method calls into log levels
36
+ Levels = {:debug => DEBUG, :info => INFO, :warn => WARN, :error => ERROR,
37
+ :fatal => FATAL, :unknown => UNKNOWN}
38
+
39
+ @@log_level = UNKNOWN
40
+ @@target = STDOUT
41
+ @@muted = []
42
+
43
+ # do not log, if caller is obj (class or instance)
44
+ def self.mute(obj)
45
+ name = obj.class == Class ? obj.name.dup : obj.class.name
46
+ @@muted << name
47
+ end
48
+
49
+ def self.is_muted?(obj)
50
+ name = obj.class == Class ? obj.name.dup : obj.class.name
51
+ @@muted.include?(name)
52
+ end
53
+
54
+ # set the log level
55
+ def set_level(lv)
56
+ if lv.respond_to?(:to_str)
57
+ lv = Levels[lv.to_sym]
58
+ end
59
+ if(!lv || (lv.respond_to?(:to_int) && lv >= DEBUG && lv <= FATAL) )
60
+ @@log_level = lv
61
+ else
62
+ STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << lv.to_s << "\""
63
+ STDERR.puts "Keepinng old log level " << Levels.keys.detect {| k| Levels[k] == @@log_level}.to_s
64
+ end
65
+ end
66
+
67
+ # set the log target
68
+ def set_target(tg)
69
+ if tg.respond_to?(:to_io)
70
+ @@target = tg
71
+ elsif(!File::exist?(tg) || ( File.file?(tg) && File.writable?(tg) ) )
72
+ @@target = File.open(tg, 'w+')
73
+ else
74
+ STDERR.puts __FILE__.dup << ': ERROR : target ' << tg << ' cannot be set'
75
+ STDERR.puts "Keeping old target " << @@target.inspect
76
+ return
77
+ end
78
+ end
79
+
80
+ # Output of log messages, depending on the log level set for the calling class
81
+ # and the name of the alias method which is actually called.
82
+ def log(message)
83
+ if !BasicLogging.is_muted?(self)
84
+ # how has this method been called?
85
+ mlevel = __callee__
86
+ if Levels.has_key?(mlevel) && Levels[mlevel] <= FATAL
87
+ # output only for levels equal or above the value that corresponds to
88
+ # the calling alias.
89
+ format_log( message, mlevel) if @@log_level && Levels[mlevel] >= @@log_level
90
+ else
91
+ STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << mlevel.to_s << "\""
92
+ end
93
+ end
94
+ end
95
+
96
+ alias :debug :log
97
+ alias :info :log
98
+ alias :warn :log
99
+ alias :error :log
100
+ alias :fatal :log
101
+
102
+ attr_reader :target, :log_level
103
+
104
+ private
105
+
106
+ # 1 format_log for all loggers.
107
+ def format_log(message, mlevel)
108
+ # indicate if a registered class or the registered object of a class is calling.
109
+ name = self.class == Class ? self.name.dup << ' [class]' : self.class.name
110
+ @@target.puts '' << name << ' ' << mlevel.to_s << ' ' << Time.now.strftime("%H:%M:%S:%6N") << ': ' << message
111
+ end
112
+ end
113
+ #---------test: execute file----------
114
+ if $0 == __FILE__
115
+ Array.extend(BasicLogging)
116
+ Array.set_level(BasicLogging::INFO)
117
+ Array.info('TEST')
118
+ ar = Array.new
119
+ ar.extend(BasicLogging)
120
+ # --- no output :
121
+ l = __LINE__
122
+ ar.debug(l.next.to_s << ': debug-test 0')
123
+ # output
124
+ ar.set_level(BasicLogging::DEBUG)
125
+ l = __LINE__
126
+ ar.debug(l.next.to_s << ': debug-test 1')
127
+
128
+ obj = Object.new
129
+ obj.extend(BasicLogging)
130
+ obj.set_level(BasicLogging::DEBUG)
131
+ puts "--------debug-----------"
132
+ obj.debug('debug')
133
+ obj.info('info')
134
+ obj.warn('warn')
135
+ obj.error('error')
136
+ obj.fatal('fatal')
137
+ puts "--------info-----------"
138
+ obj.set_level("info")
139
+ obj.debug('debug')
140
+ obj.info('info')
141
+ obj.warn('warn')
142
+ obj.error('error')
143
+ obj.fatal('fatal')
144
+ puts "--------fatal-----------"
145
+ obj.set_level("fatal")
146
+ obj.debug('debug')
147
+ obj.info('info')
148
+ obj.warn('warn')
149
+ obj.error('error')
150
+ obj.fatal('fatal')
151
+ puts "--------UNKNOWN-----------"
152
+ obj.set_level(nil)
153
+ obj.debug('debug')
154
+ obj.info('info')
155
+ obj.warn('warn')
156
+ obj.error('error')
157
+ obj.fatal('fatal')
158
+ puts " ------ Output into file ----"
159
+ obj.set_target "/tmp/test_log.log"
160
+ puts " ------ INFO -----------"
161
+ obj.set_level BasicLogging::INFO
162
+ obj.info('info output')
163
+
164
+ obj.info('info output 2')
165
+ puts "---------- invalid -------"
166
+ obj.set_target "/dev/sr0"
167
+ obj.set_level "power"
168
+ end
169
+
170
+ # EOF
data/lib/email.rb CHANGED
@@ -2,40 +2,32 @@
2
2
  #encoding: UTF-8
3
3
  =begin
4
4
  /***************************************************************************
5
- * ©2021-2021, Michael Uplawski <michael.uplawski@uplawski.eu> *
5
+ * ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
6
6
  * *
7
7
  * This program is free software; you can redistribute it and/or modify *
8
- * it under the terms of the GNU General Public License as published by *
9
- * the Free Software Foundation; either version 3 of the License, or *
10
- * (at your option) any later version. *
8
+ * it under the terms of the WTFPL 2.0 or later, see *
9
+ * http://www.wtfpl.net/about/ *
11
10
  * *
12
11
  * This program is distributed in the hope that it will be useful, *
13
12
  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
- * GNU General Public License for more details. *
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
16
14
  * *
17
- * You should have received a copy of the GNU General Public License *
18
- * along with this program; if not, write to the *
19
- * Free Software Foundation, Inc., *
20
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21
15
  ***************************************************************************/
22
16
  =end
23
17
 
24
- require_relative 'logging'
18
+ require_relative 'basic_logging'
25
19
  require 'mail'
26
20
 
27
21
  class FMail
28
- extend Logging
29
- @@log = init_logger(STDOUT)
22
+ include BasicLogging
30
23
 
31
24
  def initialize(mail_text)
32
- @log = @@log
33
25
  if !mail_text || mail_text.empty?
34
- @log.error('No mail-text provided. Aborting!')
26
+ error('No mail-text provided. Aborting!')
35
27
  exit false
36
28
  end
37
29
  @mail_text = mail_text
38
- # @log.debug('mail text is ' << @mail_text)
30
+ # debug('mail text is ' << @mail_text)
39
31
  extr_received
40
32
  end
41
33
 
@@ -47,13 +39,13 @@ class FMail
47
39
  begin
48
40
  @mail = Mail::read_from_string(@mail_text)
49
41
  rescue Exception => ex
50
- @log.error("cannot analyze this mail: " << ex.message)
51
- @log.error("Aborting!")
42
+ error("cannot analyze this mail: " << ex.message)
43
+ error("Aborting!")
52
44
  exit false;
53
45
  end
54
46
  headers = @mail.header_fields
55
47
  @received = headers.filter{|h| h.name == 'Received'}.collect{|h| h.unparsed_value}
56
- @log.debug("found received-headers: " << @received.to_s)
48
+ debug("found received-headers: " << @received.to_s)
57
49
  end
58
50
 
59
51
  end
data/lib/file_checking.rb CHANGED
@@ -1,22 +1,16 @@
1
1
  #encoding: UTF-8
2
2
  =begin
3
3
  /***************************************************************************
4
- * ©2011-2021 Michael Uplawski <michael.uplawski@uplawski.eu> *
4
+ * ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
5
5
  * *
6
6
  * This program is free software; you can redistribute it and/or modify *
7
- * it under the terms of the GNU General Public License as published by *
8
- * the Free Software Foundation; either version 3 of the License, or *
9
- * (at your option) any later version. *
7
+ * it under the terms of the WTFPL 2.0 or later, see *
8
+ * http://www.wtfpl.net/about/ *
10
9
  * *
11
10
  * This program is distributed in the hope that it will be useful, *
12
11
  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
- * GNU General Public License for more details. *
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
15
13
  * *
16
- * You should have received a copy of the GNU General Public License *
17
- * along with this program; if not, write to the *
18
- * Free Software Foundation, Inc., *
19
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20
14
  ***************************************************************************/
21
15
  =end
22
16
 
data/lib/ip_range.rb CHANGED
@@ -2,29 +2,23 @@
2
2
  #encoding: UTF-8
3
3
  =begin
4
4
  /***************************************************************************
5
- * ©2021-2021, Michael Uplawski <michael.uplawski@uplawski.eu> *
5
+ * ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
6
6
  * *
7
7
  * This program is free software; you can redistribute it and/or modify *
8
- * it under the terms of the GNU General Public License as published by *
9
- * the Free Software Foundation; either version 3 of the License, or *
10
- * (at your option) any later version. *
8
+ * it under the terms of the WTFPL 2.0 or later, see *
9
+ * http://www.wtfpl.net/about/ *
11
10
  * *
12
11
  * This program is distributed in the hope that it will be useful, *
13
12
  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
- * GNU General Public License for more details. *
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
16
14
  * *
17
- * You should have received a copy of the GNU General Public License *
18
- * along with this program; if not, write to the *
19
- * Free Software Foundation, Inc., *
20
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21
15
  ***************************************************************************/
22
16
  =end
23
17
 
24
- require_relative 'logging'
18
+
19
+ require_relative 'basic_logging'
25
20
  class IPRange
26
- extend Logging
27
- @@log = init_logger(STDOUT)
21
+ include BasicLogging
28
22
 
29
23
  private
30
24
 
@@ -35,7 +29,7 @@ class IPRange
35
29
  if nibbles.size == 4
36
30
  ( (nibbles[0] * 256 + nibbles[1] ) * 256 + nibbles[2] ) * 256 + nibbles[3]
37
31
  else
38
- @log.error(ip.dup << " is not an IP-address!")
32
+ error(ip.dup << " is not an IP-address!")
39
33
  return nil
40
34
  end
41
35
  end
@@ -53,7 +47,6 @@ class IPRange
53
47
 
54
48
  # create an IP-range
55
49
  def initialize(args)
56
- @log = @@log
57
50
  if args && args.length >= 2
58
51
 
59
52
  #first IP
@@ -61,7 +54,8 @@ class IPRange
61
54
  #last IP
62
55
  @last = ip_to_number args[1]
63
56
  if @last && @first && ( @last < @first )
64
- @log.error('ERROR! Last IP is smaller than first. Aborting!')
57
+ error('ERROR! Last IP is smaller than the first (' << @first.to_s << ' - ' << @last.to_s << ')')
58
+ error('Aborting!')
65
59
  exit false
66
60
  end
67
61
  @vrange = args[0,2]
@@ -73,6 +67,8 @@ end
73
67
  ######### TEST #############
74
68
  if __FILE__ == $0
75
69
  rg = IPRange.new(['192.168.2.33', '192.168.255.150'])
70
+ #### ERROR :
71
+ # rg = IPRange.new(['23.122.9.11', '23.122.8.11'])
76
72
  puts rg.first
77
73
  puts rg.last
78
74
  puts rg.in_range? '192.168.1.35'
data/lib/version.rb CHANGED
@@ -1,4 +1,21 @@
1
- VERSION = 0.9
2
- SUMMARY="Ranges and text can be commented with a leading '#' "
1
+ =begin
2
+ /***************************************************************************
3
+ * ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
4
+ * *
5
+ * This program is free software; you can redistribute it and/or modify *
6
+ * it under the terms of the WTFPL 2.0 or later, see *
7
+ * http://www.wtfpl.net/about/ *
8
+ * *
9
+ * This program is distributed in the hope that it will be useful, *
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
12
+ * *
13
+ ***************************************************************************/
14
+ =end
15
+
16
+ VERSION = 1.02
17
+ SUMMARY="better error message when last IP < first IP in a range"
3
18
  AUTHORS=["Michael Uplawski@uplawski.eu"]
4
19
  EMAIL="michael.uplawski@uplawski.eu"
20
+ AUTHORS_MAIL=["<michael.uplawski@uplawski.eu>"]
21
+ YEARS= "2021-2024"
metadata CHANGED
@@ -1,34 +1,34 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ip_in_range
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.9'
4
+ version: '1.02'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Uplawski@uplawski.eu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-07 00:00:00.000000000 Z
11
+ date: 2024-04-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Verify that an IP is in a range
14
- email: michael.uplawski@uplawski.eu
14
+ email: "<michael.uplawski@uplawski.eu>"
15
15
  executables:
16
16
  - ip_in_range
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - README.md
20
21
  - bin/ip_in_range
21
22
  - doc/license.txt
22
23
  - ip_in_range.gemspec
24
+ - lib/basic_logging.rb
23
25
  - lib/email.rb
24
26
  - lib/file_checking.rb
25
27
  - lib/ip_range.rb
26
- - lib/log.conf
27
- - lib/logging.rb
28
28
  - lib/version.rb
29
29
  homepage: ''
30
30
  licenses:
31
- - GPL-3.0
31
+ - Nonstandard
32
32
  metadata: {}
33
33
  post_install_message:
34
34
  rdoc_options: []
@@ -38,15 +38,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: '2.7'
41
+ version: '3.0'
42
42
  required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
47
  requirements: []
48
- rubygems_version: 3.3.15
48
+ rubygems_version: 3.4.20
49
49
  signing_key:
50
50
  specification_version: 4
51
- summary: Ranges and text can be commented with a leading '#'
51
+ summary: better error message when last IP < first IP in a range
52
52
  test_files: []
data/lib/log.conf DELETED
@@ -1,56 +0,0 @@
1
- #encoding: UTF-8
2
- =begin
3
- /***************************************************************************
4
- * ©2013 - 2019 Michael Uplawski <michael.uplawski@uplawski.eu> *
5
- * *
6
- * This program is free software; you can redistribute it and/or modify *
7
- * it under the terms of the GNU General Public License as published by *
8
- * the Free Software Foundation; either version 3 of the License, or *
9
- * (at your option) any later version. *
10
- * *
11
- * This program 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 *
14
- * GNU General Public License for more details. *
15
- * *
16
- * You should have received a copy of the GNU General Public License *
17
- * along with this program; if not, write to the *
18
- * Free Software Foundation, Inc., *
19
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20
- ***************************************************************************/
21
-
22
- A simplified logger configuration. Set the level for each individual logger
23
- below. Choose a different log-device or log-file if you like. Keep the
24
- formatting intact. Do not change other sections of this file.
25
- =end
26
-
27
- # Do not touch from here ----->
28
- require 'logger'
29
-
30
- debug = Logger::DEBUG
31
- info = Logger::INFO
32
- error = Logger::ERROR
33
- fatal = Logger::FATAL
34
- warn = Logger::WARN
35
- unknown = Logger::UNKNOWN
36
- {
37
- # <---------------- to here !
38
-
39
- # Enter your settings here, but take into consideration that not all
40
- # the named classes will really produce readable output. Well, you can
41
- # always try... Either name just the log-level or make the log-level
42
- # precede the output-device or output-file like in the examples.
43
-
44
- # Example: naming a log-file
45
- #
46
- # :HtmlBuilder => [info, 'C:\temp\htmlbuilder.log'],
47
- #
48
- # :HtmlBuilder => [debug, '/tmp/htmlbuilder.log'],
49
-
50
- :FMail => info,
51
- :IPRange => info,
52
- :TopLevel => debug,
53
-
54
- # And ignore the remainder, too.
55
- }
56
- #eof