wol 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/VERSION +1 -1
  2. data/bin/wol +37 -27
  3. data/lib/parsefile.rb +11 -5
  4. data/lib/wol.rb +6 -6
  5. data/spec/wol_spec.rb +2 -2
  6. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/bin/wol CHANGED
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/ruby -w
2
2
 
3
3
  require 'optparse'
4
- require 'wol'
5
- require 'parsefile'
4
+ require 'lib/wol.rb'
5
+ require 'lib/parsefile.rb'
6
6
  require 'pp'
7
7
 
8
8
  class Runner
9
- SAMPLE_FILE = %{# File structure
9
+ @sample_file = %{# File structure
10
10
  # --------------
11
11
  # - blank lines are ignored
12
12
  # - comment lines are ignored (lines starting with a hash mark '#')
@@ -29,12 +29,12 @@ class Runner
29
29
  args = ["-h"] if args.empty?
30
30
 
31
31
  options = {}
32
- options[:verbose] = true
32
+ options[:quiet] = false
33
33
  options[:address] = "255.255.255.255"
34
34
  options[:port] = 9
35
35
  options[:interval] = 0.01
36
36
  options[:count] = 3
37
- options[:macs] = []
37
+ options[:macs] = Array.new
38
38
  options[:file] = nil
39
39
 
40
40
  opts = OptionParser.new do |opts|
@@ -46,22 +46,22 @@ class Runner
46
46
  opts.separator "Specific options:"
47
47
 
48
48
  opts.on( '-q', '--quiet', 'No console output' ) do
49
- options[:verbose] = false
49
+ options[:quiet] = true
50
50
  end
51
51
 
52
52
  opts.on( '-a', '--address 255.255.255.255', 'Set the destination address' ) do |address|
53
53
  options[:address] = address
54
54
  end
55
55
 
56
- opts.on( '-p', '--port 9', 'Set the destination port' ) do |port|
56
+ opts.on( '-p', '--port 9', Integer, 'Set the destination port' ) do |port|
57
57
  options[:port] = port
58
58
  end
59
59
 
60
- opts.on( '-i', '--interval 0.01', 'Interval between sending packets in seconds') do |interval|
60
+ opts.on( '-i', '--interval 0.01', Float, 'Interval between sending packets in seconds') do |interval|
61
61
  options[:interval] = interval
62
62
  end
63
63
 
64
- opts.on( '-c', '--count 3', 'Number of packets to send. Default 3') do |count|
64
+ opts.on( '-c', '--count 3', Integer, 'Number of packets to send. Default 3') do |count|
65
65
  options[:count] = count
66
66
  end
67
67
 
@@ -78,7 +78,7 @@ class Runner
78
78
  end
79
79
 
80
80
  opts.on_tail( '-s', '--sample-file', 'Display a sample file') do
81
- puts SAMPLE_FILE
81
+ puts @sample_file
82
82
  exit
83
83
  end
84
84
 
@@ -91,13 +91,15 @@ class Runner
91
91
  begin
92
92
  opts.parse!(args)
93
93
 
94
- args.each do |arg|
95
- options[:macs] << arg if /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/.match(arg)
96
- end
94
+ if options[:file].nil?
95
+ args.each do |arg|
96
+ options[:macs] << arg if /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/.match(arg)
97
+ end
97
98
 
98
- options[:macs].uniq!
99
+ options[:macs].uniq!
100
+ end
99
101
 
100
- options
102
+ return options
101
103
  rescue OptionParser::InvalidOption => e
102
104
  STDERR.puts e.message, "\n", opts
103
105
  exit(-1)
@@ -108,33 +110,41 @@ class Runner
108
110
  "Ruby Wake-On-LAN version 0.1.0"
109
111
  end
110
112
 
111
- def self.wake(options)
113
+ def self.wake(options = {})
112
114
  begin
113
- if options[:file].nil?
115
+ if options[:file].empty?
114
116
  if !options[:macs].empty?
115
- Wol.new(options).wake
117
+ puts Wol.new(options).wake unless options[:quiet]
116
118
  else
117
- "You have to specify a file or MAC address"
119
+ puts "You have to specify a file or MAC address"
118
120
  end
119
121
  else
120
- wake_from_file(options)
122
+ puts wake_from_file(options) unless options[:quiet]
121
123
  end
122
- rescue
123
- "An error occured. Please check your inputs.\nIf you used a file, please check that it is properly formatted."
124
+ rescue Exception => e
125
+ puts "An error occured. Please check your inputs."
126
+ puts "If you used a file, please check that it is properly formatted."
127
+ STDERR.puts e.message
128
+ exit(-1)
124
129
  end
125
130
  end
126
131
 
127
- def self.wake_from_file(options)
132
+ def self.wake_from_file(options = {})
128
133
  message = ""
129
134
  hosts = ParseFile.parse(options[:file])
130
135
 
131
136
  for host in hosts
132
- options[:host], options[:macs], options[:port] = host[:host], host[:mac], host[:port]
133
- message << Wol.new(options).wake
137
+ options[:address], options[:macs], options[:port] = host[:address], host[:mac], host[:port]
138
+
139
+ unless options[:quiet]
140
+ message << Wol.new(options).wake
141
+ end
134
142
  end
135
143
 
136
- return message
144
+ return options[:quiet] ? nil : message
137
145
  end
138
146
  end
139
147
 
140
- puts Runner.wake(Runner.parse(ARGV))
148
+ options = Runner.parse(ARGV)
149
+
150
+ Runner.wake(options)
@@ -1,23 +1,24 @@
1
1
  class ParseFile
2
+
2
3
  def self.parse(file)
3
4
  hosts = []
4
5
  if File.exist?(file) && File.readable?(file)
5
6
  File.open(file) do |f|
6
7
  while (line = f.gets)
7
8
  unless line.match(/^#/) || line.strip.empty?
8
- mac, host, port = line.strip.split
9
+ mac, address, port = line.strip.split
9
10
 
10
11
  port ||= 9
11
12
  host ||= "255.255.255.255"
12
13
 
13
- if check_mac(mac) && check_host(host)
14
- hosts << { :mac => mac, :host => host, :port => port.to_i}
14
+ if check_mac(mac) && check_host(address)
15
+ hosts << { :mac => mac, :address => address, :port => sanitize_port(port)}
15
16
  end
16
17
  end
17
18
  end
18
19
  end
19
20
  end
20
-
21
+
21
22
  return hosts
22
23
  end
23
24
 
@@ -26,6 +27,11 @@ class ParseFile
26
27
  end
27
28
 
28
29
  def self.check_host(host)
29
- /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/.match(host)
30
+ /(^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$)|(^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$)/.match(host)
31
+ end
32
+
33
+ def self.sanitize_port(port)
34
+ p = port.to_i
35
+ return (1..61000).include?(p) ? p : 9
30
36
  end
31
37
  end
data/lib/wol.rb CHANGED
@@ -11,7 +11,7 @@ require "socket"
11
11
 
12
12
  # Ruby version of the WakeOnLan command.
13
13
  class Wol
14
- attr_accessor :macs, :address, :port, :count, :interval, :verbose
14
+ attr_accessor :macs, :address, :port, :count, :interval, :quiet
15
15
  attr_reader :socket
16
16
 
17
17
  # Create a new instance
@@ -21,14 +21,14 @@ class Wol
21
21
  # * <tt>:port => 9</tt> - The destination port. Defaults to the discard port, 9
22
22
  # * <tt>:count => 1</tt> - How many times to send the MagicPacket. Defaults to 1
23
23
  # * <tt>:interval => 0.01</tt> - How many seconds to wait between sending packets. Defaults to 0.01
24
- # * <tt>:verbose => false</tt> - What to return? Returns a string summary if true, else returns nil
24
+ # * <tt>:quiet => false</tt> - What to return? Returns a string summary if false, else returns nil
25
25
  def initialize(options = {})
26
26
  @macs = options[:macs] ||= ["ff:ff:ff:ff:ff:ff"]
27
27
  @address = options[:address] ||= "255.255.255.255"
28
28
  @port = options[:port] ||= 9
29
29
  @count = options[:count] ||= 1
30
30
  @interval = options[:interval] ||= 0.01
31
- @verbose = true unless options[:verbose] == false
31
+ @quiet = options[:quiet]
32
32
 
33
33
  @socket=UDPSocket.open()
34
34
 
@@ -49,10 +49,10 @@ class Wol
49
49
  messages << send_packet(mac) + "\n"
50
50
  end
51
51
 
52
- if @verbose
53
- return messages
54
- else
52
+ if @quiet
55
53
  return nil
54
+ else
55
+ return messages
56
56
  end
57
57
  end
58
58
 
@@ -21,7 +21,7 @@ describe "Wake-On-LAN" do
21
21
  Wol.new(:macs => "00:08:a1:a9:58:f6", :address => "example.com", :port => 80).wake.should == "Sending magic packet to example.com:80 with 00:08:a1:a9:58:f6\n"
22
22
  end
23
23
 
24
- it "should return nil if verbose is set to false" do
25
- Wol.new(:verbose => false).wake.should == nil
24
+ it "should return nil if quiet is set to true" do
25
+ Wol.new(:quiet => true).wake.should == nil
26
26
  end
27
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matias Korhonen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-06 00:00:00 +02:00
12
+ date: 2009-11-08 00:00:00 +02:00
13
13
  default_executable: wol
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency